import java.util.Scanner;

/**
 * A program to test the BinarySearchTree class.
 *
 * @author Mark Young (A00000000)
 */
public class MakeBST {

    public static void main(String[] args) {
        // create and populate the BST
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        insertAll(bst, new int[]{31, 8, 12, 99, 18, 4, 15, 55, 42, 11, 99});
        System.out.println(bst);
        pause();
        
        // check the BST for some values
        for (int i = 1; i <= 10; ++i) {
            System.out.println("BST contains " + i + "? "
                + bst.contains(i));
        }
        pause();

        System.out.println("\n\nDeleting 11 (no children)");
        bst.delete(11);
        System.out.println(bst);
        pause();
        System.out.println("\n\nDeleting 55 (one child)");
        bst.delete(55);
        System.out.println(bst);
        pause();
        System.out.println("\n\nDeleting 31 (two children -- promote a leaf)");
        bst.delete(31);
        System.out.println(bst);
        pause();
        System.out.println("\n\nDeleting 8 (two children "
                + "-- promote node with child)");
        bst.delete(8);
        System.out.println(bst);
        pause();
        System.out.println("\n\nDeleting 77 (not there)");
        bst.delete(77);
        System.out.println(bst);
        pause();
    }

    /**
     * Inserts every integer from the given array into the given BST.
     *
     * @param bst the BST to insert into.
     * @param arr the numbers to insert into the BST.
     */
    private static void insertAll(BinarySearchTree<Integer> bst, int[] arr) {
        for (int num : arr) {
            if (bst.insert(num)) {
                System.out.println("Inserted " + num);
            } else {
                System.out.println("Failed to insert " + num);
            }
        }
    }
    
    private static final Scanner KBD = new Scanner(System.in);

    /**
     * Wait for the user to press the enter key. Includes a prompt and before-
     * after blank lines.
     */
    private static void pause() {
        System.out.println();
        System.out.print("Press enter...");
        KBD.nextLine();
        System.out.println();
    }

}
