Fall 2008

HW5: Recursion and Binary Search Trees

- Add the following methods to the BST class:
/** Returns the number of nodes stored in the tree */ public static > int numNodes(TreeNode current) /** Returns the weight of the tree (i.e., sum of all node depths) */ public static > int weight(TreeNode current) /** Returns the average cost of a search in the tree (i.e., weight/size) */ public static > double avgCost(TreeNode current) /** Returns whether the tree has the BST property, i.e., for every node, * values in left (resp., right) subtree are <= (resp., >) value at node */ public static > boolean verifyBST(TreeNode current) -
It has been proven that the
*average*search cost for an arbitrary item in a binary search tree of N random items is O(log N). You are to verify this result experimentally. Write a program that prompts the user for the number of items to be stored (N) and the number of trees to generate (T). Then, it should repeatedly (T times) store N random numbers in a binary search tree and compute the average cost of searching that tree.Your program should display the average of these costs over all of the constructed trees. In addition, it should display the average height of the trees. For example,

Number of values to be stored: 1000 Number of trees to generate: 100 Generating 100 trees with 1000 random values: average cost = 11.9146 average height = 21.76 - Run your program from part 2 for various values of N, using T = 1,000. Report
the average height and cost of searching the trees you constructed. Do your statistics support the
claims that the average height and cost of searching a randomly constructed binary
search tree are both O(log N)? Justify your answer.
number of values (N) ⌈log _{2}(N+1)⌉average cost average height N = 1,000 N = 2,000 N = 4,000 N = 8,000