Homework 5
AVL Trees (Part 1)
Preliminaries

Download the template class and the driver

The template class has some useful functions for printing out your tree, so you should not modify these.

As usual, do not modify the driver.

Description

As we will discuss in class, binary search trees are not optimal structures. In the worst case, a degenerate BST degrades to a linked list. Here we will be taking the first steps to implementing a self-balancing BST.

Objective

Create a class AvlTree. You AVL tree class should be a generic class containing Comparable items. In addition, AvlTree should have an internal class private AvlNode, also a generic container of Comparable items.

The internal AvlNode should have the following:

AvlTree should have the following:

Hint

Here is a diagram of left/right rotations (taken from Wikipedia):

Tree rotations

In the diagram, a right rotation is performed with Q as the root, while a left rotation is performed with P as the root.

The image was taken from this article, which you may find useful.

If you have trouble debugging your code, you may use the method "printAscii" included within the template. This will print an ASCII drawing of your tree.

Example Dialog

      Lookup test succeeded.
      ----------------
      Rotation test:
         Jose
          |- Louie
          |  |- Nathan
          |  `- Kiley
          `- Jaki
             `- Beverly
      Left rotation around 'Jose'
         Louie
          |- Nathan
          `- Jose
             |- Kiley
             `- Jaki
                `- Beverly
      Right rotation around 'Jose'
         Louie
          |- Nathan
          `- Jaki
             |- Jose
             |  `- Kiley
             `- Beverly
      Right rotation around 'Louie'
         Jaki
          |- Louie
          |  |- Nathan
          |  `- Jose
          |     `- Kiley
          `- Beverly
      ----------------
      Randomized rotation test:
      Random tree:
         0
          `- 2
             |- 4
             |  `- 3
             `- 1
      Left rotation around 2
         0
          `- 4
             `- 2
                |- 3
                `- 1
      Right rotation around 1
         0
          `- 4
             `- 2
                |- 3
                `- 1
      Right rotation around 2
         0
          `- 4
             `- 1
                `- 2
                   `- 3
      ----------------
      
Finally:

Upload the .java file to the dropbox