@Override
  public void put(Key key, Value value) {
    // search for key, update value if found, grow table if new

    Node t = new Node(null, key, value);
    if (root == null) {
      root = t;
      return;
    }
    Node parent = null;
    Node x = root;
    while (x != null) {
      parent = x;
      int cmp = key.compareTo(x.key);
      compares++;
      if (cmp < 0) x = x.left;
      else if (cmp > 0) x = x.right;
      else {
        x.value = value;
        return;
      }
    }
    int cmp = key.compareTo(parent.key);
    compares++;
    if (cmp < 0) parent.left = t;
    else parent.right = t;
    t.parent = parent;
    // adjust tree sizes;
    while (parent != null) {
      parent.N += 1;
      parent = parent.parent;
    }
  }
Esempio n. 2
0
File: BST.java Progetto: 9r3y/c47
 private void keys(Node x, Queue<Key> queue, Key lo, Key hi) {
   if (x == null) return;
   int cmplo = lo.compareTo(x.key);
   int cmphi = hi.compareTo(x.key);
   if (cmplo < 0) keys(x.left, queue, lo, hi);
   if (cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);
   if (cmphi > 0) keys(x.right, queue, lo, hi);
 }
 private void keys(Node node, Queue<Key> queue, Key lo, Key hi) {
   if (node == null) return;
   int cmplo = lo.compareTo(node.key);
   int cmphi = hi.compareTo(node.key);
   if (cmplo < 0) keys(node.left, queue, lo, hi);
   if (cmplo <= 0 && cmphi >= 0) queue.add(node.key);
   if (cmphi > 0) keys(node.right, queue, lo, hi);
 }
Esempio 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;
 }
Esempio n. 5
0
File: BST.java Progetto: 9r3y/c47
 private Value get(Node x, Key key) {
   if (x == null) return null;
   int cmp = key.compareTo(x.key);
   if (cmp < 0) return get(x.left, key);
   else if (cmp > 0) return get(x.right, key);
   else return x.val;
 }
Esempio n. 6
0
File: BST.java Progetto: 9r3y/c47
 // Number of keys in the subtree less than key.
 private int rank(Key key, Node x) {
   if (x == null) return 0;
   int cmp = key.compareTo(x.key);
   if (cmp < 0) return rank(key, x.left);
   else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right);
   else return size(x.left);
 }
Esempio n. 7
0
  @Override
  public void put(Key key, Value value) {
    if (N == capicity) resize(2 * capicity);

    if (N == 0 || key.compareTo(keys[N - 1]) > 0) {
      keys[N] = key;
      values[N] = value;
      cachedIndex = N;
      N++;
      return;
    }
    int k = rank(key);
    cachedIndex = k;
    if (k < N && keys[k].equals(key)) {
      values[k] = value;
    } else {
      for (int i = N - 1; i >= k; i--) {
        keys[i + 1] = keys[i];
        values[i + 1] = values[i];
      }
      keys[k] = key;
      values[k] = value;
      N++;
    }
  }
Esempio n. 8
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;
 }
Esempio n. 9
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);
 }
Esempio n. 10
0
 // the smallest key in the subtree rooted at x greater than or equal to the given key
 private Node ceiling(Node x, Key key) {
   if (x == null) return null;
   int cmp = key.compareTo(x.key);
   if (cmp == 0) return x;
   if (cmp > 0) return ceiling(x.right, key);
   Node t = ceiling(x.left, key);
   if (t != null) return t;
   else return x;
 }
Esempio n. 11
0
File: BST.java Progetto: 9r3y/c47
 private Node floor(Node x, Key key) {
   if (x == null) return null;
   int cmp = key.compareTo(x.key);
   if (cmp == 0) return x;
   if (cmp < 0) return floor(x.left, key);
   Node t = floor(x.right, key);
   if (t != null) return t;
   else return x;
 }
 private Node ceiling(Node node, Key key) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp == 0) return node;
   if (cmp > 0) return ceiling(node.right, key);
   Node t = ceiling(node.left, key);
   if (t != null) return t;
   else return node;
 }
Esempio n. 13
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;
 }
Esempio n. 14
0
File: BST.java Progetto: 9r3y/c47
 private Node put(Node x, Key key, Value val) {
   if (x == null) return new Node(key, val, 1);
   int cmp = key.compareTo(x.key);
   if (cmp < 0) x.left = put(x.left, key, val);
   else if (cmp > 0) x.right = put(x.right, key, val);
   else x.val = val;
   x.N = 1 + size(x.left) + size(x.right);
   return x;
 }
 private Node floor(Node node, Key key) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp == 0) return node;
   if (cmp < 0) return floor(node.left, key);
   Node t = floor(node.right, key);
   if (t != null) return t;
   else return node;
 }
