CPSC 221
Balanced Trees, and B-Trees
Today
Announcements
- PA2 is due tonight, 11:59pm
- Examlet 6 is Thursday to Sunday: binary search trees
Today:
- How tall is an AVL tree?
- Balanced BSTs: the classics, pros and cons
- B-trees: when the data won’t fit in memory
Recap
Last time: AVL trees, a BST that rotates to stay balanced.
| find |
\(O(n)\) |
\(O(h)\) |
| insert |
\(O(n)\) |
\(O(h)\) |
| remove |
\(O(n)\) |
\(O(h)\) |
How big can \(h\) be, for an AVL tree with \(n\) nodes?
AVL tree analysis
We want: \(h = O(\_\_\_\_\_\_\_\_)\).
An upper bound on the height of a tree with \(n\) nodes
is the same as
a lower bound on the number of nodes in a tree of height \(h\).
Define \(N(h)\): the least number of nodes in an AVL tree of height \(h\).
Your Turn
Draw the AVL tree with the fewest nodes for each height.
A recurrence
\(N(h) = 1 + N(\_\_\_\_\_\_) + N(\_\_\_\_\_\_)\)
Simplify:
Guess a closed form:
Prove the guess
Theorem: an AVL tree of height \(h\) has at least \(2^{h/2}\) nodes, for all \(h \ge\) ____.
Consider an arbitrary AVL tree, and let \(h\) denote its height.
Case 1:
Case 2:
Case 3:
Punchline
\(n \ge 2^{h/2}\), so \(h \le\) ____________.
| find |
\(O(n)\) |
|
| insert |
\(O(n)\) |
|
| remove |
\(O(n)\) |
|
Classic balanced BSTs
AVL trees: height at most \(1.44 \log_2 n\)
______ rotations on find
______ rotations on insert
______ rotations on remove
Red-black trees: height at most \(2 \log_2 n\)
A constant number of rotations on insert and on remove
And others: 2-3 trees, splay trees, treaps, …
Balanced BSTs, pros and cons
Pros
insert, remove, and find are always \(O(\log n)\)
- An improvement over ______________
- Range finding and nearest neighbour
Cons
- It is possible to search for single keys faster
- If the data is too big for memory, it lives on disk, and we need a different structure
Where are we?
B-trees
The only “out of core” data structure we’ll discuss:
a dictionary for BIG data.
Can we always fit the data in main memory? So where do we keep it?
Big-O analysis assumes every basic operation costs about the same.
But…
Photos: RAM by PantheraLeo1359531, CC BY 4.0; hard drive by Zzubnik, public domain (Wikimedia Commons).
The story on disks
A 2 GHz machine does about 2 million instructions per ______.
A disk seek takes about ________.
Imagine an AVL tree storing North American driving records.
How many records?
How much data, in total?
How deep is the AVL tree?
How many disk seeks to find a record?
The story on disks, continued
Suppose we weren’t careful…
Following one pointer costs one disk read.
So the number of disk reads is
____________.
B-tree of order \(m\)
Goal: minimize the number of reads from disk.
- Build a tree that uses one disk block per node
- A disk block is the fundamental unit of transfer
- Nodes have more than one key
- The tree should be balanced and shallow
- In practice, branching factors over 1000 are common
A B-tree
B-tree of order \(m\): a node
Every node has room for \(m - 1\) keys, but __________________.
If a node has \(i\) keys, then it has ________ child pointers.
The keys in a node are in order.
Definition of a B-tree
A B-tree of order \(m\) is an \(m\)-way search tree in which:
- Every node holds at most \(m - 1\) keys, in order
- An internal node with \(i\) keys has \(i + 1\) children
- Every non-root node holds at least \(\lceil m/2 \rceil - 1\) keys
- The root holds at least 1 key
- All leaves are on the same level
Your Turn
Place the keys into this B-tree of order 5. Not every slot will be used.
15 22 31 38 44 50 57 63 71 79 86 94
Back to the driving records
How many keys can a B-tree of order \(m\) and height \(h\) hold?
With \(m = 1000\): how tall is the tree for 300 million records?
How many disk reads to find a record?
Next time
Hashing: search for a single key faster than \(O(\log n)\).
| AVL tree |
\(O(\log n)\) |
| B-tree (disk reads) |
\(O(\log_m n)\) |
| hash table |
? |