These are the solutions to the exercises found in the section, ​ Exercises ​ . The solutions provided here are in Python, but you can find the solutions in JavaScript and Ruby in the code download. [15]

  1. The tree should look like this. Note that it’s not well balanced, as the root node only has a right subtree and no left one:

    images/binary_trees/solution_1.png

  2. Search within a balanced binary search tree takes a maximum of about log(N) steps. So, if N is 1,000, search should take a maximum of about 10 steps.

  3. The greatest value within a binary search tree will always be the bottom rightmost node. We can find it by recursively following each node’s right child until we hit the bottom:

    <table><tbody><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>def</strong> ​ ​ <strong>max</strong> ​(node):</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>if</strong> ​ node.rightChild:</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ max(node.rightChild)</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>else</strong> ​:</td></tr><tr><td>​ <span>&nbsp;</span></td><td>​ <strong>return</strong> ​ node.value</td></tr></tbody></table>

  4. Here is the order for preorder traversal:

    images/binary_trees/solution_4.png

  5. Here is the order for postorder traversal:

    images/binary_trees/solution_5.png