//binary tree
//add order remove find
function tree() {
var node = function(key) {
this.left = null;
this.right = null;
this.key = key;
};
var root = null;
var insertnode = function(node, newnode) {
if(node.key > newnode.key) {
if(node.left === null) {
node.left = newnode;
} else {
insertnode(node.left, newnode);
}
} else {
if(node.right === null) {
node.right = newnode;
} else {
insertnode(node.right, newnode);
}
}
}
var mildletreenode = function(node, callback) {
if(node !== null) {
mildletreenode(node.left, callback);
callback(node.key);
mildletreenode(node.right, callback);
}
}
var beforetreenode = function(node, callback) {
if(node !== null) {
callback(node.key);
beforetreenode(node.left, callback);
beforetreenode(node.right, callback);
}
}
var aftertreenode = function(node, callback) {
if(node !== null) {
aftertreenode(node.left, callback);
aftertreenode(node.right, callback);
callback(node.key);
}
}
var minnode = function(node) {
if(node) {
while(node.left != null) {
node = node.left;
}
return node.key;
}
}
var maxnode = function(node) {
if(node) {
while(node.right != null) {
node = node.right;
}
return node.key;
}
}
var findnode = function(node, aim) {
if(node === null) {
return false;
}
if(node.key < aim) {
return findnode(node.right, aim);
} else if(node.key > aim) {
return findnode(node.left, aim);
} else {
return true;
}
}
var findaimnode = function(node) {
if(node) {
while(node.left != null) {
node = node.left;
}
return node;
}
}
var removenode = function(node, aim) {
if(node === null) {
return null;
}
if(node.key > aim) {
node.left = removenode(node.left, aim);
return node;
} else if(node.key < aim) {
node.right = removenode(node.right, aim);
return node;
} else {
if(node.left === null && node.right === null) {
node = null;
return node;
} else if(node.left !== null && node.right === null) {
node = node.left;
return node;
} else if(node.left === null && node.right !== null) {
node = node.right;
return node;
} else {
var taimnode = findaimnode(node.right);
node.key = taimnode.key;
node.right = removenode(node.right, taimnode.key);
return node;
}
}
}
var addnode = function(node, newnode) {
if(node.key > newnode.key) {
if(node.left === null) {
node.left = newnode;
} else {
insertnode(node.left, newnode);
}
} else {
if(node.right === null) {
node.right = newnode;
} else {
insertnode(node.right, newnode);
}
}
}
this.insert = function(key) {
var newnode = new node(key);
if(root === null) {
root = newnode;
} else {
insertnode(root, newnode);
}
};
this.mildletree = function(callback) {
mildletreenode(root, callback);
}
this.beforetree = function(callback) {
beforetreenode(root, callback);
}
this.aftertree = function(callback) {
aftertreenode(root, callback);
}
this.min = function() {
return minnode(root);
}
this.max = function() {
return maxnode(root);
}
this.find = function(aim) {
return findnode(root, aim);
}
this.remove = function(aim) {
return removenode(root, aim);
}
this.add = function(key) {
var newnode = new node(key);
addnode(root, newnode);
}
}
function callback(key) {
console.log(key);
}
//var nodes = [8, 3, 1, 6, 10, 5, 14];
//var thetree = new tree();
//nodes.forEach(function(key) {
// thetree.insert(key);
//})
//thetree.mildletree(callback);
//thetree.beforetree(callback);
//thetree.aftertree(callback);
//alert(thetree.max());
//alert(thetree.min());
//alert(thetree.find(4));
//thetree.remove(14);
//thetree.mildletree(callback);
//thetree.add(4);
//thetree.mildletree(callback)