Balanced tree
A tree where, at every node, the heights of the 2 subtrees differ by at most 1. That keeps its height at about log n.
Example
With 1 million nodes, a balanced tree has a height of about 20; a lopsided one can reach almost 1 million.
Test yourself: Balanced tree
Binary trees
Every perfect binary tree is also complete.
If every level is full, the last one is too, so it meets the complete rule. With 15 nodes in 4 levels, it's both.
Binary trees
A perfect binary tree of height 2 has ___ nodes.
2²⁺¹ − 1 = 7: 1 at the root, 2 on the next level and 4 leaves.
Trees in an array
What kind of tree is stored in an array without any gap?
A complete tree of 10 nodes takes indexes 0 to 9 without any None. A lopsided one of 10 would need 1,023 slots.
0/3
▶ PlayLessons that use it
Big O notation
How much does it grow?
Hash collisions
When 2 keys want the same spot
Binary trees
Each node, at most 2 children
Walking a tree
By levels or by branches
Trees in an array
No references, just math
Binary search trees
Smaller on the left, larger on the right
AVL trees
A tree that straightens itself
Heaps
The smallest, always on top
Choosing how to search
Brute force or using the order
Rebuilding a tree
Root, left and right
