In 21-25, use the steps of Algorithm 10.5.1 to build binary search trees. Use numerical order in 21 and alphabetical order in 22—25. In parts (a) and (b) of 21 and 22, the elements in the lists are the same, but the trees are different because the lists are ordered differently.
21. a. 16, 24, 21, 3, 18, 9, 7
b. 16, 7, 3, 21, 18, 24, 9
(a)
The construction of the binary search tree.
Given information:
Keys for a binary search tree is given as,
Calculation:
According to the algorithm given in
Before adding a new key search it’s correct position by going left if it is lesser than the current node else go right.
Once we are at the correct position add the key to its parent node.
Above is the binary search tree which is build using the given algorithm with the given keys
(b)
The construction of the binary search tree.