Trees and heaps
Structures that branch: binary trees and their traversals, search trees that keep keys in order, and heaps that always know their smallest item.
About 8 hours of study
Learning objectives
- Traverse a tree in preorder, inorder and postorder
- Insert, search and delete in a binary search tree
- Build a heap in an array with sift-up and sift-down
- Relate the cost of an operation to the height of the tree
Lessons
- 1
Binary trees
Nodes with left and right children, preorder, inorder and postorder traversal, and recursive height and count.
- 2
Binary search trees
Smaller keys to the left, larger to the right: insert, search, and delete with its three cases.
- 3
Heaps and priority queues
A complete binary tree stored in an array: push with sift-up, pop the minimum with sift-down, and build a heap in O(n).
Module practice
- Leaves and heightEasy
- Level by levelMedium
- Three traversalsHard
- The search pathEasy
- Build a binary search treeMedium
- Delete from a search treeHard
- Is it a min-heap?Easy
- The k smallest numbersMedium
- Join the ropesHard