Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Order Statistic Tree (fixes #15) #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ Methods
### clear()
> Removes all nodes from the tree.

### get(index)
> Get the item at the given index.

### rank(item)
> Get the index of the given item.

### find(item)
> Returns node data if found, null otherwise.

Expand Down
3 changes: 3 additions & 0 deletions lib/bintree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function Node(data) {
this.data = data;
this.left = null;
this.right = null;
this.size = 1;
}

Node.prototype.get_child = function(dir) {
Expand Down Expand Up @@ -63,6 +64,7 @@ BinTree.prototype.insert = function(data) {

// update helpers
p = node;
node.size++;
node = node.get_child(dir);
}
};
Expand All @@ -85,6 +87,7 @@ BinTree.prototype.remove = function(data) {
node = node.get_child(dir);
var cmp = this._comparator(data, node.data);
dir = cmp > 0;
node.size--;

if(cmp === 0) {
found = node;
Expand Down
14 changes: 14 additions & 0 deletions lib/rbtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Node(data) {
this.left = null;
this.right = null;
this.red = true;
this.size = 1;
}

Node.prototype.get_child = function(dir) {
Expand Down Expand Up @@ -60,6 +61,14 @@ RBTree.prototype.insert = function(data) {
p.set_child(dir, node);
ret = true;
this.size++;

// update the sizes from root to the new node
var root = head.right;
while (root !== node) {
root.size++;
var c = this._comparator(data, root.data);
root = root.get_child(c > 0);
}
}
else if(is_red(node.left) && is_red(node.right)) {
// color flip
Expand Down Expand Up @@ -130,6 +139,7 @@ RBTree.prototype.remove = function(data) {
gp = p;
p = node;
node = node.get_child(dir);
node.size--;

var cmp = this._comparator(data, node.data);

Expand Down Expand Up @@ -207,6 +217,10 @@ function single_rotate(root, dir) {
root.red = true;
save.red = false;

var child = save.get_child(!dir);
save.size = root.size;
root.size -= (child && child.size) + 1;

return save;
}

Expand Down
29 changes: 29 additions & 0 deletions lib/treebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ TreeBase.prototype.reach = function(cb) {
}
};

// get the data at the given index
TreeBase.prototype.get = function(index) {
var select = function (node, i) {
if (!node) {
return null;
}
var r = (node.left && node.left.size) + 1;
return i === r ? node.data : i < r ? select(node.left, i) : select(node.right, i - r);
};
return select(this._root, index + 1);
};

// get the index of the given data
TreeBase.prototype.rank = function(data) {
var result = 0;
var node = this._root;
while (node) {
var c = this._comparator(data, node.data);
if (c >= 0) {
result += (node.left && node.left.size) + (c > 0);
if (c === 0) {
break;
}
}
node = node.get_child(c > 0);
}
return result;
};


function Iterator(tree) {
this._tree = tree;
Expand Down
28 changes: 28 additions & 0 deletions test/test_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ function minmax(assert, tree_class) {
assert.equal(tree.max(), _.max(inserts));
}

function get(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var remove = inserts.splice(0, inserts.length / 10);
remove.forEach(function(r) {
tree.remove(r);
});
inserts.sort(function(a,b) { return a - b; });
for(var i = 0; i < inserts.length; i++) {
assert.equal(tree.get(i), inserts[i]);
}
}

function rank(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var remove = inserts.splice(0, inserts.length / 10);
remove.forEach(function(r) {
tree.remove(r);
});
inserts.sort(function(a,b) { return a - b; });
for(var i = 0; i < inserts.length; i++) {
assert.equal(tree.rank(inserts[i]), i);
}
}

function forward_it(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
Expand Down Expand Up @@ -294,6 +320,8 @@ var TESTS = {
dup: dup,
nonexist: nonexist,
minmax: minmax,
get: get,
rank: rank,
forward_it: forward_it,
forward_it_break: forward_it_break,
reverse_it: reverse_it,
Expand Down