Binary search trees have impressive performance characteristics since all operations can be done in O(log(n)) time on average. However, this is not always the case if a tree is unbalanced (more nodes on one side than another). The worst case runtime for a BST can be O(n) if a tree is completely unbalanced (there are much more complex data structures like AVL and Red-Black Trees which balance themselves to prevent these kinds of issues).
Operation Average Runtime search O(log(n)) insert O(log(n)) delete O(log(n)) space complexity O(n)
Preorder:
- Visit the root
- Traverse the left subtree
- Traverse the right subtree
InOrder:
- Traverse the left subtree
- Visit the root
- Traverse the right subtree
PostOrder
- Traverse the left subtree
- Traverse the right subtree
- Visit the root
- start at the root
- place the root node in a queue
- while there is anything in the queue
- remove the first element in the queue (dequeue)
- if what has been dequeued has a left node
- enqueue the left node
- if what has been dequeued has a right node
- enqueue the right node