Premise
The previous lookup we are all static lookup, because the data set is stored in an orderly way, there are many ways to find, can use binary, interpolation, Fibonacci, etc., but because of the order, the efficiency of the insertion and deletion is not high. At this point we need a dynamic search method, both to efficiently implement the search, but also to make the insertion and removal efficiency is good, then we can consider the binary sorting tree
Binary sort tree One: definition
Also known as binary search tree (find tree), is a tree, can be empty, but need to meet the following properties:
1 . All key values of the non-empty left subtree are less than the key values of their root nodes 2 all key values of the non-empty right subtree are greater than the key values of their root nodes 3. The left and right sub-trees are two-fork search tree
Two: Operation find
/* Bitree T We're going to search for the two fork tree Elemtype key We're going to search for the keyword bitree F records Our parent node of the current search subtree bitree* P When we insert, we will first search for the existence of data, if present, Do not insert, if not present, we can get the position we want to insert through this, directly insert can * /
Status Searchbst (Bitree T, Elemtype key, Bitree F, bitree* P)
/*Bitree T We're going to search for the two fork tree Elemtype key We're going to search for the keyword Bitree F records our parent node of the current search subtree bitree* P when we insert before, will first search for the existence of data, if present, do not insert, if not exist, we By this we can get the position we want to insert and insert it directly .*/Status Searchbst (bitree T, Elemtype key, Bitree F, Bitree*P) { if(!T) {*p = F;//returns the parent node location if not found returnFALSE; } Else { if(T->data = =key) { *p = T;//if found, p returns to the node position. returnTRUE; } Else if(T->data <key)returnSearchbst (t->rchild, Key, T, P); Else returnSearchbst (t->lchild, Key, T, P); }}
View Code
Insert
int key)
Because the special properties of the binary search tree determine that each element in a two-fork search tree may only occur once, it is not inserted if it is found that the element already exists in the binary search tree during the insertion process. Otherwise, find the right place to insert.
First case: Empty tree---> Direct INSERT, return True
The second case is that the element to be inserted already exists, and if the element already exists in the binary search tree, it is no longer inserted and the direct return is false;
Third case: can find the appropriate location, through the search function search above to return to its parent node, directly inserted can
Status Insertbst (bitree* T,intkey) {Bitree p,s; if(!T)returnERROR; if(! Searchbst (*t, Key, NULL, &P)) {//did not find duplicate data, get to the location should be inserteds = (bitree) malloc (sizeof(Bitnode)); S->data =key; S->lchild = S->rchild =NULL; if(! P//Empty Tree*t =s; Else if(Key < P->data)//Insert left sub-treeP->lchild =s; ElseP->rchild =s; returnOK; } Else returnERROR;}
View Code
Delete (the above two are relatively easy, no longer detailed, delete more cases)
int key)
1. Delete the node is the leaf node, directly delete (directly determine whether the left and right pointer exists)
2. Delete the node only the left child, directly let the node's parent point to its left child
3. Delete the node only the right child, direct the node's parent point to its right child
Note: In fact, 1 can be replaced by 2, 3, because 2, 31, including the left and right subtree does not exist, where 2, 3 points to the left and right sub-tree is null is the case of 1
4. Deleted node contains left and right children, need to be classified discussion (emphasis) < Here consider one, is to use the smallest to replace, you can also select the left subtree max to replace >
Note: The above two cases can be categorized as the second picture, which directly gives the yellow parent node the left pointer to the right child of its green node.
The third case is that the parent node of the green (non-existent Zuozi) is the red junction to be deleted, and we want to make its new node right pointer point to the original green right pointer .
Status Delete (bitree*T) {Bitree q,f; if(!*T)returnERROR; if(! (*t)->lchild)//If the left subtree doesn't exist, we just need to get to the right subtree.{Q= *u; *t = (*t)Rchild; Free (q); } Else if(! (*t)->rchild)//If the right subtree does not exist, access the left sub-tree{Q= *T; *t = (*t)Lchild; Free (q); } Else //Both sides exist, we can choose to the smallest right subtree, or the left subtree maximum access, here Select the right subtree minimum{f= *t;//F point to the parent node of QQ = (*t)Rchild; while(q) {f=Q; Q= q->lchild;//find the smallest right subtree, note that it may exist right subtree, we want to save, access its parent node } //Update the minimum data to the root node, then record the minimum point and delete(*t)->data = q->data; if(F! = (*T)) F->lchild = q->Rchild; ElseF->rchild = q->rchild;//when the right sub-tree is a right-leaning treeFree (q); } returnTRUE;} Status Deletebst (BitreeTintkey) { if(!*T)returnERROR; Else { if((*t)->data = = key)//find it, start deleting it { //Delete the node, because it is discussed separately, so write a function } Else if((*t)->data <key) Deletebst (& (*t)rchild, key); ElseDeletebst (& (*t)lchild, key); }}
View Code
Three: Code implementation
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#defineOK 1#defineERROR 0#defineTRUE 1#defineFALSE 0typedefintStatus;typedefintElemtype;typedefstruct_bitnode{elemtype data; struct_bitnode* Lchild, *Rchild;} Bitnode,*Bitree;/*Bitree T We're going to search for the two fork tree Elemtype key We're going to search for the keyword Bitree F records our current search subtree's parent node bitree* P when we insert before, will first search whether there is data, if present, do not insert, if not exist, I We can get the position we want to insert and insert it directly .*/Status Searchbst (bitree T, Elemtype key, Bitree F, Bitree*P) { if(!T) {*p = F;//returns the parent node location if not found returnFALSE; } Else { if(T->data = =key) { *p = T;//if found, p returns to the node position. returnTRUE; } Else if(T->data <key)returnSearchbst (t->rchild, Key, T, P); Else returnSearchbst (t->lchild, Key, T, P); }}status Insertbst (BitreeTintkey) {Bitree p,s; if(!T)returnERROR; if(! Searchbst (*t, Key, NULL, &P)) {//did not find duplicate data, get to the location should be inserteds = (bitree) malloc (sizeof(Bitnode)); S->data =key; S->lchild = S->rchild =NULL; if(! P//Empty Tree*t =s; Else if(Key < P->data)//Insert left sub-treeP->lchild =s; ElseP->rchild =s; returnOK; } Else returnERROR;} Status Delete (Bitree*T) {Bitree q,f; if(!*T)returnERROR; if(! (*t)->lchild)//If the left subtree doesn't exist, we just need to get to the right subtree.{Q= *T; *t = (*t)Rchild; Free (q); } Else if(! (*t)->rchild)//If the right subtree does not exist, access the left sub-tree{Q= *T; *t = (*t)Lchild; Free (q); } Else //Both sides exist, we can choose to the smallest right subtree, or the left subtree maximum access, here Select the right subtree minimum{f= *t;//F point to the parent node of QQ = (*t)Rchild; while(q) {f=Q; Q= q->lchild;//find the smallest right subtree, note that it may exist right subtree, we want to save, access its parent node } //Update the minimum data to the root node, then record the minimum point and delete(*t)->data = q->data; if(F! = (*T)) F->lchild = q->Rchild; ElseF->rchild = q->rchild;//when the right sub-tree is a right-leaning treeFree (q); } returnTRUE;} Status Deletebst (BitreeTintkey) { if(!*T)returnERROR; Else { if((*t)->data = = key)//find it, start deleting it { //Delete the node, because it is discussed separately, so write a function } Else if((*t)->data <key) Deletebst (& (*t)rchild, key); ElseDeletebst (& (*t)lchild, key); }}intMain () {intI,ret; inta[Ten] = { +, the, -, -, *, the,Wuyi, About,Panax Notoginseng, the }; Bitree T=NULL; Bitree P=NULL; for(i =0; I <Ten; i++) Insertbst (&T, a[i]); RET=searchbst (T, -, NULL, &P); if(ret) printf ("Find node\n"); Deletebst (&t, the); RET= Searchbst (T, -, NULL, &P); if(ret) printf ("Not find node, it has deleted\n"); System ("Pause"); return 0;}
Data structure (vi) Find---binary search tree (sort tree)