tries and suffix trees - stanford...
TRANSCRIPT
![Page 1: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/1.jpg)
Balanced TreesPart One
![Page 2: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/2.jpg)
Balanced Trees
● Balanced search trees are among the most useful and versatile data structures.
● Many programming languages ship with a balanced tree library.● C++: std::map / std::set● Java: TreeMap / TreeSet
● Many advanced data structures are layered on top of balanced trees.● We’ll see several later in the quarter!
![Page 3: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/3.jpg)
Where We're Going
● B-Trees (Today)● A simple type of balanced tree developed for
block storage.● Red/Black Trees (Today/Thursday)
● The canonical balanced binary search tree.● Augmented Search Trees (Thursday)
● Adding extra information to balanced trees to supercharge the data structure.
![Page 4: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/4.jpg)
Outline for Today
● BST Review● Refresher on basic BST concepts and runtimes.
● Overview of Red/Black Trees● What we're building toward.
● B-Trees and 2-3-4 Trees● Simple balanced trees, in depth.
● Intuiting Red/Black Trees● A much better feel for red/black trees.
![Page 5: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/5.jpg)
A Quick BST Review
![Page 6: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/6.jpg)
Binary Search Trees
● A binary search tree is a binary tree with the following properties:
● Each node in the BST stores a key, and optionally, some auxiliary information.
● The key of every node in a BST is strictly greater than all keys to its left and strictly smaller than all keys to its right.
9
13
1
5
6
73
2 4
10
12
11
14
15
8
![Page 7: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/7.jpg)
Binary Search Trees
● The height of a binary search tree is the length of the longest path from the root to a leaf, measured in the number of edges.
● A tree with one node has height 0.
● A tree with no nodes has height -1, by convention.
9
13
1
5
6
73
2 4
10
12
11
14
15
8
![Page 8: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/8.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 9: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/9.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 10: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/10.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 11: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/11.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 12: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/12.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 13: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/13.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 14: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/14.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 15: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/15.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 16: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/16.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 17: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/17.jpg)
73
137
42
60
271
161 314
Searching a BST
![Page 18: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/18.jpg)
Inserting into a BST
![Page 19: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/19.jpg)
Inserting into a BST
73
137
42
60
271
161 314
![Page 20: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/20.jpg)
Inserting into a BST
73
137
42
60
271
161 314
![Page 21: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/21.jpg)
Inserting into a BST
73
137
42
60
271
161 314
![Page 22: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/22.jpg)
Inserting into a BST
73
137
42
60
271
161 314
![Page 23: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/23.jpg)
Inserting into a BST
73
137
42
60
271
161 314
![Page 24: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/24.jpg)
Inserting into a BST
73
137
42
60
271
161 314
166
![Page 25: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/25.jpg)
Inserting into a BST
73
137
42
60
271
161 314
166
![Page 26: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/26.jpg)
Deleting from a BST
![Page 27: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/27.jpg)
Deleting from a BST
73
137
42
60
271
161 314
166
Delete 60 from this tree, then 73, and then 137.
Work this out with a pencil and paper, and we’ll reconvene as a
group to do it together.
Delete 60 from this tree, then 73, and then 137.
Work this out with a pencil and paper, and we’ll reconvene as a
group to do it together.
![Page 28: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/28.jpg)
Deleting from a BST
73
137
42
60
271
161 314
166
![Page 29: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/29.jpg)
Deleting from a BST
73
137
42
60
271
161 314
166
![Page 30: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/30.jpg)
Deleting from a BST
73
137
42
60
271
161 314
166
![Page 31: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/31.jpg)
Deleting from a BST
73
137
42
60
271
161 314
166
![Page 32: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/32.jpg)
Deleting from a BST
73
137
42
271
161 314
166
![Page 33: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/33.jpg)
Deleting from a BST
73
137
42
271
161 314
166
Case 0: If the node has just no children, just
remove it.
Case 0: If the node has just no children, just
remove it.
![Page 34: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/34.jpg)
Deleting from a BST
73
137
42
271
161 314
166
![Page 35: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/35.jpg)
Deleting from a BST
73
137
42
271
161 314
166
![Page 36: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/36.jpg)
Deleting from a BST
137
42
271
161 314
166
![Page 37: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/37.jpg)
Deleting from a BST
137
42
271
161 314
166
![Page 38: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/38.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 39: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/39.jpg)
Deleting from a BST
137
42 271
161 314
166
Case 1: If the node has just one child, remove it and replace it with
its child.
Case 1: If the node has just one child, remove it and replace it with
its child.
![Page 40: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/40.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 41: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/41.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 42: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/42.jpg)
Deleting from a BST
42 271
161 314
166
![Page 43: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/43.jpg)
Deleting from a BST
42 271
161 314
166
![Page 44: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/44.jpg)
Deleting from a BST
42 271
161 314
166
![Page 45: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/45.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 46: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/46.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 47: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/47.jpg)
Deleting from a BST
137
42 271
161 314
166
![Page 48: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/48.jpg)
Deleting from a BST
161
42 271
161 314
166
![Page 49: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/49.jpg)
Deleting from a BST
161
42 271
314
166
![Page 50: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/50.jpg)
Deleting from a BST
161
42 271
314166
![Page 51: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/51.jpg)
Deleting from a BST
161
42 271
314166
Case 2: If the node has two children, find its inorder
successor (which has zero or one child), replace the node's key with its successor's key,
then delete its successor.
Case 2: If the node has two children, find its inorder
successor (which has zero or one child), replace the node's key with its successor's key,
then delete its successor.
![Page 52: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/52.jpg)
Runtime Analysis
● The time complexity of all these operations is O(h), where h is the height of the tree.● That’s the longest path we can take.
● In the best case, h = O(log n) and all operations take time O(log n).
● In the worst case, h = Θ(n) and some operations will take time Θ(n).
● Challenge: How do you efficiently keep the height of a tree low?
![Page 53: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/53.jpg)
A Glimpse of Red/Black Trees
![Page 54: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/54.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
110
107
106
166
161 261
140
![Page 55: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/55.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
53
31
59 97
58
26 41
![Page 56: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/56.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
53
31
59 97
58
26 41
![Page 57: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/57.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
5
2
8
7
1 4
![Page 58: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/58.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
5
2
8
7
1 4
![Page 59: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/59.jpg)
Red/Black Trees
● A red/black tree is a BST with the following properties:● Every node is either
red or black.● The root is black.● No red node has a red
child.● Every root-null path in
the tree passes through the same number of black nodes.
5
2
8
7
1 4
![Page 60: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/60.jpg)
Red/Black Trees
● Theorem: Any red/black tree with n nodes has height O(log n).● We could prove this now, but there's a much
simpler proof of this we'll see later on.● Given a fixed red/black tree, lookups can
be done in time O(log n).
![Page 61: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/61.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 62: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/62.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 63: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/63.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 64: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/64.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 65: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/65.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 66: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/66.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
13
![Page 67: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/67.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
13
![Page 68: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/68.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
13
![Page 69: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/69.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
13What are we
supposed to do with this new node?
What are we supposed to do with
this new node?
![Page 70: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/70.jpg)
Mutating Red/Black Trees
![Page 71: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/71.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 72: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/72.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 73: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/73.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 74: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/74.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 31
![Page 75: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/75.jpg)
Mutating Red/Black Trees
17
3 11 23 37
7 37
![Page 76: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/76.jpg)
Mutating Red/Black Trees
17
3 11 23
7 37
![Page 77: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/77.jpg)
Mutating Red/Black Trees
17
3 11 23
7 37
How do we fix up the black-height property?
How do we fix up the black-height property?
![Page 78: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/78.jpg)
Fixing Up Red/Black Trees
● The Good News: After doing an insertion or deletion, we can locally modify a red/black tree in time O(log n) to fix up the red/black properties.
● The Bad News: There are a lot of cases to consider and they're not trivial.
● Some questions:● How do you memorize / remember all the rules
for fixing up the tree?● How on earth did anyone come up with
red/black trees in the first place?
![Page 79: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/79.jpg)
Fixing Up Red/Black Trees
● The Good News: After doing an insertion or deletion, we can locally modify a red/black tree in time O(log n) to fix up the red/black properties.
● The Bad News: There are a lot of cases to consider and they're not trivial.
● Some questions:● How do you memorize / remember all the rules
for fixing up the tree?● How on earth did anyone come up with
red/black trees in the first place?
![Page 80: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/80.jpg)
B-Trees
![Page 81: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/81.jpg)
Generalizing BSTs
● In a binary search tree, each node stores a single key.
● That key splits the “key space” into two pieces, and each subtree stores the keys in those halves.
2
-1 4
-2 0 63
(-∞, 2) (2, +∞)
![Page 82: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/82.jpg)
Generalizing BSTs
● In a multiway search tree, each node stores an arbitrary number of keys in sorted order.
● A node with k keys splits the key space into k+1 regions, with subtrees for keys in each region.
0 3 5
(-∞, 0) (0, 3) (3, 5) (5, +∞)
![Page 83: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/83.jpg)
Generalizing BSTs
● In a multiway search tree, each node stores an arbitrary number of keys in sorted order.
● Surprisingly, it’s a bit easier to build a balanced multiway tree than it is to build a balanced BST. Let’s see how.
2
43
5 19 31 71 83
3 7 11 13 17 23 29 37 41 47 53 67 73 79 89 9759 61
46
45
![Page 84: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/84.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
![Page 85: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/85.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
31
![Page 86: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/86.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131
![Page 87: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/87.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 59
![Page 88: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/88.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926
![Page 89: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/89.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53
![Page 90: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/90.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58
![Page 91: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/91.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58 97
![Page 92: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/92.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58 93 97
![Page 93: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/93.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58 93 9723
![Page 94: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/94.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58 93 9723 84
![Page 95: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/95.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array.
4131 5926 53 58 93 9723 8462
![Page 96: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/96.jpg)
Balanced Multiway Trees
● In some sense, building a balanced multiway tree isn’t all that hard.
● We can always just cram more keys into a single node!
● At a certain point, this stops being a good idea – it’s basically just a sorted array. What does “balance” even mean here?
4131 5926 53 58 93 9723 8462
![Page 97: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/97.jpg)
Balanced Multiway Trees
● What could we do if our nodes get too big?
![Page 98: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/98.jpg)
Balanced Multiway Trees
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
![Page 99: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/99.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
![Page 100: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/100.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 8462
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
![Page 101: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/101.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
62
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
![Page 102: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/102.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
62
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
![Page 103: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/103.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
62
4131 5926 53 58 93 9723 84
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
![Page 104: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/104.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
62
4131 6226 53 58 93 9723 8459
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
![Page 105: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/105.jpg)
Balanced Multiway Trees
4131 5926 53 58 93 9723 84
62
4131 6226 53
58
93 9723 8459
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
![Page 106: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/106.jpg)
Balanced Multiway Trees
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
● Assume that, during an insertion, we add keys to the deepest node possible.
● How do these options compare?
4131 5926 53 58 93 9723 84
62
4131 6226 53
58
93 9723 8459
![Page 107: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/107.jpg)
Balanced Multiway Trees
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
● Assume that, during an insertion, we add keys to the deepest node possible.
● How do these options compare?
4131 5926 53 58 93 9723 84
62
4131 6226 53
58
93 9723 8459
Think about this for a bit,but don’t post anything
in chat just yet. 😃
Think about this for a bit,but don’t post anything
in chat just yet. 😃
![Page 108: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/108.jpg)
Balanced Multiway Trees
● What could we do if our nodes get too big?
● Option 1: Push the new key down into its own node.
● Option 2: Split big nodes in half, kicking the middle key up.
● Assume that, during an insertion, we add keys to the deepest node possible.
● How do these options compare?
4131 5926 53 58 93 9723 84
62
4131 6226 53
58
93 9723 8459
Now, private chat me yourthoughts. Not sure? Just
answer “??” 😃
Now, private chat me yourthoughts. Not sure? Just
answer “??” 😃
![Page 109: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/109.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 99 50 4020 30 3531 39 3332 34
![Page 110: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/110.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10
99 50 4020 30 3531 39 3332 34
![Page 111: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/111.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 99
50 4020 30 3531 39 3332 34
![Page 112: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/112.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
4020 30 3531 39 3332 34
![Page 113: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/113.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9920
40
50
30 3531 39 3332 34
![Page 114: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/114.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9920
40
50
30 3531 39 3332 34
![Page 115: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/115.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
20
40 30 3531 39 3332 34
![Page 116: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/116.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
20
40 30 3531 39 3332 34
![Page 117: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/117.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
2020 40
30 3531 39 3332 34
![Page 118: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/118.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39 3332 34
![Page 119: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/119.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
35
31
39 3332 34
![Page 120: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/120.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
35
31
39 3332 34
![Page 121: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/121.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
31
3539 3332 34
![Page 122: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/122.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
31
3539 3332 34
![Page 123: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/123.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3131 39
35 3332 34
![Page 124: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/124.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
3332 34
![Page 125: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/125.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
33
32
34
![Page 126: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/126.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
33
32
34
![Page 127: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/127.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
32
33 34
![Page 128: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/128.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
32
33 34
![Page 129: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/129.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
3232 33
34
![Page 130: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/130.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.
Option 2: Split big nodes, kicking values higher up.
Keeps the tree balanced.
Keeps most nodes near the bottom.
10 9950
3020 40
3531 39
3332 34
![Page 131: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/131.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 99 50 4020 30 3531 39 3332 34
![Page 132: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/132.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
99 50 4020 30 3531 39 3332 34
![Page 133: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/133.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 99
50 4020 30 3531 39 3332 34
![Page 134: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/134.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 50 99
4020 30 3531 39 3332 34
![Page 135: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/135.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 50 9920
40 30 3531 39 3332 34
![Page 136: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/136.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 50 9920
40 30 3531 39 3332 34
![Page 137: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/137.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
40 30 3531 39 3332 34
![Page 138: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/138.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
40 30 3531 39 3332 34
![Page 139: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/139.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
40 30 3531 39 3332 34
![Page 140: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/140.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
40 30 3531 39 3332 34
![Page 141: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/141.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920 40
30 3531 39 3332 34
![Page 142: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/142.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920 30 40
3531 39 3332 34
![Page 143: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/143.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920 30 40
3531 39 3332 34
![Page 144: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/144.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
40
3531 39 3332 34
![Page 145: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/145.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
40
3531 39 3332 34
![Page 146: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/146.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031
3539 3332 34
![Page 147: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/147.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 39
35 3332 34
![Page 148: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/148.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35 39
3332 34
![Page 149: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/149.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35 39
3332 34
![Page 150: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/150.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35
39
3332 34
![Page 151: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/151.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35
39
3332 34
![Page 152: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/152.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35
39
32
33 34
![Page 153: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/153.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35
39
32 33
34
![Page 154: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/154.jpg)
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10
50
9920
30
4031 35
39
32 33
34
![Page 155: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/155.jpg)
5030 3933
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
34
![Page 156: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/156.jpg)
5030 3933
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
34
![Page 157: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/157.jpg)
5030
39
33
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
34
![Page 158: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/158.jpg)
5030
39
33
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
34
![Page 159: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/159.jpg)
5030
39
33
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
Each existing node’s depth just increased
by one.
Each existing node’s depth just increased
by one.
34
![Page 160: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/160.jpg)
5030
39
33
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532
34
![Page 161: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/161.jpg)
5030
39
33
Balanced Multiway Trees
● Option 1: Push keys down into new nodes.
● Simple to implement.● Can lead to tree
imbalances.● Option 2: Split big
nodes, kicking keys higher up.
● Keeps the tree balanced.
● Slightly trickier to implement.
10 9920 4031 3532 34
![Page 162: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/162.jpg)
Balanced Multiway Trees
● General idea: Cap the maximum number of keys in a node. Add keys into leaves. Whenever a node gets too big, split it and kick one key higher up the tree.
● Advantage 1: The tree is always balanced.
● Advantage 2: Insertions and lookups are pretty fast.
5030
39
33
10 9920 4031 3532 34
![Page 163: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/163.jpg)
Balanced Multiway Trees
● We currently have a mechanical description of how these balanced multiway trees work:
● Cap the size of each node.● Add keys into leaves.● Split nodes when they get too big and propagate the
splits upward.● We currently don’t have an operational definition of
how these balanced multiway trees work.
● e.g. “A Cartesian tree for an array is a binary tree that’s a min-heap and whose inorder traversal gives back the original array.”
![Page 164: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/164.jpg)
B-Trees
● A B-tree of order b is a multiway search tree where
● each node has between b-1 and 2b-1 keys, except the root, which may only have between 1 and 2b-1 keys;
● each node is either a leaf or has one more child than key; and
● all leaves are at the same depth.
● Different authors give different bounds on how many keys can be in each node. The ranges are often [b–1, 2b–1] or [b, 2b]. For the purposes of today’s lecture, we’ll use the range [b-1, 2b-1] for the key limits, just for simplicity.
… … …
![Page 165: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/165.jpg)
Analyzing B-Trees
![Page 166: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/166.jpg)
The Height of a B-Tree
● What is the maximum possible height of a B-tree of order b that holds n keys?
Intuition: The branching factor of the tree is at least b, so the
number of keys per level grows exponentially in b. Therefore,
we’d expect something along the lines of O(logb n).
Intuition: The branching factor of the tree is at least b, so the
number of keys per level grows exponentially in b. Therefore,
we’d expect something along the lines of O(logb n).
![Page 167: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/167.jpg)
The Height of a B-Tree
● What is the maximum possible height of a B-tree of order b that holds n keys?
1
b – 1
b – 1 b – 1
b – 1 b – 1
…
…… …
b – 1
b – 1 b – 1
b – 1 b – 1
…
…… …
1
2(b - 1)
2b(b - 1)
2b2(b - 1)
2bh-1(b - 1)
…
b – 1 b – 1 b – 1…
…
![Page 168: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/168.jpg)
The Height of a B-Tree
● Theorem: The maximum height of a B-tree of order b containing n keys is O(logb n).
● Proof: Number of keys n in a B-tree of height h is guaranteed to be at least
= 1 + 2(b – 1) + 2b(b – 1) + 2b2(b – 1) + … + 2bh-1(b – 1)
= 1 + 2(b – 1)(1 + b + b2 + … + bh-1)
= 1 + 2(b – 1)((bh – 1) / (b – 1))
= 1 + 2(bh – 1) = 2bh – 1.
Solving n = 2bh – 1 yields h = logb ((n + 1) / 2), so the height is O(logb n). ■
![Page 169: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/169.jpg)
Analyzing Efficiency
● Suppose we have a B-tree of order b.
● What is the worst-case runtime of looking up a key in the B-tree?
1 2 4 6 7 8 10 12 14 15 17 18 19 21 22 24 26
3 9 11 16 20 25
5 13 23
Formulate ahypothesis, butdon’t post inchat just yet.
Formulate ahypothesis, butdon’t post inchat just yet.
![Page 170: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/170.jpg)
Analyzing Efficiency
● Suppose we have a B-tree of order b.
● What is the worst-case runtime of looking up a key in the B-tree?
1 2 4 6 7 8 10 12 14 15 17 18 19 21 22 24 26
3 9 11 16 20 25
5 13 23
Private chat meyour best guess.
Not sure? Justanswer “??”. 😃
Private chat meyour best guess.
Not sure? Justanswer “??”. 😃
![Page 171: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/171.jpg)
Analyzing Efficiency
● Suppose we have a B-tree of order b.
● What is the worst-case runtime of looking up a key in the B-tree?
● Answer: It depends on how we do the search!
![Page 172: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/172.jpg)
Analyzing Efficiency
● To do a lookup in a B-tree, we need to determine which child tree to descend into.
● This means we need to compare our query key against the keys in the node.
● Question: How should we do this?
![Page 173: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/173.jpg)
Analyzing Efficiency
● Option 1: Use a linear search!
● Cost per node: O(b).
● Nodes visited: O(logb n).
● Total cost:
= O(b) · O(logb n)
= O(b logb n)
![Page 174: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/174.jpg)
Analyzing Efficiency
● Option 2: Use a binary search!
● Cost per node: O(log b).
● Nodes visited: O(logb n).
● Total cost:
= O(log b) · O(logb n)
= O(log b · logb n)
= O(log b · (log n) / (log b))
= O(log n). Intuition: We can’t do better than O(log n) for arbitrary data, because it’s the information-
theoretic minimum number of comparisons needed to find something in a sorted collection!
Intuition: We can’t do better than O(log n) for arbitrary data, because it’s the information-
theoretic minimum number of comparisons needed to find something in a sorted collection!
![Page 175: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/175.jpg)
Analyzing Efficiency
● Suppose we have aB-tree of order b.
● What is the worst-case runtime of inserting a key into the B-tree?
● Each insertion visits O(logb n) nodes, and in the worst case we have to split every node we see.
● Answer: O(b logb n).
![Page 176: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/176.jpg)
Analyzing Efficiency
● The cost of an insertion in a B-tree of order b is O(b logb n).
● What’s the best choice of b to use here?● Note that
= b logb n
= b (log n / log b)
= (b / log b) log n.● What choice of b minimizes b / log b?● Answer: Pick b = e.
Fun fact: This is the same time bound
you’d get if you used a b-ary heap instead of a binary heap for
a priority queue.
Fun fact: This is the same time bound
you’d get if you used a b-ary heap instead of a binary heap for
a priority queue.
![Page 177: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/177.jpg)
2-3-4 Trees
1 2 4 6 7 8 10 12 14 15 17 18 19 21 22 24 26
3 9 11 16 20 25
5 13 23
● A 2-3-4 tree is a B-tree of order 2. Specifically:
● each node has between 1 and 3 keys;
● each node is either a leaf or has one more child than key; and
● all leaves are at the same depth.
● You actually saw this B-tree earlier! It’s the type of tree from our insertion example.
![Page 178: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/178.jpg)
The Story So Far
● A B-tree supports● lookups in time O(log n), and● insertions in time O(b logb n).
● Picking b to be around 2 or 3 makes this optimal in Theoryland.● The 2-3-4 tree is great for that reason.
● Plot Twist: In practice, you most often see choices of b like 1,024 or 4,096.
● Question: Why would anyone do that?
![Page 179: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/179.jpg)
TheorylandIRL
![Page 180: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/180.jpg)
The Memory Hierarchy
![Page 181: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/181.jpg)
Memory Tradeoffs
● There is an enormous tradeoff between speed and size in memory.
● SRAM (the stuff registers are made of) is fast but very expensive:
● Can keep up with processor speeds in the GHz.● SRAM units can’t be easily combined together;
increasing sizes require better nanofabrication techniques (difficult, expensive!)
● Hard disks are cheap but very slow:
● As of 2021, you can buy a 4TB hard drive for about $70.● As of 2021, good disk seek times for magnetic drives are
measured in ms (about two to four million times slower than a processor cycle!)
![Page 182: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/182.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
![Page 183: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/183.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
256B - 8KB
16KB – 64KB
1MB - 4MB
4GB – 256GB
1TB+
Lots
0.25 – 1ns
1ns – 5ns
5ns – 25ns
25ns – 100ns
3 – 10ms
10 – 2000ms
L2 Cache
Main Memory
Hard Disk
Network (The Cloud)
Registers
L1 Cache
![Page 184: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/184.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
256B - 8KB
16KB – 64KB
1MB - 4MB
Lots
0.25 – 1ns
1ns – 5ns
5ns – 25ns
10 – 2000ms
L2 Cache
Network (The Cloud)
Registers
L1 Cache
Main Memory
Hard Disk
4GB – 256GB
1TB+
25ns – 100ns
3 – 10ms
![Page 185: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/185.jpg)
External Data Structures
● Suppose you have a data set that’s way too big to fit in RAM.● The data structure is on disk and read into RAM as needed.● Data from disk doesn’t come back one byte at a time, but
rather one page at a time.● Goal: Minimize the number of disk reads and writes, not the
number of instructions executed.
“Please give me 4KBstarting at location addr1”
1101110010111011110001…
![Page 186: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/186.jpg)
External Data Structures
● Suppose you have a data set that’s way too big to fit in RAM.● The data structure is on disk and read into RAM as needed.● Data from disk doesn’t come back one byte at a time, but
rather one page at a time.● Goal: Minimize the number of disk reads and writes, not the
number of instructions executed.
Calculate…Think…
Compute…
![Page 187: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/187.jpg)
External Data Structures
● Suppose you have a data set that’s way too big to fit in RAM.● The data structure is on disk and read into RAM as needed.● Data from disk doesn’t come back one byte at a time, but
rather one page at a time.● Goal: Minimize the number of disk reads and writes, not the
number of instructions executed.
“Please give me 4KBstarting at location addr2”
001101010001010001010001…
![Page 188: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/188.jpg)
Analyzing B-Trees
● Suppose we tune b so that each node in the B-tree fits inside a single disk page.
● We only care about the number of disk pages read or written.● It’s so much slower than RAM that it’ll dominate the
runtime.● Question: What is the cost of a lookup in a B-tree
in this model?
Answer: The height of the tree, O(logb n).
● Question: What is the cost of inserting into aB-tree in this model?
Answer: The height of the tree, O(logb n).
![Page 189: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/189.jpg)
Analyzing B-Trees
● Suppose we tune b so that each node in the B-tree fits inside a single disk page.
● We only care about the number of disk pages read or written.● It’s so much slower than RAM that it’ll dominate the
runtime.● Question: What is the cost of a lookup in a B-tree
in this model?● Answer: The height of the tree, O(logb n).
● Question: What is the cost of inserting into aB-tree in this model?● Answer: The height of the tree, O(logb n).
![Page 190: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/190.jpg)
External Data Structures
● Because B-trees have a huge branching factor, they're great for on-disk storage.
● Disk block reads/writes are slow compared to CPU operations.
● The high branching factor minimizes the number of blocks to read during a lookup.
● Extra work scanning inside a block offset by these savings.● Major use cases for B-trees and their variants (B+-trees,
H-trees, etc.) include
● databases (huge amount of data stored on disk);● file systems (ext4, NTFS, ReFS); and, recently,● in-memory data structures (due to cache effects).
![Page 191: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/191.jpg)
Analyzing B-Trees
● The cost model we use will change our overall analysis.
● Cost is number of operations:
O(log n) per lookup, O(b logb n) per insertion.
● Cost is number of blocks accessed:
O(logb n) per lookup, O(logb n) per insertion.
● Going forward, we’ll use operation counts as our cost model, though looking at caching effects of data structures would make for an awesome final project!
![Page 192: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/192.jpg)
The Story So Far
● We’ve just built a simple, elegant, balanced multiway tree structure.
● We can use them as balanced trees in main memory (2-3-4 trees).
● We can use them to store huge quantities of information on disk (B-trees).
● We’ve seen that different cost models are appropriate in different situations.
![Page 193: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/193.jpg)
So... red/black trees?
![Page 194: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/194.jpg)
Red/Black Trees● A red/black tree is a BST with
the following properties:● Every node is either red or black.● The root is black.● No red node has a red child.● Every root-null path in the tree
passes through the same number of black nodes.
After we hoist red nodes into their parents:
Each “meta node” has 1, 2, or 3 keys in it. (No red node has a red child.)
Each “meta node” is either a leaf or has one more key than node. (Root-null path property.)
Each “meta leaf” is at the same depth. (Root-null path property.)
7
3
5 11
1
2 4
6
8
9
10
12
![Page 195: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/195.jpg)
Red/Black Trees● A red/black tree is a BST with
the following properties:● Every node is either red or black.● The root is black.● No red node has a red child.● Every root-null path in the tree
passes through the same number of black nodes.
● After we hoist red nodes into their parents:● Each “meta node” has 1, 2, or 3
keys in it. (No red node has a red child.)
● Each “meta node” is either a leaf or has one more child than key. (Root-null path property.)
● Each “meta leaf” is at the same depth. (Root-null path property.)
7
3 5 11
1 2 4 6 8 9 10 12
This is a2-3-4 tree!
This is a2-3-4 tree!
![Page 196: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/196.jpg)
Data Structure Isometries
● Red/black trees are an isometry of 2-3-4 trees; they represent the structure of 2-3-4 trees in a different way.
● Many data structures can be designed and analyzed in the same way.
● Huge advantage: Rather than memorizing a complex list of red/black tree rules, just think about what the equivalent operation on the corresponding 2-3-4 tree would be and simulate it with BST operations.
![Page 197: Tries and Suffix Trees - Stanford Universityweb.stanford.edu/class/cs166/lectures/02/Slides02.pdf · 2020. 4. 14. · Where We’re Going Today, we’ll cover tries and suffix trees,](https://reader036.vdocuments.us/reader036/viewer/2022071412/610a090329e3af5d8d17eb0d/html5/thumbnails/197.jpg)
Next Time
● Deriving Red/Black Trees● Figuring out rules for red/black trees using
our isometry.● Tree Rotations
● A key operation on binary search trees.● Augmented Trees
● Building data structures on top of balanced BSTs.