import javax.swing.JOptionPane;
import java.util.Iterator;
import java.util.Comparator;
import java.util.Random;

public class TestBinarySearchTree {
    
    public static void main(String[] args) {
        BinarySearchTree<Integer> aTree = new BinarySearchTree<Integer>();
        aTree.add(37);
        Random r = new Random();
        for(int i = 25; i >= 0 ; i--) aTree.add(r.nextInt(100));
        JOptionPane.showMessageDialog(null,"Tree = " + aTree+"\nSize = "+aTree.size(),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        aTree.remove(37);
        JOptionPane.showMessageDialog(null,"Tree after removing 37 = "+aTree+"\nSize = "+aTree.size()+ "\ncontains(37)? " + aTree.contains(37),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        aTree.removeMin();
        JOptionPane.showMessageDialog(null,"Tree after removing the min = "+aTree+"\nSize = "+aTree.size(),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        aTree.removeMin();
        JOptionPane.showMessageDialog(null,"Tree after removing the min = "+aTree+"\nSize = "+aTree.size(),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        aTree.add(new Integer(37));
        JOptionPane.showMessageDialog(null,"Tree after adding 37 = "+aTree +"\nSize = "+aTree.size() + "\ncontains(37)? " + aTree.contains(37),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        Iterator<Integer> k = aTree.iterator();
        String out = "Using iterator Tree = ";
        while( k.hasNext() ) {
            out += k.next() + " ";
        } // end while
        out += "\n";
        out += "Using for each Tree = ";
        for( int n : aTree ) out += n + " ";
        out += "\n";
        JOptionPane.showMessageDialog(null,out,"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        out = "";
        for(int i = 100; i >= 0 ; i--) {
            int n = r.nextInt(100);
            if( aTree.remove(n) ) {
                out += "Tree after removing " + n + " = " + aTree + "\n";
            } // end if
        }
        JOptionPane.showMessageDialog(null,out+"\nSize = "+aTree.size(),"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        out = "";
        for( int n : aTree ) {
            aTree.remove(n);
            out += "Tree = " + aTree + "\n";
        }// end for
        out += "Tree has "+ aTree.size() + " elements.\n";
        JOptionPane.showMessageDialog(null,out,"Tree Output",JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }
    
    public static class DescendingComparator<T extends Comparable<T>> implements Comparator<T> {
        public int compare(T left, T right) {
            return -left.compareTo(right);
        }
    }
    
}

