Skip to content

Instantly share code, notes, and snippets.

@scriptonian
Last active September 9, 2017 15:32
Show Gist options
  • Save scriptonian/d11401891c2af86e729b27fd7f4c26c8 to your computer and use it in GitHub Desktop.
Save scriptonian/d11401891c2af86e729b27fd7f4c26c8 to your computer and use it in GitHub Desktop.
Binary Search Tree Traversal
BinarySearchTree.prototype = {
/*
other code here
*/
inOrder: function(node){
if (node !== null) {
//print the left subtree recursively
this.inOrder(node.left);
//print the root node
console.log(node.toString());
//print the right subtree recursively
this.inOrder(node.right);
}
},
preOrder: function(node){
if (node !== null) {
//print the root node
console.log(node.toString());
//print the left subtree recursively
this.preOrder(node.left);
//print the right subtree recursively
this.preOrder(node.right);
}
},
postOrder: function(node){
if (node !== null) {
//print the left subtree recursively
this.postOrder(node.left);
//print the right subtree recursively
this.postOrder(node.right);
//print the root node
console.log(node.toString());
}
}
/*
other code htere
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment