Assignment 2

CS 146
due March 12, 2002
100 points

Define classes BST, AVL, and AVL2 to implement binary search trees with methods insert, search, inorder, and levelOrder. The last two of these should print the data elements to System.out. The search method should simply return a boolean value telling whether the item was found. Both insert and search should take Comparable arguments. The only constructors you should need are zero-argument constructors that build an empty tree.

The three classes should also maintain counters of the number of assignments made to tree links and the number of accesses to tree links (excluding assignments to tree links), and a method printCounters to print these two values to standard outupt and resetCounters to reset them both to zero. You needn't count assignments or references to other node references. So for example, Weiss's RotateWithLeftChild method makes 2 assignments to tree links, and accesses 5 additional tree links (3 in the computation of the heights).

The differences between the three classes are that the BST class is to implement an ordinary binary search tree (without rotations), the AVL class is to implement an AVL tree with the double rotations defined in terms of the single rotations (as in the Weiss text), and the AVL2 class is to have the double rotations defined directly -- not in terms of the single rotations.

You may use inheritance as appropriate to reduce the repetition in the definitions of the three classes. You are not required to do so. In each case, your node class should be properly encapsulated. In particular, you shouldn't use the default protection (as is done in the text) unless you use an explicit package.

You may use the definitions of the text (or of the associated web site) provided that you give proper credit.

As always, you may define additional classes or methods if you find them helpful.

You should show the results of testing with the test file A2.java, which is provided on the class web site. You needn't turn in this file unless you change it.