16. Two Fork Sorting tree

Source: Internet
Author: User

Reprint please name Source: http://blog.csdn.net/u012637501 one or two fork sorting treeIf the dataset you are looking for is an ordered linear table and is stored sequentially, the lookup can be implemented using binary, interpolation, Fibonacci, and other lookup algorithms. Then, because of the order, when we are on the insert and delete operations, it takes a lot of time. The following two-fork sorting tree will be learned, which can make the insertion and deletion efficient, but also can be more efficient to implement the search algorithm. To do this, the purpose of constructing a binary sort tree is not to sort, but to provide the speed at which to find and insert delete keywords. 1. Two fork sorting tree conceptThe two-fork sort tree, also known as a two-fork lookup tree , is either an empty tree, or a two-fork tree with the following properties. if its left dial hand tree is not empty , then the value of all nodes on the left subtree is less than the value of its root structure (parent node);if its right subtree is not empty , then the value of all nodes on the right subtree is greater than the value of its root node (parent node);its left and right sub-trees are also two-fork sorting trees . 2. Two fork tree node structure
/* Two fork tree two-fork linked list node structure definition */typedef struct Bitnode                        //node structure {    int data;                                            Node Data    Struct Bitnode *lchild,*rchild;       Left and right child pointer}bitnode,*bitree;   
two or two-fork sorting tree operation algorithm 1. Query operation for two-fork sorting tree
/* Recursively finds if a key exists in the binary sort tree T,   * Pointer f points to the parents of T, whose initial call value is NULL    * If the lookup succeeds, the pointer p points to the data element node and returns True Otherwise, the pointer p points to the last node accessed on the lookup path and returns False*/status Searchbst (Bitree t,int key,bitree f,bitree *p) {    if (! T)                            //Find unsuccessful (for empty tree)    {            *p=f;            return FALSE;    }    else if (key==t->data)  //Find success    {            *p=t;            return TRUE;    }    else if (key<t->data)            return Searchbst (t->lchild,key,t,p);        Continue to find the    else            return Searchbst (t->rchild,key,t,p) in Zuozi;        Continue looking in the right subtree  }
Example : If there is a data set ={62,88,58,47,35,73,51,99,37,93}, look for the keyword key=93. use the two-fork sorting tree to find the algorithm steps as follows:① The data set is constructed into a binary sort tree ( Middle sequence traversal ) According to the binary sort tree definition;
② Call binary sort tree Query algorithm Searchbst (t,93,null,p) query keyword, where Searchbst function is a recursive function, parameter T is a binary tree list , key stands for query keyword , binary tree f . When T points to the root node, the initial value of f is null, it is useful in recursion, and the last parameter p is to find a successful Find the node location . ③if (! T){.... } statement. Used to determine whether the current binary tree is to the leaf node, at this point the current T points to the root node 62 position , because T is not empty, so the statement fragment does not execute. The ④esle if (key==t->data) statement. That is to find the matching keyword execution statement, obviously 93!=62, so the statement fragment does not execute. ⑤else if (key<t->data) statement. That is, when you want to find the keyword less than The statement is executed when the current node is 93>62, so the statement fragment is not executed. ⑥else{....} Statement. This is when executes the statement when you want to find the keyword greater than the current node. 93>62, so with recursive call searchbst (T->rchild,key, t,p) . At this time, t points to 62 right child 88 , f point to 88 parent knot point , which is 62.
⑦ At this time the second layer searchbst, because 93:88 big, so executes else{....} statement, and recursively calls Searchbst (t->rchild,key,t,p)again. At this point T points to 88 right child 99.
⑧ At this time the third layer Searchbst, because 93:99 is small, therefore executes the else if (key<t->data) statement, again recursively calls Searchbst (t->lchild,key,t,p) . At this point T pointed to the left child of 99 of the 93.
⑨ Fourth Searchbst, because key equals T->data, so the 10th to 11th line is executed, the pointer p points to the node where 93 is located and returns true to the third, second, first, and finally return function true. 2. Insert operation for two-fork sort treethe so-called binary sort tree is inserted, and the keyword is placed in the appropriate position in the tree. (1) interpolation algorithm for binary sorting tree
/* when the binary sort tree T does not have the number of keywords equal to key    When the element is inserted, * Insert key and return True, otherwise return false*/status Insertbst (bitree *t,int key) {Bitree p,s; /* Call lookup function to find out if the keyword is present *///a. If the lookup is unsuccessful if (!    Searchbst (*t,key,null,&p)) {s= (bitree) malloc (sizeof (Bitnode));                                       Open a memory space for node S s->data=key;                Place the keyword in the data field where s points to the node s->lchid=s->rchild=null;                Initializes the left and right pointer fields of the node s if (!p) *t=s;             Insert S for the new root node else if (key<p->data)//If the keyword is less than the P-node data value, insert S is the left child of node p p->lchild = s;       else//If the keyword is greater than the P-node data value, insert S is the right child of node P p->rchild=s;        }/* The node already has the same keyword in the tree, no longer inserted */else {return FALSE; }}
Example: If we call the function "Insertbst", then the result is false, if the calling function is "Insertbst (t,95)", then it must be added a right child 95 in the 93 node and returns True. It is important to note that, since the insertion algorithm calls the SEARCHBST (*T,KEY,NULL,&P) lookup algorithm in advance and uses the middle sequence to traverse the binary tree, we finally know that the pointer p points to the node of thex. 3. Constructing binary sorting tree algorithm
 
/* If there is a dataset ={62,88,58,47,35,73,51,99,37,93}    * Build a binary sort tree */int i;int a[0]={62,88,58,47,35,73,51,99,37,93}; Bitree t=null;for (i=0;i<10;i++) {    insertbst (&t,a[i]);}
4. Two fork sorting tree delete operation algorithm
(1) recursive way to the two-fork sort tree t find key, and then call delete function to delete the node./ * Delete the data element node if there is a data element in the binary sort tree T that is equal to key* and returns true; false*/
Status Deletebst (Bitree *t,int key) {     if (!*t)    //There is no data element with key equal to key            return FALSE;    else    {        if (key== (*t)->data)    //Find the data element with the keyword equal to key                return Delete (T);    Call the delete function to delete the node        else if (key< (*t)->data)                return Deletebst (& (*t)->lchild,key);            else                return Deletebst (& (*t)->rchild,key);    }   }
(2) Delete delete algorithm
/* remove node p from two-fork sorting tree    , and re-connect its left or right subtree */status Delete (Bitree *p) {Bitree q,s;    /* Case TWO: Delete the right subtree of node p or the left subtree is empty */if ((*p)->lchild==null)//a. The right subtree is empty simply by re-connecting its left subtree {q=*p;    *p= (*p)->lchild;    Free (q);    } else if ((*p)->rchild==null)//b. Simply re-connect its right subtree {q=*p;    *p= (*p)->rchild;    Free (q);    The node to which the pointer p points}//Case three: the left and right subtree are not empty else {q=*p;            S= (*p)->lchild;    while (S->rchild)//Turn left and then right to the end (find the precursor to the delete node) {q=s;                       s=s->rchild;    } (*P)->data=s->data;    S direct precursor to the deleted node if (q!=*p) q->rchild=s->lchild;    To re-connect the right subtree of Q q->lchild=s->lchild; Else    Re-connect the left subtree of Q free (s); } return TRUE;} 
Source Analysis:q=*p;    *p= (*p)->rchild;   Free (q); function: Assigns the node P point to the new node Q, and the P pointer points to the left node, that is, to implement the re-connected right subtree, and then release the node Q.
three or two fork sort tree summaryThe binary sort tree is aHow to linkStorage, preserving the advantages of the link storage structure when performing an insert or delete operation without moving elements, as long as the appropriate insert and delete locations are found, onlyModify a link pointerCan.Insert Delete time performance is better, and for binary sort treesFind, the Way isThe path from the root node to the node to find, with a comparison of the number of levels of the node in the binary sort tree that is equal to the given value。 In extreme cases, at least 1 times, that is, the junction is the node to be found, at most, it will not exceed the depth of the tree, i.e. Two fork sort tree The lookup performance depends on the shape of the two-fork sort tree 。

16. Two Fork Sorting tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.