Binary Search Tree as known as BST is a tree data structure which stores data in a sorted way. Average search time of a
Binary Search Tree is log(n) where n is the number of tree nodes. See following explanation for more details. It's collected from
http://lcm.csa.iisc.ernet.in/dsa/node91.html
- A prominent data structure used in many systems programming applications for representing and managing dynamic sets.
- Average case complexity of Search, Insert, and Delete Operations is O(log n), where n is the number of nodes in the tree.
- DEF: A binary tree in which the nodes are labeled with elements of an ordered dynamic set and the following BST property is satisfied: all elements stored in the left subtree of any node x are less than the element stored at x and all elements stored in the right subtree of x are greater than the element at x.
- An Example: Figure 4.14 shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree.
- Note that inorder traversal of a binary search tree always gives a sorted sequence of the values. This is a direct consequence of the BST property. This provides a way of sorting a given sequence of keys: first, create a BST with these keys and then do an inorder traversal of the BST so created.
- Note that the highest valued element in a BST can be found by traversing from the root in the right direction all along until a node with no right link is found (we can call that the rightmost element in the BST).
- The lowest valued element in a BST can be found by traversing from the root in the left direction all along until a node with no left link is found (we can call that the leftmost element in the BST).
- Search is straightforward in a BST. Start with the root and keep moving left or right using the BST property. If the key we are seeking is present, this search procedure will lead us to the key. If the key is not present, we end up in a null link.
- Insertion in a BST is also a straightforward operation. If we need to insert an element x, we first search for x. If x is present, there is nothing to do. If x is not present, then our search procedure ends in a null link. It is at this position of this null link that x will be included.
- If we repeatedly insert a sorted sequence of values to form a BST, we obtain a completely skewed BST. The height of such a tree is n - 1 if the tree has n nodes. Thus, the worst case complexity of searching or inserting an element into a BST having n nodes is O(n).
Figure 4.14: An example of a binary search tree
|
Deletion in BST Let x be a value to be deleted from the BST and let X denote the node containing the value x. Deletion of an element in a BST again uses the BST property in a critical way. When we delete the node X containing x, it would create a "void" that should be filled by a suitable existing node of the BST. There are two possible candidate nodes that can fill this void, in a way that the BST property is not violated: (1). Node containing highest valued element among all descendants of left child of X. (2). Node containing the lowest valued element among all the descendants of the right child of X. In case (1), the selected node will necessarily have a null right link which can be conveniently used in patching up the tree. In case (2), the selected node will necessarily have a null left link which can be used in patching up the tree. Figure
4.15 illustrates several scenarios for deletion in BSTs.
Figure 4.15: Deletion in binary search trees: An example
|
Binary Search Tree Example Code in C++
022 | node *current,*parent; |
026 | root=parent=current=NULL; |
029 | void insert(node *target){ |
036 | if (!search(target->key)){ |
037 | if (target->key<parent->key) parent->left=target; |
038 | else parent->right=target; |
040 | else cout<<target->key<< " Already Exist" <<endl; |
045 | node *search( int inputKey){ |
048 | if (inputKey==current->key) return current; |
050 | if (inputKey<current->key) current=current->left; |
051 | else current=current->right; |
055 | void searchPath( int key){ |
057 | int *pathAry= new int [AllNodeNum]; |
060 | if (key==current->key){ |
061 | for ( int j=0;j<i;j++) cout<<pathAry[j]<< "->" ; |
062 | cout<<current->key<<endl; |
066 | if (key<current->key){ |
067 | pathAry[i++]=current->key; |
069 | current=current->left; |
072 | pathAry[i++]=current->key; |
074 | current=current->right; |
077 | cout<< "Key don't exist in Tree" <<endl; |
086 | cout<< "This key don't exist this BST" <<endl; |
089 | else if (temp->left==NULL&&temp->right==NULL){ |
091 | if (temp==parent&&parent==current) root=NULL; |
093 | else if (parent->left==temp) parent->left=NULL; |
094 | else parent->right=NULL; |
096 | else if (temp->left!=NULL&&temp->right!=NULL){ |
098 | node *chnode=temp->left; |
099 | node *chnodeParent=temp; |
100 | while (chnode->right){ |
102 | chnode=chnode->right; |
104 | int slot=chnode->key; |
105 | chnode->key=temp->key; |
108 | if (chnodeParent==temp) chnodeParent->left=chnode->left; |
109 | else chnodeParent->right=chnode->left; |
111 | if (chnodeParent==temp) chnodeParent->left=NULL; |
112 | else chnodeParent->right=NULL; |
119 | if (parent==current&&temp==root) root=temp->left; |
120 | else if (parent->left==temp) parent->left=temp->left; |
121 | else parent->right=temp->left; |
124 | if (parent==current&&temp==root) root=temp->right; |
125 | else if (parent->left==temp) parent->left=temp->right; |
126 | else parent->right=temp->right; |
135 | if (!root) cout<< "Empty Tree" <<endl<<endl; |
137 | void printTree(node *point){ |
139 | cout<< "Key Value : " <<point->key<<endl<< "Current Node : " ; |
140 | cout<<point<< " Left Node : " <<point->left; |
141 | cout<< " Right Node : " <<point->right<<endl; |
142 | printTree(point->left); |
143 | printTree(point->right); |
150 | tree *bst= new tree(); |
151 | cout<< "<Binary Search Tree>" <<endl; |
153 | cout<< "========================================================" <<endl; |
154 | cout<< "1. Insert" << " 2. Delete" << " 3. Search Path" << " 4. Print" ; |
155 | cout<< " 0. Exit" <<endl; |
156 | cout<< "========================================================" <<endl; |
157 | cout<< "Plz Enter Num : " ; |
161 | cout<< "Enter What you want to Insert Num : " ; |
163 | node *temp= new node(num); |
168 | cout<< "Enter What you want to Delete Num : " ; |
170 | delNode=bst->del(num); |
171 | if (delNode!=NULL) delete delNode; |
174 | cout<< "Enter What you want to Search Num : " ; |
176 | bst->searchPath(num); |
182 | cout<< "--------------------------------" <<endl; |
183 | cout<< "Thanks for using Program GoodBye" <<endl; |
187 | cout<< "Plz Re-Enter Menu" <<endl; |