Ejemplo n.º 1
0
 private void keys(Node x, Queue<Key> queue, Key lo, Key hi) {
   if (x == null) return;
   int compLo = lo.compareTo(x.key);
   int compHi = hi.compareTo(x.key);
   if (compLo < 0) keys(x.left, queue, lo, hi);
   if (compLo <= 0 && compHi >= 0) queue.enqueue(x.key);
   if (compHi > 0) keys(x.right, queue, lo, hi);
 }
Ejemplo n.º 2
0
 private Node delete(Node x, Key key) {
   if (x == null) return null;
   int comp = key.compareTo(x.key);
   if (comp < 0) x.left = delete(x.left, key);
   else if (comp > 0) x.right = delete(x.right, key);
   else {
     if (x.left == null) {
       if (x.pred != null) x.pred.succ = x.succ;
       if (x.succ != null) x.succ.pred = x.pred;
       return x.right;
     }
     if (x.right == null) {
       if (x.pred != null) x.pred.succ = x.succ;
       if (x.succ != null) x.succ.pred = x.pred;
       return x.left;
     }
     Node t = max(x.left);
     t.left = deleteMaxWithOutDeleteThread(x.left);
     t.right = x.right;
     if (x.pred != null) x.pred.succ = x.succ;
     if (x.succ != null) x.succ.pred = x.pred;
     x = t;
   }
   x.N = size(x.left) + size(x.right) + 1;
   x.height = Math.max(height(x.left), height(x.right)) + 1;
   x.avgCompares =
       (avgCompares(x.left) * size(x.left) + avgCompares(x.right) * size(x.right) + size(x))
           / size(x);
   return x;
 }
Ejemplo n.º 3
0
 private int rank(Node x, Key key) {
   if (x == null) return 0;
   int comp = key.compareTo(x.key);
   if (comp == 0) return size(x.left);
   else if (comp < 0) return rank(x.left, key);
   else return size(x.left) + 1 + rank(x.right, key);
 }
Ejemplo n.º 4
0
 private Node put(Node x, Key key, Value val) {
   if (x == null) {
     Node pre = floor(root, key);
     Node nex = ceiling(root, key);
     lastest = new Node(key, val, 1, 1, 1);
     if (pre != null) pre.succ = lastest;
     if (nex != null) nex.pred = lastest;
     lastest.succ = nex;
     lastest.pred = pre;
     return lastest;
   }
   int comp = key.compareTo(x.key);
   if (comp < 0) x.left = put(x.left, key, val);
   else if (comp > 0) x.right = put(x.right, key, val);
   else {
     lastest = x;
     x.value = val;
   }
   x.N = size(x.right) + size(x.left) + 1;
   x.height = Math.max(height(x.left), height(x.right)) + 1;
   x.avgCompares =
       (avgCompares(x.left) * size(x.left) + avgCompares(x.right) * size(x.right) + size(x))
           / size(x);
   return x;
 }
Ejemplo n.º 5
0
 /*   private Node get(Node x, Key key) //recursive implementation
 {
     if(x == null)
         return null;
     int com = key.compareTo(x.key);
     if(com < 0) return get(x.left, key);
     else if(com > 0) return get(x.right, key);
     else return x;
 }*/
 private Node get(Node x, Key key) {
   while (x != null) {
     int comp = key.compareTo(x.key);
     if (comp < 0) x = x.left;
     else if (comp > 0) x = x.right;
     else return x;
   }
   return null;
 }
Ejemplo n.º 6
0
 private Node floor(Node x, Key key) {
   if (x == null) return null;
   int comp = key.compareTo(x.key);
   if (comp < 0) return floor(x.left, key);
   else if (comp > 0) {
     Node res = floor(x.right, key);
     if (res != null) return res;
     else return x;
   } else return x;
 }
Ejemplo n.º 7
0
 private Node ceiling(Node x, Key key) {
   if (x == null) return null;
   int comp = key.compareTo(x.key);
   if (comp > 0) return ceiling(x.right, key);
   else if (comp == 0) return x;
   else {
     Node res = ceiling(x.left, key);
     if (res == null) return x;
     else return res;
   }
 }
Ejemplo n.º 8
0
 public boolean checkSelectRank() {
   for (Key k : keys()) {
     if (k.compareTo(select(rank(k))) != 0) return false;
   }
   return true;
 }