function Node(number) {
this.left = null;
this.right = null;
this.element = number;
}
function Bst() {
this.root = null;
}
Bst.prototype = {
constructor: Bst,
insert: function(number) {
var node = new Node(number);
if (this.root === null) {
this.root = node;
} else {
this.insertNode(this.root, node);
}
},
insertNode: function(node, newNode) {
if (newNode.element < node.element) {
if (node.left === null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
this.insertNode(node.right, newNode);
}
}
},
inOrderTraverse: function(fn) {
this.inOrderTraverseNode(this.root, fn);
},
inOrderTraverseNode(node, fn) {
if (node !== null) {
this.inOrderTraverseNode(node.left, fn);
fn(node.element);
this.inOrderTraverseNode(node.right, fn);
}
}
}
// a.insert(0, 5);
// a.insert(1, 4);
// console.log(a);
// a.removeAt(2);
// console.log(a);
console