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; }
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; }
private Node deleteMaxWithOutDeleteThread(Node x) { if (x == null) return null; if (x.right == null) { return x.left; } x.right = deleteMax(x.right); 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; }
private Node deleteMax(Node x) { if (x == null) return null; 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; } x.right = deleteMax(x.right); 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; }