Professional Documents
Culture Documents
CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees: Mark Redekopp David Kempe
CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees: Mark Redekopp David Kempe
CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees: Mark Redekopp David Kempe
CSCI 104
B-Trees (2-3, 2-3-4) and
Red/Black Trees
Mark Redekopp
David Kempe
2
An example of B-Trees
2-3 TREES
3
Definition
• 2-3 Tree is a tree where a 2 Node
2 4
• Following the line of reasoning…
– All leaves at the same level with
internal nodes having at least 2 a valid 2-3 tree
50 20 30
20 60
10 25 50 60
10 30
25
7
•
2-3 Insertion Algorithm
Key: Since all leaves must be at the same level ("leaves always have their
feet on the ground"), insertion causes the tree to "grow upward"
• To insert a value,
– 1. walk the tree to a leaf using your search approach
– 2a. If the leaf is a 2-node (i.e.1 value), add the new value to that node
– 2b. Else break the 3-node into two 2-nodes with the smallest value as the left,
biggest as the right, and median value promoted to the parent with smallest
and biggest node added as children of the parent
– Repeat step 2(a or b) for the parent Key: Any time a node accumulates 3 values,
split it into single valued nodes (i.e. 2-nodes)
• Insert 60, 20, 10, 30, 25, 50, 80 and promote the median
20 20
60 20 60 10 20 60 10 60 10 30 60
8
Add 25 Add 50
20 20 30 20 30
10 2530 60 10 25 60 10 25 50 60
9
Add 80 30
20 30 20 30 60 20 60
10 25 50 6080 10 25 50 80 10 25 50 80
10
30
20 60
10 25 50 80
11
30 30 30
20 60 20 60 20 60 90
10 25 50 80 90 10 25 50 80 9091 10 25 50 80 91
12
30
20 60 90
10 25 50 80 91 92
13
30 30 30 90
20 60 90 20 60 90 92 20 60 92
10 25 50 80 91 92 93 10 25 50 80 91 93 10 25 50 80 91 93
14
Note
• 2-3 tree removal may be skipped due to time
constraints
15
•
2-3 Tree Removal
Key: 2-3 Trees must remain "full" (leaf nodes all at the same level)
• Remove Another key: Want to get item to remove
down to a leaf and then work up the tree
– 1. Find data item to remove
– 2. If data item is not in a leaf node, find in-order successor (which is in a leaf
node) and swap values (it's safe to put successor in your location)
– 3. Remove item from the leaf node
– 4. If leaf node is now empty, call fixTree(leafNode)
• fixTree(n)
– If n is root, delete root and return
– Let p be the parent of n
– If a sibling of n has two items
• Redistribute items between n, sibling, and p and move any appropriate child from
sibling to n
– Else
• Choose a sibling, s, of n and bring an item from p into s redistributing any children of n
to s
• Remove node n
• If parent is empty, fixTree(p)
16
Remove Cases
Redistribute 1 Redistribute 2
P L
P L
S L - S P
S L - S P
a b c d a b c d
Merge 1 Merge 2
L P
S - S L S - S P
a b c a b c
Remove 60
Remove Examples Remove 80
Key: Keep all your
30 feet (leaves) on the 30
ground (on the
bottom row)
20 60 20 60 90
10 25 50 80 90 10 25 50 80 91
30 30
Not a leaf node so
20 80 swap w/ 20 60 90
successor at leaf Can't just delete
because a 3-
node would
10 25 50 60 90 10 25 50 - 91 have only 2
children
30 30
Since 2 items at Rotate 60
20 80 leaf, just 20 90 down into 50 to
remove 60 make a 3-node
at the leaf and
2-node parent
10 25 50 90 10 25 50 60 91
18
Remove Cases
Redistribute 1 Redistribute 2
P L
P L
S L - S P
S L - S P
a b c d a b c d
Merge 1 Merge 2
P P
S - S P S - S P
a b c a b c
Empty root -
S L S L
a b c a b c
19
Remove 80
Remove Examples
30 -
Rotate parent
down and empty
20 80 20 30 node up, then
recurse
10 25 50 90 10 25 50 90
10 25 50 - 10 25 50 90
30 Rotate parent
down and empty
node up, then 20 30
20 - recurse
10 25 50 90
10 25 50 90
20
Remove Cases
Redistribute 1 Redistribute 2
P L
P L
S L - S P
S L - S P
a b c d a b c d
Merge 1 Merge 2
P P
S - S P S - S P
a b c a b c
Empty root -
S L S L
a b c a b c
21
Remove 30
Remove Exercise 1
30 50 Step 1: Not a leaf,
so swap with
20 80 20 80
successor
10 25 50 90 10 25 30 90
50 Step 2: Remove
item from node
20 80
10 25 90
10 25 80 90
Step 4: Merge
values
20 50
10 25 80 90
10 25 80 90
23
Remove 50
Remove Exercise 2
30 Step 1: It’s a leaf
node, so no need to
20 70 90
find successor.
Remove the item
10 15 25 50 75 95 from node.
30 Step 2: Since no 3-
node children, push
20 90
a value of parent
into a child.
10 15 25 70 75 95
10 15 25 70 75 95
24
Remove Cases
Redistribute 1 Redistribute 2
P L
P L
S L - S P
S L - S P
a b c d a b c d
Merge 1 Merge 2
P P
S - S P S - S P
a b c a b c
Empty root -
S L S L
a b c a b c
25
Insert 12
Insertion Exercise 1
30
20 70 90
10 15 25 50 75 95
26
Insert 23
Insertion Exercise 2
35
20 70 90
10 15 25 30 50 75 95
27
Insert 39
Insertion
40 100
Exercise 3
25 30 70 90 110
10 28 33 37 50 75 95 105 120
28
Removal Exercise 4
Remove 10 40 100
25 30 70 90 110
10 28 33 37 50 75 95 105 120
29
Removal Exercise 5
Remove 40 40 100
25 30 70 90 110
10 28 33 37 50 75 95 105 120
30
Remove 30
Removal Exercise 6
30
20 70 90
10 15 25 50 75 95
31
Other Resources
• http://www.cs.usfca.edu/~galles/visualization
/BTree.html
32
Definition
• 2-3-4 trees are very much like 2-3 trees but a 2 Node a 3 Node
a 2 Node a 3 Node
_ _ _
a 4 Node
_ _ _
34
m l r
Search Tree
• 4 Node:
– Left subtree nodes are < l < > <
>l
&&
>
m m l r
– Middle-left subtree > l and < r <r
l m r
>l >m
< >
&& &&
l r
<m <r
35
20 20
60 20 60 10 20 60 10 60 10 30 60
36
20 20 20 30
50
10 25 30 60 10 25 30 60 10 25 50 60
37
Add 80
20 30 20 30
80
10 25 50 60 10 25 50 60 80
38
20 30
10 25 50 60 80
39
Add 58
2-3-4 Insertion Exercise 2
20 30 60
10 25 50 55 80
40
Add 57
2-3-4 Insertion Exercise 3
30
20 60
10 25 50 55 58 80
41
Resulting Tree
2-3-4 Insertion Exercise 3
30
20 55 60
10 25 50 57 58 80
42
B-Trees
• 2-3-4 trees are just instances of a more general data
structure known as B-Trees
• Define minimum number of children (degree) for
non-leaf nodes, d
– Non-root nodes must have at least d-1 keys and d children
– All nodes must have at most 2d-1 keys and 2d children
– 2-3-4 Tree (d=2)
• Used for disk-based storage and indexing with
large value of d to account for large random-
access lookup time but fast sequential access
time of secondary storage
43
B Tree Resources
• https://www.cs.usfca.edu/~galles/visualizatio
n/BTree.html
• http://ultrastudio.org/en/2-3-4_tree
44
m m
a 4 Node m
S = Small
M = Median
L = Large
s m l s l
a b c d
l s
a 3 Node
s l s c
or a
l
a b b c
47
20 30 20
Equivalent
RB-Tree 10 30
10 25 50 60 80
25 60
50 80
48
Red-Black Insertion
• Insertion Algorithm:
– 1. Insert node into normal BST location (at a leaf
location) and color it RED
– 2a. If the node's parent is black (i.e. the leaf used
to be a 2-node) then DONE (i.e. you now have
what was a 3- or 4-node)
– 2b. Else perform fixTree transformations then
repeat step 2 on the parent or grandparent
(whoever is red) Insert 10
– recoloring or 30
aunt/
parent
– 1 or 2 rotations and recoloring uncle
20 40
• Which case of fixTree you perform depends x
on the color of the new node's "aunt/uncle" 10
50
fixTree Cases
1.
G G G
Recolor
P U P U N P G U N P U
N c N c
a b a b
2. G
G G
Recolor
P U P U PN G U P N U
a N a N
b c b c
Note: For insertion/removal
algorithm we consider non-
3.
existent leaf nodes as black
Recolor R R nodes
Root
51
4.
fixTree Cases
1 Rotate / G Right P P G N P G
Recolor
rotate of
P U P,G N G
N c U a b c U
N c a b c U
a b
a b
5. G G N
2 Rotates / Right
Recolor Left rotate
P U of N,P
N U rotate of P G
N,G
a c & Recolor a b c
N P U
b c a b
P G P N G
a N U a b c U
b c www.cse.ohio-state.edu/~gurari/course/cis680Ch11.html
52
Insertion
• Insert 10, 20, 30, 15, 25, 12, 5, 3, 8
Empty Insert 10 Insert 20 Insert 30 Case 4: Left rotate
Violates consec. reds and recolor
10 10 10 20
20 20 10 30
30
Insert 15 Insert 25
20 20 20 20
10 30 10 30 10 30 10 30
Case 2: Case 3:
15 15 15 15 25
Recolor Recolor
root
53
Insertion
• Insert 10, 20, 30, 15, 25, 12, 5, 3, 8
Insert 12
20 20 20
10 30 10 30 12 30
15 25 12 25 10 15 25
12 30 12 30
10 15 25 10 15 25
Case 1: Recursive call "fix" on
5 Recolor 5 12 but it's parent is
black so we're done
54
Insertion
• Insert 10, 20, 30, 15, 25, 12, 5, 3, 8
Insert 3
20
20
12 30
12 30
10 15 25
5 15 25
5
Case 4: Rotate 3 10
3
55
Insertion
• Insert 10, 20, 30, 15, 25, 12, 5, 3, 8
Insert 8
20 20 12
12 30 12 30 5 20
5 15 25 5 15 25 3 10 15 30
3 10 3 10 8 25
Insertion Exercise 1
Insert 27
12
5 20
G
3 10 15 30
8
P
25
N 27
57
Insertion Exercise 1
Insert 27
12 12
5 20 5 20
G
3 10 15 30 3 10 15 27
8
P 8
25 25 30
N 27
This is case 5.
1. Left rotate around P
2. Right rotate around N
3. Recolor
58
Insertion Exercise 2
Insert 40
12
5 20
G
3 10 15 27
A P
8 25 30
N 40
59
Insertion Exercise 2
Insert 40
12 12
5 20 5 20
G
3 10 15 27 3 10 15 27
A P
8 25 30 8 25 30
N 40 40
Insertion Exercise 2
G
12 12
A P
5 20 5 20
N
3 10 15 27 3 10 15 27
8 25 30 8 25 30
40 40
Insertion Exercise 3
Insert 50 12
5 20
3 10 15 27
G
8 25 30
P
40
N
50
62
Insertion Exercise 3
Insert 50 12 12
5 20 5 20
3 10 15 27 3 10 15 27
G
8 25 30 8 25 40
P
40 30 50
N
50
Insertion Exercise 4
Insert 45
12
5 20
3 10 15 27
G
8 25 40
A P
30 50
45
N
64
Insertion Exercise 4
Insert 45
12 12
5 20 5 20
3 10 15 27 3 10 15 27
G
8 25 40 8 25 40
A P
30 50 30 50
45 45
N
Insertion Exercise 4
12
5
G
20
A P
3 10 15 27
8 25 40 N
30 50
45
66
Final Result
12
5 27
40
3 10 20
8 15 25 30 50
45
67
Insertion Exercise 5
Insert 9
12
5 27
G
40
3 10 20
P A 25 30 50
8 15
N
9 45
68
Insertion Exercise 5
12
5 27
40
3 9 20
8 10 15 25 30 50
45
69
RB TREE IMPLEMENTATION
71
Hints
• Implement private methods:
– findMyUncle()
– AmIaRightChild()
– AmIaLeftChild()
– RightRotate
– LeftRotate
• Need to change x's parent, y's parent, b's parent, x's right, y's left, x's
parent's left or right, and maybe root
(-inf, inf) (-inf, inf)
y Right rotate
x
of x,y
(-inf, y) (y, inf) (-inf, x) (x, inf)
x c a y
Left rotate (x, y) (y, inf)
(-inf, x) (x,y) of y,x
a b b c
72
Hints
• You have to fix the tree after insertion if…
• Watch out for traversing NULL pointers
– node->parent->parent
– However, if you need to fix the tree your grandparent…
• Cases break down on uncle's color
– If an uncle doesn't exist (i.e. is NULL), he is (color?)…
(-inf, inf) (-inf, inf)
y Right rotate
x
of x,y
(-inf, y) (y, inf) (-inf, x) (x, inf)
x c a y
Left rotate (x, y) (y, inf)
(-inf, x) (x,y) of y,x
a b b c
73
FOR PRINT
74
1.
fixTree Cases
G G G
Recolor
P U P U N P G U N P U
N c N c
a b a b
2. G
G G
Recolor
P U P U PN G U P N U
a N a N
b c b c
Note: For insertion/removal
algorithm we consider non-
3.
existent leaf nodes as black
Recolor R R nodes
Root
75
4.
fixTree Cases
1 Rotate / G Right P P G N P G
Recolor
rotate of
P U P,G N G
N c U a b c U
N c a b c U
a b
a b
5. G G N
2 Rotates / Right
Recolor Left rotate
P U of N,P
N U rotate of P G
N,G
a c & Recolor a b c
N P U
b c a b
P G P N G
a N U a b c U
b c www.cse.ohio-state.edu/~gurari/course/cis680Ch11.html