Uploaded by Arslan Amin

Binary Search Tree (1)

advertisement
Binary Search Tree
A special type of binary tree where each node in the tree have property that value of node is always
larger than left child and value of node is always smaller than right child. If we apply the definition we
can say:
-
Root node is always larger than all nodes on left sub tree and smaller than all nodes on right sub
tree
- Similarly every node has larger value than all values of all nodes of left sub tree and smaller value
than all nodes on right sub tree
Some examples of BST are:
Here we have taken 2 binary search trees having alphabets on
each node, where each node has smaller alphabets on left and
larger alphabets on right.
Just to revise inorder traversal of left side BST is:
ABDEFMN
Which is in sorted in ascending order. You can check this fact for all
BST’s that inorder gives sorted values in ascending order.
D
B
A
B
M
F
D
A
N
E
The advantage of BST is fast search. Starting search from root we have to move on either left side or
right side while ignoring the other side because it is impossible to have value on both sides, if value is
smaller it will definitely exist on left side and otherwise on right side or does not exist. Therefore in
every case we have to search on some specific path, mean complexity of search is O(height).
Recalling height of a tree is no of nodes on longest branch. Therefore now all matters is height. Again
recalling if a binary tree has n nodes, it has:
Maximum Height: n
Minimum Height: lg2n
Therefore for a BST search complexity can vary from n to lgn depending on height. Now important is
how we construct a BST.
Insertion
It is well known that a Binary Tree Array implementation requires height where we take array of size 2h1 and store values on specific indexes by calculating left & right child appropriately. In BST creation is
simply by inserting values 1 by 1, now it depends how values comes. It can be ideal or worst, you can
construct multiple BST for same values, all it matters the order of data, we are not considering any pre
calculation on data before insertion. Therefore, insertion is if root node not exist make current value as
root node. If root exist compare value and move to left or right depending on value is smaller or larger.
Moving from root to down on any particular branch whenever a null value found place new value.
Therefore we are concluding our discussion by inserting values: D B A M F E N
D is first value so make it root
D
Now D is root node. Compare B to D, B is smaller than D so make it left child of D.
D
B
Again D is root. Compare A to D, D is larger so move to left. Again compare B to A,
B is larger so place A as left child of B
D
B
A
Again D is root. Compare M to D, D is smaller so make M right child of D
D
B
M
A
Now we have to insert F. Compare F with D. F is larger so move to right. Compare
F to M. F is smaller so make it left child of F. Here we can see that because F is
larger than D, it is on right sub tree, whereas, F is smaller than M so it is on left
sub tree of M
D
B
M
F
A
E is larger than D so move to right side. M is larger than E so move to left side. F is
larger than E so make E as left child of F
D
B
M
F
A
E
N is larger than D so move to right side. Again N is larger than M so make it right
child of N
D
B
A
F
E
Next we will discuss deletion of BST.
M
N
Download