
Definition:
A binary search tree (BST), also called an ordered or
sorted binary tree, is a rooted binary tree in which the value of left
node must be smaller than the parent node, and the value of right
node must be greater than the parent node. This rule is applied
recursively to the left and right subtrees of the root.
A BST is considered balanced if every level of the tree is fully filled with
the exception of the last level. On the last level, the tree is filled left to
A Perfect BST is one in which it is both full and complete (all child
nodes are on the same level and each node has a left and a right child
The time complexity of operations on the binary search tree is directly
proportional to the height of the tree. It's a non-linear data structure as it
does not store data in a sequential manner, but stores in a hierarchical
Insertion:
Draw a BST by inserting the following numbers from
left to right: 11 6 8 19 4 10 5 17 43 49 31
Scan the list from left to right 11 is root. Left subtree will have the
smaller numbers from 11 and right subtree will have greater than 11. It is
same for left and right subtree as well. Next 6, smaller than 11 so left. 8,
smaller than 11 but greater than 6 so left of 11 and right of 6. 19,
greater than 11, so right of 11. 17, greater than 11 but smaller than 19
so right of 11 and left of 19. and goes on…
Deletion of a node, say z, from a BST T should abide three cases:
If z is a leaf node, the parent node's pointer to z gets replaced with nil
and consequently z gets removed from the tree.(i.e. if we want to delete
5 from the above tree then we can directly delete it nothing have to
replaced just delete it. )
If z has a single child node, the child gets elevated as either left or
right child of z's parent depending on the position of z within the BST
and as a result z gets deleted.(i.e. if we want to delete 4 from the
above tree then we can delete and 5 which is it's right child that will
replace with 4. If any single left child would be here that will replace
If z has both a left and right child, the either inorder successor or
inorder predecessor of z takes the position of z in the tree:
If y is z's inorder successor, y gets elevated and replace z from z's
initial left sub-tree. (i.e. if we want to delete 11 from the above
tree then we can delete and inorder successor from left subtree, 10
which is also greatest in the left subtree of 11 will replace 11)
If y is z's the inorder predecessor, y gets elevated and replace z from
z's initial right sub-tree.(i.e. if we want to delete 11 from the above
tree then we can delete and inorder predecessor from right subtree,
17 which is also smallest in the right subtree of 11 will replace
Both the 2 cases of point 3 is true.
Construct Binary Search Tree(BST) from Preorder
Preorder: 20 16 5 18 17 19 60 85 70 (Root-Left-Right)
When preorder is given we have to find out the inorder
traversal. In BST inorder traversal always in ascending order.
Inorder: 5 16 17 18 19 20 60 70 85 (Left-Root-Right)
For root scan preorder left to right first one is root i.e. 20.
Then find 20 in inorder and from starting to 20 (5 16 17 18
19) will be part of left subtree and after 20 (60 70 85) will
be part of right subtree. Find which is after 20 in preorder, 16
will be the root of left subtree, in inorder left of 16 will be left
part and right will be right part. Find what is after 16, 5
(already placed), next 18 will be the root. And goes on…
Postorder: 5 17 19 18 16 70 85 60 20 (Left-Right-Root)
Here also we can first find inorder traversal or we can simply
construct the tree with postorder only.
For root scan postorder right to left first one is root i.e. 20.
Then find 20 in inorder and from starting to 20 (5 16 17 18
19) will be part of left subtree and after 20 (60 70 85) will
be part of right subtree. Find which is after 20 that is part of
left subtree in postorder, 16 will be the root of left subtree, in
inorder left of 16 will be left part and right will be right part.
Find what is after 16, 18 will be the root. And goes on…
For only postorder find which is smaller than 20 after 20
i.e. 16 so it will be the root of left subtree. Then 18 is
greater than 16 so it is right root of 16. For right subtree
of 20 find which is after 20 that will be the root of right