Esempio n. 16
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;
 }
 public Iterable<Key> keys(Key lo, Key hi) {
   Queue<Key> queue = new Queue<Key>();
   if (lo == null && hi == null) return queue;
   if (lo == null) throw new NullPointerException("lo is null in keys()");
   if (hi == null) throw new NullPointerException("hi is null in keys()");
   if (lo.compareTo(hi) > 0) return queue;
   for (int i = rank(lo); i < rank(hi); i++) queue.enqueue(keys[i]);
   if (contains(hi)) queue.enqueue(keys[rank(hi)]);
   return queue;
 }
 private Node get(Node node, Key key) {
   Node t = node;
   while (t != null) {
     int cmp = key.compareTo(t.key);
     compares++;
     if (cmp == 0) return t;
     else if (cmp < 0) t = t.left;
     else if (cmp > 0) t = t.right;
   }
   return null;
 }
Esempio n. 19
0
 public Key max() {
   if (size == 0) {
     return null;
   } else {
     Key maximum = list.getFirst();
     for (Key p : list) {
       if (p.compareTo(maximum) == 1) maximum = p;
     }
     return maximum;
   }
 }
 // return the number of keys in the table that are smaller than given key
 public int rank(Key key) {
   int lo = 0, hi = N - 1;
   while (lo <= hi) {
     int m = lo + (hi - lo) / 2;
     int cmp = key.compareTo(keys[m]);
     if (cmp < 0) hi = m - 1;
     else if (cmp > 0) lo = m + 1;
     else return m;
   }
   return lo;
 }
 private int rank(Node node, Key key) {
   if (node == null) return 0;
   int cmp = key.compareTo(node.key);
   if (cmp == 0) {
     return size(node.left);
   } else if (cmp < 0) {
     return rank(node.left, key);
   } else {
     return size(node.left) + 1 + rank(node.right, key);
   }
 }
Esempio n. 22
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;
   }
 }
Esempio n. 23
0
  // delete the key-value pair with the given key rooted at h
  private Node delete(Node h, Key key) {
    // assert get(h, key) != null;

    if (key.compareTo(h.key) < 0) {
      if (!isRed(h.left) && !isRed(h.left.left)) h = moveRedLeft(h);
      h.left = delete(h.left, key);
    } else {
      if (isRed(h.left)) h = rotateRight(h);
      if (key.compareTo(h.key) == 0 && (h.right == null)) return null;
      if (!isRed(h.right) && !isRed(h.right.left)) h = moveRedRight(h);
      if (key.compareTo(h.key) == 0) {
        Node x = min(h.right);
        h.key = x.key;
        h.val = x.val;
        // h.val = get(h.right, min(h.right).key);
        // h.key = min(h.right).key;
        h.right = deleteMin(h.right);
      } else h.right = delete(h.right, key);
    }
    return balance(h);
  }
Esempio n. 24
0
File: BST.java Progetto: 9r3y/c47
 private Node delete(Node x, Key key) {
   if (x == null) return null;
   int cmp = key.compareTo(x.key);
   if (cmp < 0) x.left = delete(x.left, key);
   else if (cmp > 0) x.right = delete(x.right, key);
   else {
     if (x.right == null) return x.left;
     if (x.left == null) return x.right;
     Node t = x;
     x = min(t.right);
     x.right = deleteMin(t.right);
     x.left = t.left;
   }
   x.N = size(x.left) + size(x.right) + 1;
   return x;
 }
Esempio n. 25
0
  // insert the key-value pair in the subtree rooted at h
  private Node put(Node h, Key key, Value val) {
    if (h == null) return new Node(key, val, RED, 1);

    int cmp = key.compareTo(h.key);
    if (cmp < 0) h.left = put(h.left, key, val);
    else if (cmp > 0) h.right = put(h.right, key, val);
    else h.val = val;

    // fix-up any right-leaning links
    if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
    if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
    if (isRed(h.left) && isRed(h.right)) flipColors(h);
    h.N = size(h.left) + size(h.right) + 1;

    return h;
  }
 private Node delete(Node node, Key key) {
   if (node == null) return null;
   int cmp = key.compareTo(node.key);
   if (cmp < 0) node.left = delete(node.left, key);
   else if (cmp > 0) node.right = delete(node.right, key);
   else {
     // replace node with its successor
     if (node.right == null) return node.left;
     if (node.left == null) return node.right;
     Node t = node;
     node = min(t.right);
     node.right = deleteMin(t.right);
     node.left = t.left;
   }
   node.N = size(node.left) + size(node.right) + 1;
   return node;
 }
 public Key floor(Key key) {
   int i = rank(key);
   if (i < N && key.compareTo(keys[i]) == 0) return keys[i];
   if (i == 0) return null;
   else return keys[i - 1];
 }
Esempio n. 28
0
File: BST.java Progetto: 9r3y/c47
 // check that ranks are consistent
 private boolean isRankConsistent() {
   for (int i = 0; i < size(); i++) if (i != rank(select(i))) return false;
   for (Key key : keys()) if (key.compareTo(select(rank(key))) != 0) return false;
   return true;
 }
 public int size(Key lo, Key hi) {
   if (lo.compareTo(hi) > 0) return 0;
   if (contains(hi)) return rank(hi) - rank(lo) + 1;
   else return rank(hi) - rank(lo);
 }
Esempio n. 30
0
 public int compareTo(Entry<Key, Value> entry) {
   return key.compareTo(entry.key);
 }