(Last Revision: 12/05/96) A D D E N D U M T O A S S I G N M E N T # 5 Here are some hints to help you design the various parts of your program. ================================================================= IsBST: ================================================================= You can tell if a binary tree is a binary search tree by traversing the tree with the inOrder traversal algorithm. If the keys of the tree are all encountered in order, then the tree is a BST, otherwise it is not. ================================================================= SimilarTrees: ================================================================= Two binary trees A and B are similar if both A and B are empty, or if both of the following are true: 1. The left subtree of the root of A is similar to the left subtree of the root of B, AND 2. The right subtree of the root of A is similar to the right subtree of the root of B. Now write a recursive function that checks to see if two binary trees are similar. ================================================================= Leaf Count: ================================================================= This indicates how you can design a recursive function LeafCount. Suppose T is a BST. If T is empty it has 0 leaves. If T is not empty, then if the root node of T has no children, then T has one leaf. Otherwise the number of leaves of T is the number of leaves in its left subtree, plus the number of leaves in its right subtree. ================================================================= CopyTree: ================================================================= To copy a tree, traverse the source tree in preOrder (recursively), and use InsertElement to insert a copy of each element of the source tree into a target tree. The target tree starts out as an empty tree, of course. If you like, try to think of a more efficient way of doing this. ================================================================= Mirror: ================================================================= Here is an idea for a recursive procedure: To make a mirror image M of a BST T, if T is nil then M is nil. Otherwise, make the root pointer of M point to a copy of the root node of T, then make a mirror image of the right subtree of the root node of T and hang it on the left of the root node of M, and make a mirror image of the left subtree of the root node of T, and hang it on the right of the root node of M. ================================================================= Ancestor: ================================================================= Here is an idea for a recursive procedure: To print the ancestors of node S from the root down, first check to see if the tree is nil. If not, then check to see if the root node is S. If not then print the value in the root node (assuming there is at least one node), then determine whether S is to the left or right of the root. If it is to the left, then print the ancestors of S in the left subtree, else print the ancestors of S in the right subtree. How can we print the ancestors starting from the parent of S, and working back to the root? ================================================================= =================================================================