Пример #1
0
  public static void main(String[] args) {

    /** array to be inserted in BSTSort and values that will be inserted in the BinarySearchTree */
    int[] values = {
      2, 5, 33, 532, 543, 63, 451, 61, 4, 6, 22, 46, 1, 4, 12, 41, 456, 23, 51, 35, 6, 12
    };

    /** BSTSorted tree */
    System.out.println("BST Ordered Tree: ");
    BSTSort(values);

    System.out.println(" ");

    /** Empty BinarySearchTree */
    BinarySearchTree tree = new BinarySearchTree();

    /**
     * iterates through the array and inserts a value from the array into the newly formed
     * BinarySearchTree
     */
    for (int i = 0; i < values.length; i++) {
      tree.insert(new BSTNode(values[i]));
    }

    /** PostOrder Sorted Tree */
    System.out.println("Post Order: ");
    tree.postOrder();

    System.out.println("");

    /** PreOrder Sorted Tree */
    System.out.println("Pre Order: ");
    tree.preOrder();
    System.out.println("");

    /** Size of Tree after values have been inserted */
    System.out.println("Current Size: ");
    System.out.println(tree.getSize());

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value1 = tree.search(5);
    if (value1 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value1.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value2 = tree.search(33);
    if (value2 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value2.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value3 = tree.search(451);
    if (value3 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value3.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value4 = tree.search(6);
    if (value4 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value4.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value5 = tree.search(1);
    if (value5 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value5.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value6 = tree.search(333);
    if (value6 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value6.getKey());
    }
    BSTNode value7 = tree.search(342334);
    if (value7 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value7.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value8 = tree.search(43323);
    if (value8 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value8.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value9 = tree.search(89687);
    if (value9 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value9.getKey());
    }

    /**
     * Search for values in the tree. If a value is not found, a message will be printed to notify
     * the user.
     */
    BSTNode value10 = tree.search(10000);
    if (value10 == null) {
      System.out.println("Not found.");
    } else {
      System.out.println("Found it " + value10.getKey());
    }

    /** Prints the values of the tree from smallest to largest */
    System.out.println("Ascending Order: ");
    BSTNode current = tree.minimum();
    for (int i = 0; i < tree.getSize(); i++) {
      System.out.print(" " + current.toString());
      current = tree.successor(current);
    }

    System.out.println(" ");

    /** Prints the values of the tree from largest to smallest */
    System.out.println("Descending Order: ");
    BSTNode current1 = tree.maximum();
    for (int i = 0; i < tree.getSize(); i++) {
      System.out.print(" " + current1.toString());
      current1 = tree.predecessor(current1);
    }

    System.out.println(" ");

    /** Deleting a few values and nodes from the tree */
    tree.delete(tree.search(61));
    tree.delete(tree.search(6));
    tree.delete(tree.search(46));

    /** Returns size of the altered tree */
    System.out.println("Current Size: ");
    System.out.println(tree.getSize());

    /** Rearranges the altered tree using inOrder travels */
    System.out.println("In Order: ");
    System.out.print(" ");
    tree.inOrder();

    /** testing the BSTSort with trees of different sizes */
    int[] values2 = {4, 56, 7, 1, 3, 7};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values2);

    /** testing the BSTSort with trees of different sizes */
    int[] values3 = {33, 2, 1};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values3);

    /** testing the BSTSort with trees of different sizes */
    int[] values4 = {3, 4, 5, 3, 3, 522, 454, 621};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values4);

    /** testing the BSTSort with trees of different sizes */
    int[] values5 = {3, 231, 5, 3, 3, 5443322, 434, 621, 6, 78, 9, 54345, 64};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values5);

    /** testing the BSTSort with trees of different sizes */
    int[] values6 = {5, 4, 3, 2, 1, 0, -1, -2, -3};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values6);

    /** testing the BSTSort with trees of different sizes */
    int[] values7 = {5};
    System.out.println(" ");
    System.out.println("BST Ordered Tree: ");
    BSTSort(values7);
  }