Some important concepts in trees:
Types of Traversals:
Depth first traversal is where we prioritize going through the depth (height) of the tree first. methods for depth first traversal:
Pre-order: root >> left >> right
ALGORITHM preOrder(root)
// INPUT <-- root node
// OUTPUT <-- pre-order output of tree node's values
OUTPUT <-- root.value
if root.left is not Null
preOrder(root.left)
if root.right is not NULL
preOrder(root.right)
In-order: left >> root >> right
ALGORITHM inOrder(root)
// INPUT <-- root node
// OUTPUT <-- in-order output of tree node's values
if root.left is not NULL
inOrder(root.left)
OUTPUT <-- root.value
if root.right is not NULL
inOrder(root.right)
Post-order: left >> right >> root
ALGORITHM postOrder(root)
// INPUT <-- root node
// OUTPUT <-- post-order output of tree node's values
if root.left is not NULL
postOrder(root.left)
if root.right is not NULL
postOrder(root.right)
OUTPUT <-- root.value
Breadth first traversal iterates through the tree by going through each level of the tree node-by-node. breadth first traversal uses a queue (instead of the call stack via recursion) to traverse the width/breadth of the tree.
Pseudocode:
ALGORITHM breadthFirst(root)
// INPUT <-- root node
// OUTPUT <-- front node of queue to console
Queue breadth <-- new Queue()
breadth.enqueue(root)
while breadth.peek()
node front = breadth.dequeue()
OUTPUT <-- front.value
if front.left is not NULL
breadth.enqueue(front.left)
if front.right is not NULL
breadth.enqueue(front.right)
| Binary Tree | K-ary Trees |
|---|---|
| restrict the number of children to two (left and right) | can have any number of children per node |
| no specific sorting order for a binary tree | we use K to refer to the maximum number of children that each Node is able to have |