SlideShare a Scribd company logo
1 of 436
Download to read offline
Data Structures and Algorithms – COMS21103
Dynamic Search Structures
Self-balancing Trees and Skip Lists
Benjamin Sach
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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)
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
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)
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
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. . .
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)
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)
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
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
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
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
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
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
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!
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. . .
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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)
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)
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)
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)
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)
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
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
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?
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
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
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)
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
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
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
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
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?
Part one
Self-balancing trees
inspired by slides by Inge Li Gørtz
in turn inspired by slides by Kevin Wayne
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-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-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
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
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
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-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)
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-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
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
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
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-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
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
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
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
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-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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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?
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
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)
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?
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 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)
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)
The INSERT operation
25 261 3 5 12 14
1424
19 227 10
2 4 6 13 15
To perform INSERT(x, k),
11 18
17
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
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
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
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
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
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
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
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
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)
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)
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)
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)
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
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
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
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)
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)
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)
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)
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,
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, ???
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
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
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
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
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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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. . .
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)
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)
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?
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
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
The INSERT operation
2 6 9
25 261 3 12 14
1424
19 2216 175
13 15
107 8
INSERT(x, 20)
184
11
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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)
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)
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)
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,
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,
???
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
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
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
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
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
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
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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. . .
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?
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
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
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
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
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
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?
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
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)
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)
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)
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)
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
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
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
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
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
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
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
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
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
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
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
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?
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
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
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
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
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?
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
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 :)
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
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
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists

More Related Content

What's hot

What's hot (19)

Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
Problem Solving with Algorithms and Data Structure - Lists
Problem Solving with Algorithms and Data Structure - ListsProblem Solving with Algorithms and Data Structure - Lists
Problem Solving with Algorithms and Data Structure - Lists
 
Lecture2
Lecture2Lecture2
Lecture2
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture4
Lecture4Lecture4
Lecture4
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
Team 3
Team 3Team 3
Team 3
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
 
Studying geneva real estate prices using perl grammars
Studying geneva real estate prices using perl grammarsStudying geneva real estate prices using perl grammars
Studying geneva real estate prices using perl grammars
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
 
Sorting
SortingSorting
Sorting
 
Groovy
GroovyGroovy
Groovy
 
Stacks
StacksStacks
Stacks
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Binary search
Binary searchBinary search
Binary search
 
Introduction to R for Data Science :: Session 3
Introduction to R for Data Science :: Session 3Introduction to R for Data Science :: Session 3
Introduction to R for Data Science :: Session 3
 

Viewers also liked

Campionato di pallavolo 2015/2016 - Comunicato n°10
Campionato di pallavolo 2015/2016 -  Comunicato n°10Campionato di pallavolo 2015/2016 -  Comunicato n°10
Campionato di pallavolo 2015/2016 - Comunicato n°10Giuliano Ganassi
 
Dedicated sever collocation
Dedicated sever collocationDedicated sever collocation
Dedicated sever collocationmatthewjames95
 
Presentacioncarlosabraham6
Presentacioncarlosabraham6Presentacioncarlosabraham6
Presentacioncarlosabraham6otto fernandez
 
Product update 2015 dashboards
Product update 2015 dashboardsProduct update 2015 dashboards
Product update 2015 dashboardsPractice Fusion
 
Television & media as a medium for news
Television & media as a medium for newsTelevision & media as a medium for news
Television & media as a medium for newsSatish Sojitra
 
Getting Fit for the Future: Community Hospitals in a Time of Transition
Getting Fit for the Future: Community Hospitals in a Time of TransitionGetting Fit for the Future: Community Hospitals in a Time of Transition
Getting Fit for the Future: Community Hospitals in a Time of Transitionathenahealth
 
Capas separadoras do 2º período
Capas separadoras do 2º períodoCapas separadoras do 2º período
Capas separadoras do 2º períodoSónia Rodrigues
 
Foxtrot Division Capabilities Collection
Foxtrot Division Capabilities Collection Foxtrot Division Capabilities Collection
Foxtrot Division Capabilities Collection Jeff Hunter
 

Viewers also liked (10)

Kays
KaysKays
Kays
 
Vipin bharti
Vipin bhartiVipin bharti
Vipin bharti
 
Campionato di pallavolo 2015/2016 - Comunicato n°10
Campionato di pallavolo 2015/2016 -  Comunicato n°10Campionato di pallavolo 2015/2016 -  Comunicato n°10
Campionato di pallavolo 2015/2016 - Comunicato n°10
 
Dedicated sever collocation
Dedicated sever collocationDedicated sever collocation
Dedicated sever collocation
 
Presentacioncarlosabraham6
Presentacioncarlosabraham6Presentacioncarlosabraham6
Presentacioncarlosabraham6
 
Product update 2015 dashboards
Product update 2015 dashboardsProduct update 2015 dashboards
Product update 2015 dashboards
 
Television & media as a medium for news
Television & media as a medium for newsTelevision & media as a medium for news
Television & media as a medium for news
 
Getting Fit for the Future: Community Hospitals in a Time of Transition
Getting Fit for the Future: Community Hospitals in a Time of TransitionGetting Fit for the Future: Community Hospitals in a Time of Transition
Getting Fit for the Future: Community Hospitals in a Time of Transition
 
Capas separadoras do 2º período
Capas separadoras do 2º períodoCapas separadoras do 2º período
Capas separadoras do 2º período
 
Foxtrot Division Capabilities Collection
Foxtrot Division Capabilities Collection Foxtrot Division Capabilities Collection
Foxtrot Division Capabilities Collection
 

Similar to Self-balancing Trees and Skip Lists

introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptxHammadTariq51
 
Ds session 8
Ds   session 8Ds   session 8
Ds session 8prags_nie
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptxTekle12
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
(5) collections algorithms
(5) collections algorithms(5) collections algorithms
(5) collections algorithmsNico Ludwig
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 

Similar to Self-balancing Trees and Skip Lists (20)

Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Ds session 8
Ds   session 8Ds   session 8
Ds session 8
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
(5) collections algorithms
(5) collections algorithms(5) collections algorithms
(5) collections algorithms
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
Data structures
Data structuresData structures
Data structures
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
arrays in c
arrays in carrays in c
arrays in c
 

More from Benjamin Sach

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASBenjamin Sach
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASBenjamin Sach
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsBenjamin Sach
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsBenjamin Sach
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range SearchingBenjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesBenjamin Sach
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistanceBenjamin Sach
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common AncestorBenjamin Sach
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum QueriesBenjamin Sach
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysBenjamin Sach
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesBenjamin Sach
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingBenjamin Sach
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingBenjamin Sach
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Benjamin Sach
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmBenjamin Sach
 

More from Benjamin Sach (20)

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 
Hashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect HashingHashing Part Two: Static Perfect Hashing
Hashing Part Two: Static Perfect Hashing
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
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?
  • 66. Part one Self-balancing trees inspired by slides by Inge Li Gørtz in turn inspired by slides by Kevin Wayne
  • 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)
  • 106. The INSERT operation 25 261 3 5 12 14 1424 19 227 10 2 4 6 13 15 To perform INSERT(x, k), 11 18 17
  • 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
  • 162. The INSERT operation 2 6 9 25 261 3 12 14 1424 19 2216 175 13 15 107 8 INSERT(x, 20) 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