bbtree
bbtree copied to clipboard
new BBTree().find(x) causes exception (cannot search on empty tree)
Hi, this is a neat implementation.
But your find() function does not consider empty trees, so searching in an empty tree causes an exception to be thrown (which is usually no expected behavior on collections).
Adding an if-null-return test fixes this issue:
find : function (key) {
if( !this.root )
return null;
var node = this.root,
compare = this._compare;
while (node !== bottom) {
var c = compare(key, node.key);
if (c === 0) return node;
node = c < 0 ? node.left : node.right;
}
return null;
};
Greetings!
I added an iterator and a toString() function and created a gist: https://gist.github.com/IkarosKappler/ca15b1afede5be0a4dd6b2cdc8040b74