Binary Search Tree Notes
Binary Search Tree Notes
– All keys in the left sub-tree of T are less than the root element.
– All keys in the right sub-tree of T are greater than the root element.
• Searching become very efficient in a binary search tree since, we get a hint at each step,
about which sub-tree contains the desired element.
• The binary search tree is considered as efficient data structure in compare to arrays and
linked lists. In searching process, it removes half sub-tree at every step. Searching for an
element in a binary search tree takes o(log2n) time. In worst case, the time it takes to
search an element is 0(n).
• It also speed up the insertion and deletion operations as compare to that in array and
linked list.
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
The process of creating BST by using the given elements, is shown in the image below.
Operations on Binary Search Tree
There are many operations which can be performed on a binary search tree.
1. Searching: Finding the location of some specific element in a binary search tree.
2. Insertion in BST: Adding a new element to the binary search tree at the appropriate
location so that the property of BST do not violate.
3. Deletion in BST: Deleting some specific node from a binary search tree. However,
there can be various cases in deletion depending upon the number of children, the
node have.
1. Searching:
Searching means finding or locating some specific element or node within a data structure.
Compare the element with the root of the tree.
It is the simplest case. If the key ‘u’ to be deleted is a leaf node then the appropriate link
field of parent node of key u only needs to be set as NULL and free the allocated space.
If the u to be deleted has either a left subtree or right subtree(not both), then the link of
parent nodeof u is set to point to the appropriate subtree.
In the following image, the node 12 is to be deleted. It has only one child. The node will be
replaced with its child node and the replaced node 12 (which is now leaf node) will simply
be deleted.
3. The node to be deleted has two children.
(Or However, the node which is to be deleted, is replaced with its in-order successor or
predecessor . In the following image, the node 50 is to be deleted which is the root node of
the tree. The in-order traversal of the tree given below.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree,
which will simply be deleted.
6,25,30,50,52,60,70,75