X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FBalancedTree.java;h=7d1c34117755cf6442a04c0975f5afb8103337a9;hb=c8598d51fe71ce77d466c02b6fa264617be9335b;hp=1c42ef1643b1842da5bfd6abc8c572f874bc4ed3;hpb=2bfc4055cd215be961a882cab0256b038350f61a;p=org.ibex.core.git diff --git a/src/org/ibex/util/BalancedTree.java b/src/org/ibex/util/BalancedTree.java index 1c42ef1..7d1c341 100644 --- a/src/org/ibex/util/BalancedTree.java +++ b/src/org/ibex/util/BalancedTree.java @@ -27,7 +27,7 @@ public class BalancedTree { /** the number of elements in the tree */ public final int treeSize() { return root == 0 ? 0 : size[root]; } - + /** clamps index to [0..treeSize()] and inserts object o *before* the specified index */ public final synchronized void insertNode(int index, Object o) { if(o == null) throw new Error("can't insert nulls in the balanced tree"); @@ -224,10 +224,10 @@ public class BalancedTree { if (d <= 0) throw new Error("rotation error"); left[d] = b; right[b] = c <= 0 ? -d : c; + parent[b] = d; parent[d] = p; if(c > 0) parent[c] = b; - if (p == 0) root = d; else if (left[p] == b) left[p] = d; else if (right[p] == b) right[p] = d; @@ -331,6 +331,7 @@ public class BalancedTree { // we found the node to delete } else { + // fast path: it has no children if (left[slot] <= 0 && right[slot] <= 0) { if (p == 0) root = 0; @@ -338,6 +339,7 @@ public class BalancedTree { int[] side = left[p] == slot ? left : right; side[p] = side[slot]; // fix parent's pointer } + // fast path: it has no left child, so we replace it with its right child } else if (left[slot] <= 0) { if (p == 0) root = right[slot];