StudyHub

Algorithms · Hub

Trees

Six tree structures, from the basic vocabulary to the trees inside databases. Binary search trees keep values ordered for $O(h)$ search; AVL and B-trees keep $h$ small; heaps keep only the minimum or maximum on top.

Why it matters: the height of a tree is the cost of almost every operation on it. Every structure here is a different trick for keeping that height near $\log n$.

5 random questions mixed from all six tree pages.

Quick comparison

StructureSearchInsertDeleteMin / maxShape guarantee
Binary tree$O(n)$$O(1)$ at a free spot—$O(n)$None
Binary search tree$O(h)$$O(h)$$O(h)$$O(h)$None: $h$ can reach $n - 1$
AVL tree$O(\log n)$$O(\log n)$$O(\log n)$$O(\log n)$Heights of siblings differ by ≤ 1
Min-heap$O(n)$$O(\log n)$min: $O(\log n)$min: $O(1)$Complete tree
Max-heap$O(n)$$O(\log n)$max: $O(\log n)$max: $O(1)$Complete tree
B-tree$O(\log n)$$O(\log n)$$O(\log n)$$O(\log n)$All leaves at the same depth

The six tree pages

Binary tree basics

Root, leaves, height and the four traversals.

O(n) traversals · height ≥ ⌊log₂ n⌋

Study

Binary search tree

Smaller left, bigger right, at every node.

O(h) search / insert / delete · sorted in-order

Study

AVL tree

A BST that rotates to stay balanced.

O(log n) guaranteed · O(1) per rotation

Study

Min-heap

Every parent ≤ its children; min at the root.

O(n) build · O(log n) insert / remove-min

Study

Max-heap

Every parent ≥ its children; max at the root.

O(log n) insert / remove-max · O(1) peek

Study

B-tree

Fat nodes with many keys; grows by splitting.

O(log n) · very short · one disk read per level

Study

Which one to pick?

flowchart TD
  A{"What do you need?"} -->|"only the smallest or largest, fast"| H{"Smallest or largest?"}
  H -->|smallest| MIN["Min-heap"]
  H -->|largest| MAX["Max-heap"]
  A -->|"ordered keys: search, ranges, sorted output"| B{"Data on disk, millions of keys?"}
  B -->|yes| BT["B-tree"]
  B -->|"no, in memory"| C{"Need a worst-case guarantee?"}
  C -->|yes| AVL["AVL tree"]
  C -->|"no, input is random"| BST["Binary search tree"]
  A -->|"just a hierarchy, no ordering"| BIN["Binary tree basics"]
      

Key points

External resources

Videos

Review schedule

Come back after 3, 7, 14, then 30 days. Spacing beats cramming.

Feynman prompt

Explain this topic in your own words, as if teaching a beginner.

Explain it in your own words first, then record a 2–3 minute video on your phone.

25:00

Ready to record? Open your phone camera now and record a 2–3 minute explanation.

My notes

Saved only in this browser (localStorage). Nothing is uploaded.