Skip to content

Instantly share code, notes, and snippets.

@cozzbie
Created January 4, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzbie/1e77198ffac6cc13c0c54c7c5c08262d to your computer and use it in GitHub Desktop.
Save cozzbie/1e77198ffac6cc13c0c54c7c5c08262d to your computer and use it in GitHub Desktop.
const node = val => {
return {
data: val,
left: null,
right: null
};
}
const buildTree = () => {
const root = node(10);
root.left = node(11);
root.left.left = node(7);
root.right = node(9);
root.right.left = node(15);
root.right.right = node(8);
return root;
}
const preOrder = node => {
if(!node) {
return;
}
console.log(node.data);
preOrder(node.left);
preOrder(node.right);
}
const inOrder = node => {
if(!node) {
return;
}
inOrder(node.left);
console.log(node.data);
inOrder(node.right);
}
const postOrder = node => {
if(!node) {
return;
}
postOrder(node.left);
postOrder(node.right);
console.log(node.data);
}
const tree = buildTree();
console.log('Preorder')
preOrder(tree);
console.log('Inorder');
inOrder(tree);
console.log('PostOrder')
postOrder(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment