The main goals for this lab are:
In this assignment you will write a AVLTree (AVLTree.java) that extends your LinkedBinaryTree from HW05.
Your AVLTree should be generic over type E.
First, copy over your LinkedBinaryTree.java and the BinaryTree.java interface from HW05.
Then, modify the Node class in LinkedBinaryTree to have public access.
Add a parent reference and a height instance variable to the Node class of
LinkedBinaryTree. Also add public methods Node<E> getParent(), int getHeight(), E getData(), Node<E> getLeft(), and Node<E> getRight() for testing.
Inheritance of inner/nested classes of Java could be weird so it’s easier to just make changes
to the Node in LinkedBinaryTree instead.
getHeight() should return the height of the subtree rooted at that node. A leaf node should have a height of 1.
Modify/override LinkedBinaryTree and/or AVLTree so that parent and height
are set correctly on insertion and deletion.
You might need additional helper methods (to compute
height, for example).
Add the following methods / constructors to AVLTree.java
public Node getRoot()
public AVLTree()
public AVLTree(E data)
Override toString of the Node class to print the element followed by its
height in parenthesis.
Modify/Override toStringInOrder so that it uses the above toString of Node
and returns a traversal string listing the elements with height attached.
Implement public Node rebalance(Node n) with associated helpers (public Node rotateLeft(Node n), public Node rotateRight(Node n), public Node rotateLeftRight(Node n),
public Node rotateRightLeft(Node n)) and call appropriately on insertion. rotateLeftRight and rotateRightLeft should take the root that we want to perform the second rotation over.
Create an AVLTree<String> and insert the exercise example given in class, i.e. “M”, “N”,
“O”, “L”, “K”, “Q”, “P”, “H”, “I”, “A”. Draw the expected tree by hand and assert that your code does indeed create the expected tree.
Override toString of AVLTree to print out the in-order traversal, with the height of each
node attached. For example, the tree above should return the following string:
A(1) H(2) I(3) K(1) L(2) M(1) N(4) O(1) P(2) Q(1)
In this lab we covered AVL Trees.
This lab will be autograded. Submit the following files to gradescope:
LinkedBinaryTree.javaAVLTree.java