Lecture Videos
  1  public class TreePerformanceTest {
  2    public static void main(String[] args) {
  3      final int TEST_SIZE = 500000; // Tree size used in the test
  4  
  5      // Create an AVL tree
  6      Tree<Integer> tree1 = new AVLTree<Integer>();
  7      System.out.println("AVL tree time: " +
  8        getTime(tree1, TEST_SIZE) + " milliseconds");
  9  
 10      // Create a 2-4 tree
 11      Tree<Integer> tree2 = new Tree24<Integer>();
 12      System.out.println("2-4 tree time: "
 13        + getTime(tree2, TEST_SIZE) + " milliseconds");
 14  
 15      // Create a red-black tree
 16      Tree<Integer> tree3 = new RBTree<Integer>();
 17      System.out.println("RB tree time: "
 18        + getTime(tree3, TEST_SIZE) + " milliseconds");
 19    }
 20  
 21    public static long getTime(Tree<Integer> tree, int testSize) {
 22      long startTime = System.currentTimeMillis(); // Start time
 23  
 24      // Create a list to store distinct integers
 25      java.util.List<Integer> list = new java.util.ArrayList<Integer>();
 26      for (int i = 0; i < testSize; i++)
 27        list.add(i);
 28  
 29      java.util.Collections.shuffle(list); // Shuffle the list
 30  
 31      // Insert elements in the list to the tree
 32      for (int i = 0; i < testSize; i++)
 33        tree.insert(list.get(i));
 34  
 35      java.util.Collections.shuffle(list); // Shuffle the list
 36  
 37      // Delete elements in the list from the tree
 38      for (int i = 0; i < testSize; i++)
 39        tree.delete(list.get(i));
 40  
 41      // Return elapse time
 42      return System.currentTimeMillis() - startTime;
 43    }
 44  }