bbtree icon indicating copy to clipboard operation
bbtree copied to clipboard

new BBTree().find(x) causes exception (cannot search on empty tree)

Open IkarosKappler opened this issue 7 years ago • 1 comments

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!

IkarosKappler avatar Dec 18 '18 19:12 IkarosKappler

I added an iterator and a toString() function and created a gist: https://gist.github.com/IkarosKappler/ca15b1afede5be0a4dd6b2cdc8040b74

IkarosKappler avatar Dec 19 '18 19:12 IkarosKappler