Find - binary sort tree (find, insert, delete)

Question: In order to maintain the order of t...
Binary sort tree
Question:

In order to maintain the order of the data, the ordered linear table uses: half / dichotomy, interpolation, Fibonacci search to improve the efficiency of sequential search, but the efficiency of insertion and deletion is low

How to improve the efficiency of insertion and deletion when searching efficiently?

In some application scenarios: need to insert and delete when finding

resolvent:

Binary sort tree

Characteristics of binary sort tree

1) If the left subtree is not empty, the values of all nodes on the left subtree are less than or equal to the values of its root nodes

2) If the right subtree is not empty, the values of all nodes on the right subtree are greater than or equal to the values of its root nodes

3) The left and right subtrees are also binary sorting trees

The complexity of binary sorting algorithm:

Time complexity: the idea of binary search. The number of searches is the height of binary search tree. If the tree is balanced binary tree, it is O(logn). Otherwise, the worst case is right skew tree O(n)

The disadvantages of binary sorting algorithm are as follows:

There are many types of binary tree construction. Different shape of binary tree will lead to great difference in search performance, such as ordinary binary tree and a right skew tree.

Search of binary sort tree:

1) Find the data key, and judge whether the key is equal to the root node data of the tree

2) If the key of the data to be queried is less than the root node data, it is recursively searched in the left subtree

3) If the key of the data to be queried is greater than the root node data, it is recursively searched in the right subtree

C pseudo code:

//Define node structure typedef struct BiTNode { int data; //Node data struct BiTNode *lchild, *rchild;//Left and right child pointer }BiTNode,*BiTree
/Recursive search of binary sort tree T Does it exist in key //f points to T's parents //p gets the location of the found node Status SearchBST(BiTree T,int key,BiTree f,BiTree *p) { //Failed to find if(!T)//Determine whether the current binary tree reaches the leaf node { *p=f;//Pointer p points to the last node accessed on the lookup path and returns false return False; } //Search success else if(key==T->data) { *p=T; return True; } else if(key<T->data)//Element to be searched is less than node data return SearchBST(T->lchild,key,T,p);//Continue searching in the left subtree else return SearchBST(T->rchild,key,T,p);//Continue searching in the right subtree }

Insert binary sort tree:

1) If the data key to be inserted cannot be found in the binary sort tree, then perform step 2)

2) The data to be inserted is initialized as node S. if the tree is empty, assign node s directly to the tree

3) If the data key to be inserted is less than the root node data, it will be inserted as the left child

4) If the data key to be inserted is greater than the root node data, it will be inserted as the right child

//Define node structure typedef struct BiTNode { int data; //Node data struct BiTNode *lchild, *rchild;//Left and right child pointer }BiTNode,*BiTree

//Data insertion of binary tree Status InsertBST(BiTree *T,int key) { BiTree p,s;//Create a binary tree node //key not found in binary sort tree if(!SearchBST(*T,key,NULL,&p)) { //Initialization of s node s=(BiTree)malloc(sizeof(BiTNode)); s->data=key; s->lchild=s->rchild=NULL; //If p node is empty if(!p) *T=s; else if (key<p->data)//The value key to be inserted is less than the data pointed to by the p node p->lchild=s;//s inserted as left child else//The value key to be inserted is greater than the data pointed to by the p node p->rchild=s;//s inserted as right child return True; } //There are nodes with the same keyword in the tree, so they will not be inserted else return False; }

An example of building a binary sort tree:

//Generate a binary tree int i; int a[10]=; Bitree T=NULL; for(i=0;i<10;i++) { InsertBST(&T,a[i]); }

Delete binary sort tree:

There are three situations for deleting nodes:

1) Delete leaf node

2) The deleted node has only left or right subtrees

3) Deleted nodes have left and right subtrees

//Define node structure typedef struct BiTNode { int data; //Node data struct BiTNode *lchild, *rchild;//Left and right child pointer }BiTNode,*BiTree
//Delete the data node whose element is equal to key Status DeleteBST(BiTree *T,int key) { //There is no data element with key equal to key if(!*T) return False; else { if(key==(*T)->data) //Find data element with key equal to key return Delete(T); else if(key<(*T)->data)//The key of the element to be deleted is smaller than the found element --- search in the left subtree of the node return DeleteBST(&(*T)->lchild,key); else //The key of the element to be deleted is greater than the found element --- search in the right subtree of the node return DeleteBST(&(*T)->rchild,key); } }
Status Delete(BiTree *p) { BiTree q,s; //In the first case, delete nodes only have left or right subtrees if((*p)->rchild==NULL)//Only the left subtree { q=*p; *p=(*p)->lchild; free(q); } else if((*p)->lchild==NULL)//Only right subtree { q=*p; *p=(*p)->rchild; free(q); } //In the second case, the deleted nodes include left subtree and right subtree else { q=*p;//The node to be deleted gives temporary variable q s=(*p)->lchild;//The left subtree pointed by the node to be deleted is given to the temporary variable s while(s->rchild)//Left subtree s looks right until it finds the precursor of the node to be deleted { q=s; s=s->rchild; } (*p)->data=s->data;//s points to the direct precursor of the deleted node, and assigns its value directly to the node to be deleted * p if(q!=*p)//Direct precursor of deleted node p! = root node q of direct precursor of deleted node q->rchild=s->lchild;//The right child of root node q points to the left child of the direct precursor of the deleted node else q->lchild=s->lchild; free(s); } }

Note the above Codes:

1) q!=*p:

2) q=*p:

If the structure of the above figure is modified to:

There are no nodes 37 and 36, and s points to 35.  

If the points of p and q are the same at 47 nodes, then assign 29 pointed by S - > lchild to q - > lchild

Follow up: how to solve the imbalance caused by inserting new nodes into the binary sort tree multiple times?

heda3 Published 142 original articles, praised 114, visited 90000+ Private letter follow

4 February 2020, 04:36 | Views: 8800

Add new comment

For adding a comment, please log in
or create account

0 comments