Created
December 9, 2011 08:49
-
-
Save thinkphp/1450784 to your computer and use it in GitHub Desktop.
Binary Search Tree in MooTools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* by Adrian Statescu <[email protected]> | |
* Twitter: @thinkphp | |
* G+ : http://gplus.to/thinkphp | |
* MIT Style License | |
*/ | |
var Node = new Class({ | |
initialize: function(info) { | |
this.info = info; | |
this.left = null; | |
this.right = null; | |
this.level = null; | |
} | |
}); | |
var SearchBinaryTree = new Class({ | |
initialize: function() { | |
this._root = null; | |
}, | |
create: function(val){ | |
if(this._root == null) { | |
this._root = new Node(val); | |
} else { | |
current_node = this._root; | |
for(;;) { | |
if(val < current_node.info) { | |
if(current_node.left) { | |
current_node = current_node.left; | |
} else { | |
current_node.left = new Node(val); | |
break; | |
} | |
} else if(val > current_node.info) { | |
if(current_node.right) { | |
current_node = current_node.right; | |
} else { | |
current_node.right = new Node(val); | |
break; | |
} | |
} else { | |
break; | |
} | |
} | |
} | |
}, | |
traversal: function(method,callback) { | |
switch(method) { | |
case 'inorder': | |
inorder(this._root); | |
break; | |
case 'preorder': | |
preorder(this._root); | |
break; | |
case 'postorder': | |
postorder(this._root); | |
break; | |
default: | |
BFT(this._root) | |
} | |
function inorder(node) { | |
if(node.left) { | |
inorder(node.left); | |
} | |
callback.call(this,node); | |
if(node.right) { | |
inorder(node.right); | |
} | |
} | |
function preorder(node) { | |
callback.call(this,node); | |
if(node.left) { | |
preorder(node.left); | |
} | |
if(node.right) { | |
preorder(node.right); | |
} | |
} | |
function postorder(node) { | |
if(node.left) { | |
preorder(node.left); | |
} | |
if(node.right) { | |
preorder(node.right); | |
} | |
callback.call(this,node); | |
} | |
function BFT(node) { | |
node.level = 1; | |
var queue = [node], | |
output = [], | |
current_level = node.level, | |
current_node; | |
while(queue.length > 0) { | |
current_node = queue.shift(); | |
if(current_node.level > current_level) { | |
current_level++; | |
output.push("\n"); | |
} | |
output.push(current_node.info + " "); | |
if(current_node.left) { | |
current_node.left.level = current_level + 1; | |
queue.push(current_node.left); | |
} | |
if(current_node.right) { | |
current_node.right.level = current_level + 1; | |
queue.push(current_node.right); | |
} | |
} | |
return callback.call(this,output.join("")) | |
} | |
} | |
}); | |
var tree = new SearchBinaryTree(); | |
tree.create(8); | |
tree.create(3); | |
tree.create(1); | |
tree.create(6); | |
tree.create(4); | |
tree.create(7); | |
tree.create(10); | |
tree.create(14); | |
tree.create(13); | |
var out = []; | |
console.log("Breadth-First Traversal"); | |
tree.traversal('BFT',function(node){ | |
console.log(node) | |
}) | |
tree.traversal('inorder',function(node){ | |
out.push(node.info) | |
}) | |
console.log("Inorder Traversal"); | |
console.log(out) | |
out = [] | |
tree.traversal('preorder',function(node){ | |
out.push(node.info) | |
}) | |
console.log("Preorder Traversal"); | |
console.log(out) | |
out = [] | |
tree.traversal('postorder',function(node){ | |
out.push(node.info) | |
}) | |
console.log("Postorder Traversal"); | |
console.log(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment