コード例 #1
0
  private static BalancedTree<Integer, Integer> createRedBlackTreeAsComplete() {
    Comparator<Integer> comp =
        new Comparator<Integer>() {

          public int compare(Integer o1, Integer o2) {
            return o1 - o2;
          }
        };
    BalancedTree<Integer, Integer> bt = new BalancedTree<Integer, Integer>(comp);

    // for perfect balanced tree, we must create FULL tree which is greater
    // than n (this is 2^20).
    int k = (int) (Math.log(n) / Math.log(2));
    int b = (int) Math.pow(2, k);
    int i = 0;
    while (b > 0) {
      bt.insert(b, b);

      for (int j = 1; j <= Math.pow(2, i) - 1; j++) {
        bt.insert(b + 2 * b * j, b + 2 * b * j);
      }

      b = b / 2;
      i++;
    }

    return bt;
  }
コード例 #2
0
  private static BalancedTree<Integer, Integer> createRedBlackTree() {
    Comparator<Integer> comp =
        new Comparator<Integer>() {

          public int compare(Integer o1, Integer o2) {
            return o1 - o2;
          }
        };
    BalancedTree<Integer, Integer> bt = new BalancedTree<Integer, Integer>(comp);

    for (int i = 1; i <= n; i++) {
      bt.insert(i, i);
    }

    return bt;
  }