Variants of Trees
In the previous exercises, we've mainly looked at the binary tree, which is one of the most common kinds of trees. In a binary tree, each node can have two child nodes at most. However, a plain binary tree doesn't always serve this purpose. Next, we'll look at a more specialized version of the binary tree, called a binary search tree.
Binary Search Tree
A binary search tree (BST) is a popular version of the binary tree. BST is nothing but a binary tree with the following properties:
- Value of the parent node ≥ value of the left child
- Value of the parent node ≤ value of the right child
In short, left child ≤ parent ≤ right child.
This leads us to an interesting feature. At any point in time, we can always say that all the elements that are less than or equal to the parent node will be on the left side, while those greater than or equal to the parent node will be on the right side. So, the problem of searching an element keeps...