import java.util.Random; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; class BinarySearchTree { public BinarySearchTree( ) { this( null ); } public BinarySearchTree( Comparator c ) { theSize = 0; root = null; cmp = c; } public boolean remove( AnyType x ) { int oldSize = size( ); root = remove( x, root ); return size( ) != oldSize; } private Node remove( AnyType x, Node t ) { if( t == null ) return null; int compareResult = myCompare( x, t.data ); if( compareResult < 0 ) t.left = remove( x, t.left ); else if( compareResult > 0 ) t.right = remove( x, t.right ); else { // this is the node if( t.left != null && t.right != null ) // 2 children { t.data = findMin( t.right ); t.right = removeMin( t.right ); } else { theSize--; return t.left == null ? t.right : t.left; } } return t; } private AnyType findMin( Node t ) { if( t == null ) throw new NullPointerException( ); if( t.left == null ) return t.data; else return findMin( t.left ); } private Node removeMin( Node t ) { if( t == null ) throw new NullPointerException( ); if( t.left == null ) { theSize--; return t.right; } else { t.left = removeMin( t.left ); return t; } } public boolean add( AnyType x ) { int oldSize = size( ); root = add( x, root ); return size( ) != oldSize; } private Node add( AnyType x, Node t ) { if( t == null ) { theSize++; return new Node( x, null, null ); } int compareResult = myCompare( x, t.data ); if( compareResult < 0 ) t.left = add( x, t.left ); else if( compareResult > 0 ) t.right = add( x, t.right ); return t; } // Public driver public boolean contains( AnyType x ) { return contains( x, root ); } // Recursive private boolean contains( AnyType x, Node t ) { if( t == null ) return false; int compareResult = myCompare( x, t.data ); if( compareResult < 0 ) return contains( x, t.left ); else if( compareResult > 0 ) return contains( x, t.right ); else // compareResult == 0 return true; } public void println( ) { System.out.print( "[ " ); print( root ); System.out.println( "]" ); } private void print( Node t ) { if( t != null ) { print( t.left ); print( t.right ); System.out.print( t.data + " " ); } } public int size1( ) { return size1( root ); } private int size1( Node t ) { if( t == null ) return 0; else return size1( t.left ) + size1( t.right ) + 1; } public int height( ) { return height( root ); } private int height( Node t ) { if( t == null ) return -1; else return Math.max( height( t.left ), height( t.right ) ) + 1; } public int size( ) { return theSize; } private int myCompare( AnyType lhs, AnyType rhs ) { if( cmp != null ) return cmp.compare( lhs, rhs ); else return ((Comparable)lhs).compareTo( rhs ); } private static class Node { Node( AnyType d, Node lt, Node rt ) { data = d; left = lt; right = rt; } AnyType data; Node left; Node right; } private int theSize; private Node root; private Comparator cmp; } public class Day13 { public static void main ( String [ ] args ) { int N = 300000; Random r = new Random( ); BinarySearchTree bs1 = new BinarySearchTree( ); Set s = new TreeSet( ); for( int i = 0; i < N; i++ ) { int val = r.nextInt( ); bs1.add( val ); s.add( val ); } System.out.println( bs1.size( ) ); System.out.println( s.size( ) ); System.out.println( bs1.height( ) ); } }