Skip to content

Commit

Permalink
implement key enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
cschen1205 committed May 26, 2017
1 parent 085c08c commit e565e24
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# js-redblacktree
Package provides javascript implementation of red black tree
Package provides javascript implementation of red black tree. The api of the red black tree is designed so that it matches with the api of the Java counterpart SortedMap api.



Expand Down Expand Up @@ -41,5 +41,11 @@ console.log(bst.size()); // display 3
console.log(bst.containsKey(6)); // display false;
console.log(bst.get(6)); // display undefined

// print out sorted keys

var keys = bst.keySet();
for(var i = 1; i < keys.length; ++i) {
console.log(keys[i]);
}

```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-redblacktree",
"version": "1.0.6",
"description": "Package implements various array sorting algorithms",
"description": "Package implements left leaning red black balanced search tree for symbol table key-sorted map",
"author": "Caishun Chen",
"contributors": [
"Caishun Chen <cschen1205@gmail.com>"
Expand Down
17 changes: 17 additions & 0 deletions src/jsrbtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ var jsrbtree = jsrbtree || {};
return x;
};

RedBlackTree.prototype.keySet = function() {
var keys = [];

this._collect(this.root, keys);
return keys;
};

RedBlackTree.prototype._collect = function(x, queue) {
if (x == null) {
return;
}

this._collect(x.left, queue);
queue.push(x.key);
this._collect(x.right, queue);
};

jss.RedBlackTree = RedBlackTree;

})(jsrbtree);
Expand Down
5 changes: 5 additions & 0 deletions test/bst-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ describe("Red Black Tree", function() {
expect(bst.size()).to.equal(4);
expect(bst.containsKey(6));
expect(bst.get(6)).to.equal(5.4);

var keys = bst.keySet();
for(var i = 1; i < keys.length; ++i) {
expect(keys[i-1]).to.below(keys[i]);
}
});

it("should delete correctly", function () {
Expand Down

0 comments on commit e565e24

Please sign in to comment.