Advanced structures
Four structures for harder problems: tries for words, union-find for groups, segment trees for ranges, and AVL trees that keep themselves balanced.
About 9 hours of study
Learning objectives
- Store and search words by prefix with a trie
- Track groups with union-find
- Answer range questions with a segment tree or a Fenwick tree
- Keep a search tree balanced with rotations
Lessons
- 1
Tries: prefix trees Intermediate
Store words letter by letter in a tree, so finding a word, or every word that starts with a prefix, costs only the length of the word.
- 2
Disjoint sets (union-find) Intermediate
Keep track of which items are in the same group, merging groups in almost constant time with union by size and path compression.
- 3
Segment trees and Fenwick trees Advanced
Answer range-sum questions and change single values in O(log n) each, where a plain array or prefix sums make one of the two slow.
- 4
AVL trees: self-balancing search trees Advanced
Keep a binary search tree's height close to log n by checking every node's balance after an insert and repairing it with rotations.
Module practice
- Word or prefix?Easy
- Count words by prefixMedium
- AutocompleteHard
- Same group?Easy
- Friend groupsMedium
- Edges that close a cycleHard
- Range sums without updatesEasy
- Range minimum with updatesMedium
- Range sums with updatesHard
- Is the tree balanced?Easy
- Rotate the rootMedium
- Build an AVL treeHard