A traversal of a tree is a systematic way of accessing or visiting all the nodes of .
There are different traversal schemes, for DST we need to know:
Preorder traversal
Preorder traversal is where a node is visited before its descendants.
- Parents always come before their children.
- Running time for a tree with nodes is .
Link to originaldef preOrder(T, v): visit(v) for each child w of v in T: preOrder(T, w)In-order traversal
In-order traversal (applies to binary trees) is where a node is visited after its left sub-tree and before the right sub-tree.
Link to originaldef inOrder(T, v): if hasLeft(v): inOrder(T, left(v)) visit(v) if hasRight(v): inOrder(T, right(v))Postorder traversal
Postorder traversal is where a node is visited after its descendants.
- Running time for a tree with nodes is .
Link to originaldef postOrder(T, v): for each child w of v in T: postOrder(T, w) visit(v)