Source of AVLTreeDemo.java


  1: //AVLTreeDemo.java

  3: public class AVLTreeDemo
  4: {
  5:     public static void main(String[] args)
  6:     {
  7:         // Create an empty AVLTree object.
  8:         AVLTree tree = new AVLTree();

 10:         // Insert some random-looking integers into the tree.
 11:         int[] keys = { 10, 20, 5, 22, 15, 47, 19, 3, 12, 18 };
 12:         for (int key : keys)
 13:         {
 14:             Node node = new Node(key);
 15:             tree.insert(node);
 16:         }

 18:         // Print the tree after all inserts are complete.
 19:         System.out.println("Tree after initial insertions:");
 20:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 21:         System.out.println();

 23:         // Find and remove the node with the key value 12.
 24:         // This should cause a right rotation on node 10.
 25:         System.out.println("Remove node 12:");
 26:         tree.removeKey(12);
 27:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 28:         System.out.println();

 30:         // Find and remove the node with the key value 20.
 31:         // This should cause its right child to shift up into
 32:         // the 20 node's position without any other reordering
 33:         // required.
 34:         System.out.println("Remove node 20:");
 35:         tree.removeKey(20);
 36:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 37:         System.out.println();

 39:         // Attempt to remove a node with key 30, a value not in the tree.
 40:         System.out.println("Remove node 30 (should not be in tree):");
 41:         if (!tree.removeKey(30))
 42:         {
 43:             System.out.println("*** Key not found. Tree is not changed. ***");
 44:         }
 45:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 46:     }
 47: }