import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>();
    a.add(new Student("Leslie", 6)); 
    a.add(new Student("Laura" , 6)); 
    a.add(new Student("Larry" , 6)); 
    a.add(new Student("Alex"  , 5)); 
    a.add(new Student("Chris" , 4)); 
    System.out.println( a ); 
    Collections.sort( a ); 
    System.out.println( a ); 
  }
}

--

public class Student implements Comparable<Student> {
  public int compareTo(Student other) {
    if (this.age < other.age) return -1; // I go first
    else if (this.age > other.age) return 1; // I go second 
    else return this.name.compareTo(other.name); 
  }
  private String name; 
  private int age; 
  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public String toString() {
    return this.name + ":" + this.age;  
  }
}

--

public class BST {
  private int value; 
  private BST left, right; 
  public BST(int value, BST left, BST right) {
    this.value = value; 
    this.left = left; 
    this.right = right; 
  }
  public static void main(String[] args) {
    BST one = new BST( 1, null, null ); 
    BST six = new BST( 6, new BST(4, null, null) ,
                          new BST(7, null, null) ); 
    BST three = new BST( 3, one, six );
    // System.out.println( three ); 
    BST eight = new BST( 8, three, 
                            new BST(10, null, 
                                        new BST( 14, new BST( 13, null, 
                                                                  null),
                                                     null)));
    System.out.println( eight.size() ) // 9; 
    System.out.println( eight.find(3) ); // 4 will print element index 2
  }
  public int find(int k) {
    if (k <= 0) throw new Exception(); 
    if (this.left != null && this.left.size() >= k) {
      // k < 1 + (this.left == null ? 0 : this.left.size()
      return this.left.find(k);  
    } else if (this.left == null && k == 1 || 
               this.left != null && k == 1 + this.left.size()) {
      // k == 1 + (this.left == null ? 0 : this.left.size())
      return this.value; 
    } else if (k > 1 + (this.left == null ? 0 : this.left.size()) {
      if (this.right == null) throw new Exception(); 
      else this.right.find( k - (1 + (this.left == null ? 0 : this.left.size()) );
    }
  }
  public int size() {
    return 1 + 
      (this.left == null ? 0 : this.left.size()) + 
      (this.right == null? 0 : this.right.size());  
  }
  public String toString() {
    // return this.value + " " + 
    //   ((this.left == null) ? "" : (this.left + " ")) + 
    //   ((this.right == null) ? "" :(this.right + " ")); 
       return 
        ((this.left == null) ? "" : (this.left + " ")) + 
         this.value + " " + 
        ((this.right == null) ? "" :(this.right + " ")); 
  }
}

--