public static void main(String[] args) {
    AVLTree t = new AVLTree();
    long c = System.currentTimeMillis();
    for (int x = 0; x < 1000000; x++) {
      int ran = (int) (Math.random() * (1 << 30)) + 5;
      t.add(ran);
    }
    // t.traverse(root);
    t.add(1);
    System.out.println(t.contains(t.root, 1));
    System.out.println(t.contains(t.root, 2));
    t.remove(1);
    System.out.println(t.contains(t.root, 1));
    System.out.println(System.currentTimeMillis() - c);
    // t.add(9);
    // t.add(5);
    // t.add(10);
    // t.add(0);
    // t.add(6);
    // t.add(11);
    // t.add(-1);
    // t.add(1);
    // t.add(2);
    // traverse(root);
    // t.remove(10);
    // System.out.println();
    // t.traverse(root);

  }
Exemplo n.º 2
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());
  }
Exemplo n.º 3
0
  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);
  }