Traversal Complexities
Let's lay out our numeric conclusions for the traversal approaches:
preorder() | ||
---|---|---|
inorder() | ||
postorder() |
Where is the tree's order, and is the tree's height.
The number of calls and stack sizes are all the same. Now, recall the Proper Binary Tree Bounds Lemma: The height of a tree, denoted has the bounds:
where is the number of nodes. This yields the following complexities:
Traversal Method | Time Complexity | Space Complexity |
---|---|---|
preorder() |
||
inorder() |
||
postorder() |
Where is the tree's order, and is the tree's height.
Thus, traversing an entire binary tree has a time complexity of And the space complexity for such a traversal is at least and at most depending on the tree's height.