public void testInsert() {
    System.out.println("***************** Insert operation *****************\n");

    avlTree.insert(1, "1");
    avlTree.insert(7, "7");
    avlTree.insert(5, "5");
    avlTree.insert(11, "11");
    avlTree.insert(9, "9");
    avlTree.insert(3, "3");
    avlTree.insert(2, "2");
    avlTree.insert(10, "10");
    avlTree.insert(6, "6");
    avlTree.insert(8, "8");
    avlTree.insert(12, "12");
    avlTree.insert(4, "4");

    System.out.println("Result of preorder traversal after performing insert operation");
    System.out.println("Correct output: 5 2 1 3 4 9 7 6 8 11 10 12 ");
    System.out.print("Result of yours: ");

    String result = "";
    Iterator itr = avlTree.positions().iterator();
    while (itr.hasNext()) {
      AVLNode nextNode = (AVLNode) itr.next();
      if (nextNode.element() != null) {
        System.out.print(((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
        result = result + (((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
      }
    }
    System.out.println();
    System.out.println();
    assertEquals("5 2 1 3 4 9 7 6 8 11 10 12 ", result);
  }
 public static void main(String[] args) {
   AVLTree trees = new AVLTree();
   trees.insert(9);
   trees.insert(8);
   trees.insert(7);
   trees.insert(6);
   trees.insert(5);
   trees.insert(4);
   trees.insert(3);
   trees.insert(2);
   // trees.insert(1);
   trees.printTree();
   trees.inorderPrinting();
 }
Example #3
0
  public static void AVLTest(int iterations) {
    AVLTree<Integer> avl = new AVLTree<>();

    Random rand = new Random();

    for (int i = 0; i < iterations; i++) avl.insert(rand.nextInt(Integer.MAX_VALUE));

    while (!avl.isEmpty()) avl.remove(avl.getRoot().getData());
  }
    public Object clone() {
      AVLTree<E> tree = new AVLTree<E>();

      LinkedList<AVLTreeNode<E>> queue = new LinkedList<AVLTreeNode<E>>();

      if (root == null) return tree;

      queue.add((AVLTreeNode<E>) root);

      while (queue.size() > 0) {
        AVLTreeNode<E> node = queue.remove(0);
        tree.insert(node.element);

        if (node.left != null) queue.add((AVLTreeNode<E>) (node.left));

        if (node.right != null) queue.add((AVLTreeNode<E>) (node.right));
      }
      return tree;
    }
  public void testRemoveEntryOfKV() {
    System.out.println("***************** Delete operation *****************\n");
    avlTree.insert(33, "33");
    avlTree.insert(24, "24");
    avlTree.insert(27, "27");
    avlTree.insert(32, "32");
    avlTree.insert(35, "35");
    avlTree.insert(29, "29");
    avlTree.insert(23, "23");
    avlTree.insert(21, "21");
    avlTree.insert(30, "30");
    avlTree.insert(25, "25");
    avlTree.insert(28, "28");
    avlTree.insert(22, "22");
    avlTree.insert(31, "31");
    avlTree.insert(26, "26");
    avlTree.insert(34, "34");

    avlTree.remove(avlTree.find(21));
    avlTree.remove(avlTree.find(22));
    // test 1) restructuring after removal of node
    System.out.println("Test 1) Result of preorder traversal after performing delete operation");
    System.out.println("Correct output: 27 25 23 24 26 32 29 28 30 31 34 33 35 ");
    System.out.print("Result of yours: ");

    String result = "";
    Iterator itr = avlTree.positions().iterator();
    while (itr.hasNext()) {
      AVLNode nextNode = (AVLNode) itr.next();
      if (nextNode.element() != null) {
        System.out.print(((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
        result = result + (((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
      }
    }
    System.out.println();
    System.out.println();

    avlTree.remove(avlTree.find(26));
    // test 2) when imbalance propagate upward and additional restructuring is needed
    System.out.println(
        "Test 2) Result of preorder traversal after performing delete operation - Propagated");
    System.out.println("Correct output: 29 27 24 23 25 28 32 30 31 34 33 35 ");
    System.out.print("Result of yours: ");

    result = "";
    itr = avlTree.positions().iterator();
    while (itr.hasNext()) {
      AVLNode nextNode = (AVLNode) itr.next();
      if (nextNode.element() != null) {
        System.out.print(((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
        result = result + (((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
      }
    }
    System.out.println();
    System.out.println();

    avlTree.remove(avlTree.find(34));
    avlTree.remove(avlTree.find(35));
    avlTree.remove(avlTree.find(33));

    // Test 3) Final result of tree after deletion of several nodes
    System.out.println(
        "Test 3) Final result of preorder traversal traversal after deletion of several nodes");
    System.out.println("Correct output: 29 27 24 23 25 28 31 30 32 ");
    System.out.print("Result of yours: ");

    result = "";
    itr = avlTree.positions().iterator();
    while (itr.hasNext()) {
      AVLNode nextNode = (AVLNode) itr.next();
      if (nextNode.element() != null) {
        System.out.print(((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
        result = result + (((Entry<Integer, String>) (nextNode.element())).getValue() + " ");
      }
    }
    System.out.println();
    assertEquals("29 27 24 23 25 28 31 30 32 ", result);
  }