TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Self-balancing Trees and Skip Lists
1. Data Structures and Algorithms – COMS21103
Dynamic Search Structures
Self-balancing Trees and Skip Lists
Benjamin Sach
2. Dynamic Search Structures
A dynamic search structure,
Each element x must have a unique key - x.key
The following operations are supported:
DELETE(k) - deletes the (unique) element x with x.key = k
INSERT(x, k) - inserts x with key k = x.key
FIND(k) - returns the (unique) element x with x.key = k
(or reports that it doesn’t exist)
stores a set of elements
(or reports that it doesn’t exist)
3. Dynamic Search Structures
A dynamic search structure,
Each element x must have a unique key - x.key
The following operations are supported:
DELETE(k) - deletes the (unique) element x with x.key = k
INSERT(x, k) - inserts x with key k = x.key
FIND(k) - returns the (unique) element x with x.key = k
(or reports that it doesn’t exist)
stores a set of elements
(or reports that it doesn’t exist)
We would also like it to support (among others):
PREDECESSOR(k) - returns the (unique) element x
with the largest key such that x.key < k
RANGEFIND(k1, k2) - returns every element x with k1 x.key k2
4. Using a Linked List as a Dynamic Search Structure
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
5. Using a Linked List as a Dynamic Search Structure
We could implement a Dynamic Search Structure using an unsorted linked list:
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
6. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
7. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
8. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
9. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
10. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
11. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
12. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
13. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
14. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
15. Using a Linked List as a Dynamic Search Structure
5
Bob
5
Bob x
x.key
x
x.key
We could implement a Dynamic Search Structure using an unsorted linked list:
4
Dawn
4
Dawn
3
Alice
35
Emma
3
Alice
36
Emma
3
Alice
33
Alice
INSERT is very efficient,
- add the new item to the head of the list in O(1) time
8
Chris
7
Chris
FIND and DELETE are very inefficient, they take O(n) time
- we have to look through the entire linked list to find an item (in the worst case)
There are many ways in which we could implement a search structure. . .
but they aren’t all efficient
4
Dawn
4
Dawn
Let n denote the number of elements stored in the structure
- our goal is to implement a structure with operations which scale well as n grows
16. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
17. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
18. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
node
19. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
node
left subtree
(smaller keys)
20. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
node
left subtree
(smaller keys)
right subtree
(larger keys)
21. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
22. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
node
left subtree
(smaller keys)
right subtree
(larger keys)
23. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
24. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
25. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
26. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
27. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
28. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
29. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
21 > 20 so go right
30. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
21 > 20 so go right
31. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
32. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
33. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
FIND(21)
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
21 = 21 found it!
34. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
35. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
36. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
37. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
38. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
39. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
40. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
41. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
42. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
43. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
44. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
45. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
46. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
21 = 21 found it!
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
DELETE(21)
47. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 21 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
21 = 21 found it!
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
now delete it!
DELETE(21)
48. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
21 < 27 so go left
21 > 20 so go right
21 < 23 so go left
21 = 21 found it!
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
now delete it!
DELETE(21)
49. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
50. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
INSERT(62)
51. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
INSERT(62)
52. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
INSERT(62)
53. Binary Search Trees
35
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
INSERT(62)
54. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
INSERT(62)
55. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
Recall that in a binary search tree, for any node
- all the nodes in the left subtree have smaller keys
- all the nodes in the right subtree have larger keys
We perform a FIND operation by following a path from the root. . .
this takes O(h) time - where h is the height
h
The INSERT and DELETE operations are similar and also take O(h) time
56. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
57. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
How big is h?
58. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
How big is h?
n is the number
of elements
59. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
60. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
61. Binary Search Trees
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
h
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
In particular, each INSERT could increase h by one
62. Binary Search Trees
63
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
In particular, each INSERT could increase h by one
h
63. Binary Search Trees
63
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
64
In particular, each INSERT could increase h by one
h
64. Binary Search Trees
63
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
64
In particular, each INSERT could increase h by one
h
65. Binary Search Trees
63
6235
A classic choice for a dynamic search structure is
a binary search tree. . .
key
27
20 36
15 23 31 61
16 26
How big is h?
It might be as small as log2 n (if the tree is perfectly balanced)
n is the number
of elements
but it might be as big as n (if the tree is completely unbalanced)
64
In particular, each INSERT could increase h by one
h
how can we overcome this?
67. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
68. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
69. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
2-node
70. 2-3-4 Trees
11 18
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
3-node
71. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
4-node
72. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
73. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
74. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
75. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
2-node
76. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
2-node
smaller than 24
77. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
2-node
smaller than 24
bigger than 24
78. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
79. 2-3-4 Trees
11 18
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
3-node
80. 2-3-4 Trees
11 18
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
3-node
smaller than 11
81. 2-3-4 Trees
11 18
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
3-node
smaller than 11 bigger than 18
82. 2-3-4 Trees
11 18
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
3-node
smaller than 11 bigger than 18
between 11 and 18
83. 2-3-4 Trees
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
84. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
4-node
85. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
4-node
smaller than 2
86. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
4-node
smaller than 2
between 2 and 4
87. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
4-node
smaller than 2
between 2 and 4 between 4 and 6
88. 2-3-4 Trees
2 4 6
Key idea: Nodes can have between 2 and 4 children (hence the name)
Perfect balance - every path from the root to a leaf has the same length
(always, all the time)
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
2-node: 2 children and 1 key
3-node: 3 children and 2 keys
4-node: 4 children and 3 keys
The are “dummy leaves” (they don’t do or contain anything)
Like in a binary search tree,
the keys held at a node determine
the contents of its subtrees
4-node
smaller than 2
between 2 and 4 between 4 and 6
bigger than 6
89. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
90. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
91. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
92. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
93. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
94. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
95. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
96. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
97. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
98. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
found it!
99. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
found it!
What is the time complexity of the FIND operation?
100. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
found it!
What is the time complexity of the FIND operation?
It’s O(h) again
h
101. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
found it!
What is the time complexity of the FIND operation?
It’s O(h) again
h
(each step down the path takes O(1) time)
102. The FIND operation
25 261 3 5 12 14
1424
19 227 10 317
2 4 6 13 15
11 18
we perform a FIND operation by following a path from the root. . .
Just like in a binary search tree,
12 is between 11 and 18 FIND(12)
descisions are made by inspecting the key(s) at the current node
and following the appropriate edge
12 is smaller than 13
found it!
What is the time complexity of the FIND operation?
It’s O(h) again
h
(each step down the path takes O(1) time)
What is the height, h of a 2-3-4 tree?
103. The height of a 2-3-4 tree
Perfect balance - every path from the root to a leaf has the same length
(we’ll justify this as we go along)
This implies that the height, h of a 2-3-4 tree with n nodes is
Worst case: log2 n (all 2-nodes)
Best case: log4 n =
log2 n
2 (all 4-nodes)
h is between 10 and 20 for a million nodes
( is an element)
104. The height of a 2-3-4 tree
Perfect balance - every path from the root to a leaf has the same length
(we’ll justify this as we go along)
This implies that the height, h of a 2-3-4 tree with n nodes is
Worst case: log2 n (all 2-nodes)
Best case: log4 n =
log2 n
2 (all 4-nodes)
h is between 10 and 20 for a million nodes
The time complexity of the FIND operation is O(h)
( is an element)
105. The height of a 2-3-4 tree
Perfect balance - every path from the root to a leaf has the same length
(we’ll justify this as we go along)
This implies that the height, h of a 2-3-4 tree with n nodes is
Worst case: log2 n (all 2-nodes)
Best case: log4 n =
log2 n
2 (all 4-nodes)
h is between 10 and 20 for a million nodes
( is an element)
The time complexity of the FIND operation is O(h) = O(log n)
107. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
11 18
17
108. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
INSERT(x, 16)
11 18
17
109. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
INSERT(x, 16)
11 18
17
110. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
INSERT(x, 16)
11 18
17
111. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
INSERT(x, 16)
11 18
17
112. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
INSERT(x, 16)
11 18
17
113. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
INSERT(x, 16)
11 18
16 17
114. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
115. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
116. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
117. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
118. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
119. The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
120. The INSERT operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
INSERT(x, 9)
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
121. The INSERT operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
122. The INSERT operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
123. The INSERT operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
124. The INSERT operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
125. The INSERT operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
126. The INSERT operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
Step 4: If the leaf is a 4-node,
127. The INSERT operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
Step 4: If the leaf is a 4-node, ???
128. The INSERT operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
Step 4: If the leaf is a 4-node, ??? We will make sure this never happens
129. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 9232 51
130. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 9232 51
BEFORE
AFTER
71
86
9232 51
41 63
131. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 9232 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
9232 51
41 63
132. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 92
these subtrees could have any size
32 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
9232 51
41 63
133. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 92
these subtrees could have any size
32 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
92
these subtrees haven’t changed
32 51
41 63
134. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 92
these subtrees could have any size
32 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
92
these subtrees haven’t changed
32 51
41 63
no path lengths have changed
135. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 92
these subtrees could have any size
32 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
92
these subtrees haven’t changed
32 51
41 63
no path lengths have changed
(if it was perfectly balanced, it still is)
136. SPLITTING 4-nodes
We can SPLITany 4-node into two 2-nodes
if it’s parent isn’t a 4-node
41 63
71 86 92
these subtrees could have any size
32 51
BEFORE
AFTER
The extra key is pushed up to the parent
(so it won’t work if the parent is a 4-node)
71
86
92
these subtrees haven’t changed
32 51
41 63
no path lengths have changed
(if it was perfectly balanced, it still is)
SPLIT takes O(1) time
137. The INSERT operation
25 261 3 12 14
1424
19 22
2 4 6
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
SPLIT 4-nodes as we go down
5
13 15
138. The INSERT operation
25 261 3 12 14
1424
19 22
2 4 6
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
5
13 15
139. The INSERT operation
25 261 3 12 14
1424
19 22
2 4 6
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
5
13 15
140. The INSERT operation
25 261 3 12 14
1424
19 22
2 4 6
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
5
13 15
141. The INSERT operation
25 261 3 12 14
1424
19 22
2 4 6
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
SPLIT this!
5
13 15
142. The INSERT operation
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
11 18
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4
SPLIT this!
5
13 15
143. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
SPLIT this!
5
13 15
144. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
SPLIT this!
5
13 15
145. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
5
13 15
146. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
5
13 15
147. The INSERT operation
7 9 10
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
5
13 15
148. The INSERT operation
7 9 10
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
7 9 10
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
SPLIT this!
5
13 15
149. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
2 6
4 11 18
SPLIT this!
9
75 10
13 15
150. The INSERT operation
6 9
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
SPLIT this!
2
4 11 18
6 9
75 10
13 15
151. The INSERT operation
6 9
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
2
4 11 18
6 9
75 10
13 15
152. The INSERT operation
6 9
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
2
4 11 18
6 9
75 10
13 15
153. The INSERT operation
6 9
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
154. The INSERT operation
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
INSERT(x, 8)
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
155. The INSERT operation
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
156. The INSERT operation
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
OK, one more thing. . .
157. The INSERT operation
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
OK, one more thing. . .
INSERT(x, 20)
158. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
OK, one more thing. . .
INSERT(x, 20)
159. The INSERT operation
4 11 18
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
2
4 11 18
6 9
5
13 15
107 8
OK, one more thing. . .
INSERT(x, 20)
what happens when we SPLIT the root?
160. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
5
13 15
107 8
OK, one more thing. . .
INSERT(x, 20)
what happens when we SPLIT the root?
184
11
161. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 22
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the leaf is a 2-node,
insert (x, k), converting it into a 3-node
16 17
Step 3: If the leaf is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
5
13 15
107 8
OK, one more thing. . .
INSERT(x, 20)
what happens when we SPLIT the root?
184
11
163. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 2216 175
13 15
107 8
INSERT(x, 20)
SPLITTING the root increases the height of the tree
and increases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
184
11
164. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 2216 175
13 15
107 8
INSERT(x, 20)
SPLITTING the root increases the height of the tree
and increases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
This is the only way INSERT can affect the length of paths
so it also maintains the perfect balance property
184
11
165. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 2216 175
13 15
107 8
INSERT(x, 20)
SPLITTING the root increases the height of the tree
and increases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
This is the only way INSERT can affect the length of paths
so it also maintains the perfect balance property
As each SPLIT takes O(1) time, overall INSERT takes O(log n) time
184
11
166. The INSERT operation
2 6 9
25 261 3 12 14
1424
19 2216 175
13 15
107 8
INSERT(x, 20)
As each SPLIT takes O(1) time, overall INSERT takes O(log n) time
184
11
Step 1: Search for the key k as if performing FIND(k).
To perform INSERT(x, k),
Step 2: If the bottom node is a 2-node,
insert (x, k), converting it into a 3-node
Step 3: If the bottom node is a 3-node,
insert (x, k), converting it into a 4-node
SPLIT 4-nodes as we go down
167. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
11 18
16 177 9 10
168. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
11 18
16 177 9 10
169. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
DELETE(16)
11 18
16 177 9 10
170. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
DELETE(16)
11 18
16 177 9 10
171. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
DELETE(16)
11 18
16 177 9 10
172. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
DELETE(16)
11 18
16 177 9 10
173. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
DELETE(16)
11 18
16 177 9 10
174. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
DELETE(16)
11 18
177 9 10
175. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
177 9 10
176. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
7 9 10
177. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
7 9 10
178. The DELETE operation
25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
7 9 10
179. The DELETE operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
7 9 10
180. The DELETE operation
7 9 10 25 261 3 5 12 14
1424
19 22
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
7 9 10
181. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
DELETE(9)
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
182. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
183. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
184. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
185. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
186. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
187. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
Step 4: If the leaf is a 2-node,
188. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
Step 4: If the leaf is a 2-node,
???
189. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
Step 4: If the leaf is a 2-node,
??? We will make sure this never happens
190. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
71
86
92
41 63
into a 4-node
191. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
71
86
92
41 63
into a 4-node
192. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
41 63
into a 4-node
193. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
41 63
This is the opposite of a
SPLIT operation
into a 4-node
194. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
these subtrees haven’t changed
41 63
This is the opposite of a
SPLIT operation
into a 4-node
195. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
these subtrees haven’t changed
41 63
no path lengths have changed
This is the opposite of a
SPLIT operation
into a 4-node
196. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
these subtrees haven’t changed
41 63
no path lengths have changed
(if it was perfectly balanced, it still is)
This is the opposite of a
SPLIT operation
into a 4-node
197. FUSING 2-nodes
We can FUSE two 2-nodes (with the same parent)
if that parent isn’t a 2-node
41 63
71 86 92
BEFORE
AFTER
The extra key is pulled down from the parent
(so it won’t work if the parent is a 2-node)
71
86
92
these subtrees haven’t changed
41 63
no path lengths have changed
(if it was perfectly balanced, it still is)
This is the opposite of a
SPLIT operation
FUSE takes O(1) time
into a 4-node
198. TRANSFERING keys
If there is a 2-node and a 3-node
(with the same parent)
we can perform a TRANSFER
71
8641 63
91 97
(even if the parent is the root)
199. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63 91
978671
91 97
(even if the parent is the root)
200. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63 91
978671
91 97
(even if the parent is the root)
201. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63
these subtrees haven’t changed
91
978671
91 97
(even if the parent is the root)
202. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
no path lengths have changed
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63
these subtrees haven’t changed
91
978671
91 97
(even if the parent is the root)
203. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
no path lengths have changed
(if it was perfectly balanced, it still is)
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63
these subtrees haven’t changed
91
978671
91 97
(even if the parent is the root)
204. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
no path lengths have changed
(if it was perfectly balanced, it still is)
TRANSFER takes O(1) time
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63
these subtrees haven’t changed
91
978671
91 97
(even if the parent is the root)
205. TRANSFERING keys
If there is a 2-node and a 3-node
BEFORE
AFTER
The keys have been rearranged
no path lengths have changed
(if it was perfectly balanced, it still is)
TRANSFER also works with a 2-node and a 4-node
TRANSFER takes O(1) time
(with the same parent)
we can perform a TRANSFER
71
8641 63
41 63
these subtrees haven’t changed
91
978671
91 97
(even if the parent is the root)
206. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
use FUSE and TRANSFER to convert 2-nodes as we go down
2 4 6
207. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
2 4 6
208. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
2 4 6
209. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
2 4 6
210. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
2 4 6
211. The DELETE operation
5 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
2 4 6
212. The DELETE operation
3 5 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
2 4 6
213. The DELETE operation
3 5 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
3 5
2 4 6
214. The DELETE operation
3 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
3 5
2 4 6
215. The DELETE operation
3 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
3 5
2 6
4
216. The DELETE operation
3 25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
3 54
2 6
217. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
FUSE this!
3 54
2 6
218. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
3 54
2 6
219. The DELETE operation
25 261 3 5 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
3 54
2 6
Now we can DELETE the 5
220. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
DELETE(5)
use FUSE and TRANSFER to convert 2-nodes as we go down
2 6
Now we can DELETE the 5
3 4
221. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
use FUSE and TRANSFER to convert 2-nodes as we go down
2 6
3 4
222. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
use FUSE and TRANSFER to convert 2-nodes as we go down
2 6
3 4
OK, one more thing. . .
223. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
use FUSE and TRANSFER to convert 2-nodes as we go down
2 6
3 4
OK, one more thing. . . what happens when we FUSE the root?
224. FUSING the root
FUSING the root can decrease the height of the tree
which in turn decreases the length of all root-leaf paths by one
71 92
We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .
we make an exception for the root
83root
71 92 fused root83
225. FUSING the root
FUSING the root can decrease the height of the tree
which in turn decreases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
71 92
We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .
we make an exception for the root
83root
71 92 fused root83
226. FUSING the root
FUSING the root can decrease the height of the tree
which in turn decreases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
This is the only way DELETE can affect the length of paths
so it also maintains the perfect balance property
71 92
We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .
we make an exception for the root
83root
71 92 fused root83
227. FUSING the root
FUSING the root can decrease the height of the tree
which in turn decreases the length of all root-leaf paths by one
- i.e every path from the root to a leaf has the same length
So it maintains the perfect balance property
This is the only way DELETE can affect the length of paths
so it also maintains the perfect balance property
As each FUSE or TRANSFER takes O(1) time, overall DELETE takes O(log n) time
71 92
We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .
we make an exception for the root
83root
71 92 fused root83
228. The DELETE operation
As each FUSE or TRANSFER takes O(1) time, overall DELETE takes O(log n) time
25 261 12 14
1424
19 227 10
13 15
Step 1: Search for the key k using FIND(k).
To perform DELETE(k) on a leaf (we’ll deal with other nodes later)
Step 2: If the leaf is a 3-node,
delete (x, k), converting it into a 2-node
11 18
17
Step 3: If the leaf is a 4-node,
delete (x, k), converting it into a 3-node
use FUSE and TRANSFER to convert 2-nodes as we go down
2 6
3 4
229. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
230. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
231. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
DELETE(11)
232. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
DELETE(11)
233. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
DELETE(11)
234. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
DELETE(11)
235. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
DELETE(11)
10 is the predecessor of 11
236. The DELETE operation
25 261 12 14
1424
19 227 10
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
DELETE(11)
10 is the predecessor of 11
237. The DELETE operation
7 25 261 12 14
1424
19 22
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
DELETE(11)
10 is the predecessor of 11
7 10
238. The DELETE operation
7 25 261 12 14
1424
19 22
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k
DELETE(11)
10 is the predecessor of 11
7 10
239. The DELETE operation
7 25 261 12 14
1424
19 22
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k
DELETE(11)
7 10
240. The DELETE operation
25 261 12 14
1424
19 22
13 15
11 18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k
DELETE(11)
7 10
241. The DELETE operation
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k
DELETE(11)
7
10
242. The DELETE operation
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k
7
10
243. The DELETE operation
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4
What if we want to DELETE something other than a leaf?
Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)
- that’s the element with the largest key k
such that k < k
Step 2: Call DELETE(k )
- fortunately k is always a leaf
Step 3: Overwrite k with another copy of k This also takes O(log n) time
7
10
244. 2-3-4 tree summary
A 2-3-4 is a data structure based on a tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4 7
10
245. 2-3-4 tree summary
A 2-3-4 is a data structure based on a tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4 7
10
Unfortunately, 2-3-4 trees are awkward to implement
because the nodes don’t all have the same number of children
246. 2-3-4 tree summary
A 2-3-4 is a data structure based on a tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
25 261 12 14
1424
19 22
13 15
18
17
2 6
3 4 7
10
Unfortunately, 2-3-4 trees are awkward to implement
because the nodes don’t all have the same number of children
So, what is used in practice?
247. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
51 53 5556 9
55 57
248. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
51 53 5556 9
55 57
249. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
All root-to-leaf paths have the same number of black nodes
51 53 5556 9
55 57
250. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
All root-to-leaf paths have the same number of black nodes
Red nodes cannot have red children
51 53 5556 9
55 57
251. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
All root-to-leaf paths have the same number of black nodes
Red nodes cannot have red children
51 53 5556 9
55 57
If these are used in practice, why did you waste our time with 2-3-4 trees?
252. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
All root-to-leaf paths have the same number of black nodes
Red nodes cannot have red children
51 53 5556 9
55 57
If these are used in practice, why did you waste our time with 2-3-4 trees?
1. 2-3-4 trees are conceptually much nicer
253. Red-Black tree summary
A Red-Black tree is a data structure based on a binary tree structure
which supports INSERT(x, k), FIND(k) and DELETE(k)
each of these operations takes worst case O(log n) time
4
52 58
The root is black
All root-to-leaf paths have the same number of black nodes
Red nodes cannot have red children
51 53 5556 9
55 57
If these are used in practice, why did you waste our time with 2-3-4 trees?
1. 2-3-4 trees are conceptually much nicer 2. they are secretly the same :)
254. 2-3-4 trees vs. Red-Black trees
Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)
The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree
(the details of the Red-Black tree operations are in CLRS Chapter 13)
4
52 58
51 53 5556 9
55 57
4 8
1 2 3 5 6 7 9
255. 2-3-4 trees vs. Red-Black trees
Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)
The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree
(the details of the Red-Black tree operations are in CLRS Chapter 13)
4
52 58
51 53 5556 9
55 57
4 8
1 2 3 5 6 7 9