2003/11/26 21:59:13
[org.ibex.core.git] / src / org / xwt / util / BalancedTree.java
diff --git a/src/org/xwt/util/BalancedTree.java b/src/org/xwt/util/BalancedTree.java
new file mode 100644 (file)
index 0000000..77091a0
--- /dev/null
@@ -0,0 +1,167 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt.util;
+
+// Implemented from http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/red_black.html
+
+// 1. Every node is either red or black.
+// 2. Every leaf node is black; if a red node has no right or left child,
+//    pretend that an imaginary (sentinel) black node is there.
+// 3. If a node is red, then both its children are black.
+// 4. Every simple path from a node to a descendant leaf contains the
+//    same number of black nodes.
+
+
+// FIXME: ability to ask for n^th node; requires a descendant count
+
+/** a red-black tree of arbitrary objects */
+public class RedBlackTree {
+
+    private static final boolean RED = false;
+    private static final boolean BLACK = true;
+
+    private static final int DELETE = 0; 
+    private static final int INSERT = 1;
+
+    // These arrays are indexed by "slot", a totally meaningless number
+    // assigned to each object object[slot] has index index[slot] and
+    // color color[slot].  Note that slot 0 is reserved as "null".
+
+    // FEATURE: use a bitmask?
+    private int[] left;          ///< if positive: left child's slot; if negative: predecessor's slot
+    private int[] right;         ///< if positive: right child's slot; if negative: successor's slot
+    private int[] size;          ///< the number of descendants of this node *including the node itself*
+    private Object[] objects;    ///< every object in the tree has an entry here; ordering is completely random
+
+    private int root = 0;               ///< the slot of the root element
+    
+    private int freeslot = 0;
+
+    private int leftmost(int slot) { return left[slot] <= 0 ? slot : leftmost(left[slot]); }
+    private int rightmost(int slot) { return right[slot] <= 0 ? slot : rightmost(right[slot]); }
+    private int next(int slot) { return right[slot] <= 0 ? -1 * right[slot] : leftmost(right[slot]); }
+    private int prev(int slot) { return left[slot] <= 0 ? -1 * left[slot] : rightmost(left[slot]); }
+
+    //    parent             parent
+    //      |                  |
+    //      b                  d 
+    //     / \                / \
+    //    a   d    < == >    b   e
+    //       / \            / \
+    //      c   e          a   c
+    void rotate(boolean toTheLeft, int b, int parent) {
+        if (b == 0) throw new Error("rotate called on the null slot");
+        int[] left = toTheLeft ? this.left : this.right;
+        int[] right = toTheLeft ? this.right : this.left;
+        int d = right[b];
+        if (d == 0) throw new Error("attempted to rotate a node with only one child in the wrong direction");
+        int c = left[d];
+        left[d] = b;
+        right[b] = c;
+        size[b] -= size[d];
+        int csize = c <= 0 ? 0 : size[c] + 1;
+        size[b] += csize;
+        size[d] -= csize;
+        size[d] += size[b];
+        if (parent == 0)              root = d;
+        else if (left[parent] == b)   left[parent] = d;
+        else if (right[parent] == b)  right[parent] = d;
+        else throw new Error("rotate called with invalid parent");
+    }
+
+    public void balance(int slot, int parent) {
+        if (slot == 0) return;
+        if (size[left[slot]] > 2 * size[right[slot]]) {
+            rotate(false, slot, parent);
+        } else if (size[left[slot]] * 2 < size[right[slot]]) {
+            rotate(true, slot, parent);
+        }
+        size[slot] = 1 + size[left[slot]] + size[right[slot]];
+    }
+
+    // FIXME: maintain fakeptrs
+
+
+    // private void intersection() { }
+    // private void union() { }
+    // private void subset() { }
+
+    private void insert(int idx, int arg, int slot, int parent) {
+
+        int diff = idx - size[left[slot]];
+        if (slot == 0 || diff == 0) {
+            if (size[arg] != 0) throw new Error("double insertion");
+
+            left[arg] = left[slot];   // steal slot's left subtree
+            left[slot] = 0;
+            right[arg] = slot;        // make slot our right subtree
+
+            // FIXME: if slot == 0 we can't use it to figure out which end of parent we belong on
+            if (parent == 0) root = arg;
+            else (left[parent] == slot ? left : right)[parent] = arg;
+
+            balance(slot, arg);
+            balance(arg, slot);
+            return;
+        }
+
+        if (diff < 0) insert(idx, arg, left[slot], slot);
+        else insert(idx - size[left[slot]] - 1, arg, right[slot], slot);
+        balance(slot, parent);
+    }
+
+    private int indexOf(int slot) {
+        int parent = -1 * left[leftmost(slot)];
+        if (parent == 0) return size[left[slot]];             // we are on the far left edge
+        else return size[left[slot]] + indexOf(parent) + 1;   // all nodes after parent and before us are in our left subtree
+    }
+
+    private int get(int idx, int slot) {
+        int diff = idx - size[left[slot]];
+        if (diff > 0) return get(diff - 1, right[slot]);
+        else if (diff < 0) return get(idx, left[slot]);
+        else return slot;
+    }
+
+    // return slot that was deleted
+    private int delete(int idx, int slot, int parent) {
+        int diff = idx - size[left[slot]];
+        if (slot == 0) return 0;
+        else if (diff < 0) {
+            int ret = delete(idx, left[slot], slot);
+            balance(slot, parent);
+            return ret;
+
+        } else if (diff > 0) {
+            int ret = delete(diff - 1, right[slot], slot);
+            balance(slot, parent);
+            return ret;
+
+        } else {
+            size[slot] = 0;
+            if (left[slot] == 0) {
+                if (parent == 0) root = right[slot];
+                else (left[parent] == slot ? left : right)[parent] = right[slot];
+                right[slot] = 0;
+                balance(slot, parent);
+            } else if (right[slot] == 0) {
+                if (parent == 0) root = left[slot];
+                else (left[parent] == slot ? left : right)[parent] = left[slot];
+                left[slot] = 0;
+                balance(slot, parent);
+            } else {
+                int replacement = delete(idx - 1, slot, parent);
+                if (replacement != 0) {
+                    left[replacement] = left[slot];
+                    right[replacement] = right[slot];
+                }
+                if (parent == 0) root = replacement;
+                else (left[parent] == slot ? left : right)[parent] = replacement;
+                left[slot] = 0;
+                right[slot] = 0;
+                balance(replacement, parent);
+            }
+            return slot;
+        }
+    }
+
+}