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;
    }
Example #2
0
  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");
  }
 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();
 }