Tree height
The height is the number of edges from the root to the farthest leaf. A node's depth is the number of edges from the root to that node.
Example
In the chain 1 → 2 → 5 → 8, the depth of 8 is 3 and the tree's height is also 3.
Test yourself: Tree height
Binary trees
The root of any tree has depth ___.
There are no edges between the root and itself. Its child has depth 1 and its grandchild, 2.
0/1
â–¶ PlayLessons that use it
What is an algorithm
You already use algorithms
Iteration and recursion
2 ways to repeat
Dynamic lists
An array that grows by itself
Stacks
Last in, first out
Hash algorithms
A good hash spreads evenly
Binary trees
Each node, at most 2 children
Walking a tree
By levels or by branches
Binary search trees
Smaller on the left, larger on the right
AVL trees
A tree that straightens itself
Heaps
The smallest, always on top
Building a heap
Bottom-up is cheaper
Binary search in depth
2 pointers closing in
Searching with a hash
From O(n²) to O(n) with a table
Choosing how to search
Brute force or using the order
What makes a good sort
5 things you ask of it
Selection sort
Picking the smallest, over and over
Bubble sort
The largest rises like a bubble
Insertion sort
Like arranging the cards in your hand
Quick sort
A pivot and 2 sides
Merge sort
Split in half and merge in order
Rebuilding a tree
Root, left and right
Tower of Hanoi
n − 1, 1 and n − 1
Backtracking
Try, and if it fails, go back
Dynamic programming
Solve each thing only once
Fractional knapsack
Most value per kilo
Largest container
Move the shorter board
Requirements and estimates
Ask before you draw
