Created
January 4, 2020 19:59
-
-
Save cozzbie/1e77198ffac6cc13c0c54c7c5c08262d to your computer and use it in GitHub Desktop.
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
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