- Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order. This is also called ordered binary tree.
- In a binary search tree, the value of all the nodes in the left sub-tree is less than the value of the root.
- Similarly, value of all the nodes in the right sub-tree is greater than or equal to the value of the root.
- This rule will be recursively applied to all the left and right sub-trees of the root.
Example
Number of distinct binary search trees possible with 3 distinct keys
= 2×3C3 / 3+1
= 6C3 / 4
= 5
If three distinct keys are A, B and C, then 5 distinct binary search trees are-
Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
When elements are given in a sequence,
- Always consider the first element as the root node.
- Consider the given elements and insert them in the BST one by one.
Insert 60
As 60 > 50, so insert 60 to the right of 50.
As 60 < 70, so insert 60 to the left of 70.
Construct a Binary Search Tree (BST) for the following sequence of numbers-
43, 10, 79, 90, 12, 54, 11, 9, 50
Solution:
Construct a Binary Search Tree (BST) for the following sequence of numbers-
10,12,5,4,20,8,7,15 and 13
Solution
How many distinct binary search trees can be constructed out of 4 distinct keys?
Solution
Number of distinct binary search trees possible with 4 distinct keys
= 2nCn / n+1
= 2×4C4 / 4+1
= 8C4 / 5
= 14
No comments:
Post a Comment