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 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 static void main(String[] args) { AVLTree avl = new AVLTree(); Scanner scan = new Scanner(System.in); int start = 0; do { System.out.println("Add what? (-1 to quit)"); try { start = scan.nextInt(); scan.nextLine(); } catch (java.util.InputMismatchException ex) { System.out.println("Enter an integer next time please."); System.exit(0); } boolean ans = avl.add(start); PrintTree.printTree(avl); if (!ans) System.out.println(start + " not added"); } while (start != -1); System.out.println("Goodbye"); }
/** * Constructor for the AVLInsert class * * @param avl - AVLTree * @param value - int */ public AVLInsert(AVLTree avl, int value) { super(avl.M); this.M = avl.M; this.avlTree = avl; this.value = value; // create a new node this.v = new AVLNode(avl, value); this.v.bgColor(AVLNode.INSERT_COLOR); avl.avlNode = this.v; // set the header of the textArea setHeader("insertion"); }
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); }
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(); }
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); }