Is this an efficient and clean implementation?
function BST(items) {
this.tree = {};
if (items) items.forEach(this.add, this);
}
BST.prototype.add = function(item) {
if (!this.tree.value) {
this.tree.value = item;
return item;
}
var currNode = this.tree;
var inserted = false;
while (!inserted) {
if (item > currNode.value) {
if (currNode.rightNode) currNode = currNode.rightNode;
else {
currNode.rightNode = {
value: item
};
inserted = true;
}
} else {
if (currNode.leftNode) currNode = currNode.leftNode;
else {
currNode.leftNode = {
value: item
};
inserted = true;
}
}
}
return item;
};
BST.prototype.printTree = function() {
console.log(this.tree);
};