Esempio n. 1
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    LinkedTree tree = new LinkedTree();
    tree.insert(7, "root node");
    tree.insert(9, "7's right child");
    tree.insert(5, "7's left child");
    tree.insert(2, "5's left child");
    tree.insert(8, "9's left child");
    tree.insert(6, "5's right child");
    tree.insert(4, "2's right child");

    System.out.print("\n preorder: ");
    tree.preorderPrint();
    System.out.println();

    System.out.print("postorder: ");
    tree.postorderPrint();
    System.out.println();

    System.out.print("  inorder: ");
    tree.inorderPrint();
    System.out.println();

    System.out.print("\nkey to search for: ");
    int key = in.nextInt();
    in.nextLine();
    LLList data = tree.search(key);
    if (data != null) System.out.println(key + " = " + data.getItem(0));
    else System.out.println("no such key in tree");

    System.out.print("\nkey to delete: ");
    key = in.nextInt();
    in.nextLine();
    data = tree.delete(key);
    if (data != null) System.out.println("removed " + data);
    else System.out.println("no such key in tree");

    System.out.print("\n preorder: ");
    tree.preorderPrint();
    System.out.println();

    System.out.print("postorder: ");
    tree.postorderPrint();
    System.out.println();

    System.out.print("  inorder: ");
    tree.inorderPrint();
    System.out.println();
  }