@Override protected void fixAggregates(int node) { super.fixAggregates(node); aggregatedCounts[node] = counts[node] + aggregatedCounts[left(node)] + aggregatedCounts[right(node)]; }
/** Rotate left the subtree under <code>n</code> */ private void rotateLeft(int n) { final int r = right(n); final int lr = left(r); right(n, lr); if (lr != NIL) { parent(lr, n); } final int p = parent(n); parent(r, p); if (p == NIL) { root = r; } else if (left(p) == n) { left(p, r); } else { assert right(p) == n; right(p, r); } left(r, n); parent(n, r); fixAggregates(n); fixAggregates(parent(n)); }
/** Rotate right the subtree under <code>n</code> */ private void rotateRight(int n) { final int l = left(n); final int rl = right(l); left(n, rl); if (rl != NIL) { parent(rl, n); } final int p = parent(n); parent(l, p); if (p == NIL) { root = l; } else if (right(p) == n) { right(p, l); } else { assert left(p) == n; left(p, l); } right(l, n); parent(n, l); fixAggregates(n); fixAggregates(parent(n)); }
/** * Update <code>node</code> with the current data. */ public void update(int node) { final int prev = prev(node); final int next = next(node); if ((prev == NIL || compare(prev) > 0) && (next == NIL || compare(next) < 0)) { // Update can be done in-place copy(node); for (int n = node; n != NIL; n = parent(n)) { fixAggregates(n); } } else { // TODO: it should be possible to find the new node position without // starting from scratch remove(node); add(); } }
private void rebalance(int node) { for (int n = node; n != NIL; ) { final int p = parent(n); fixAggregates(n); switch (balanceFactor(n)) { case -2: final int right = right(n); if (balanceFactor(right) == 1) { rotateRight(right); } rotateLeft(n); break; case 2: final int left = left(n); if (balanceFactor(left) == -1) { rotateLeft(left); } rotateRight(n); break; case -1: case 0: case 1: break; // ok default: throw new AssertionError(); } n = p; } }
root = nodeAllocator.newNode(); copy(root); fixAggregates(root); return true; } else {
@Override protected void fixAggregates(int node) { super.fixAggregates(node); aggregatedCounts[node] = counts[node] + aggregatedCounts[left(node)] + aggregatedCounts[right(node)]; }
/** Rotate right the subtree under <code>n</code> */ private void rotateRight(int n) { final int l = left(n); final int rl = right(l); left(n, rl); if (rl != NIL) { parent(rl, n); } final int p = parent(n); parent(l, p); if (p == NIL) { root = l; } else if (right(p) == n) { right(p, l); } else { assert left(p) == n; left(p, l); } right(l, n); parent(n, l); fixAggregates(n); fixAggregates(parent(n)); }
/** Rotate left the subtree under <code>n</code> */ private void rotateLeft(int n) { final int r = right(n); final int lr = left(r); right(n, lr); if (lr != NIL) { parent(lr, n); } final int p = parent(n); parent(r, p); if (p == NIL) { root = r; } else if (left(p) == n) { left(p, r); } else { assert right(p) == n; right(p, r); } left(r, n); parent(n, r); fixAggregates(n); fixAggregates(parent(n)); }
/** * Update <code>node</code> with the current data. */ public void update(int node) { final int prev = prev(node); final int next = next(node); if ((prev == NIL || compare(prev) > 0) && (next == NIL || compare(next) < 0)) { // Update can be done in-place copy(node); for (int n = node; n != NIL; n = parent(n)) { fixAggregates(n); } } else { // TODO: it should be possible to find the new node position without // starting from scratch remove(node); add(); } }
private void rebalance(int node) { for (int n = node; n != NIL; ) { final int p = parent(n); fixAggregates(n); switch (balanceFactor(n)) { case -2: final int right = right(n); if (balanceFactor(right) == 1) { rotateRight(right); } rotateLeft(n); break; case 2: final int left = left(n); if (balanceFactor(left) == -1) { rotateLeft(left); } rotateRight(n); break; case -1: case 0: case 1: break; // ok default: throw new AssertionError(); } n = p; } }
root = nodeAllocator.newNode(); copy(root); fixAggregates(root); return true; } else {