Balanced Search Trees
2-3-4 trees red-black trees
References: Algorithms in Java (handout)
Balanced Search Trees 2-3-4 trees red-black trees References: - - PowerPoint PPT Presentation
Balanced Search Trees 2-3-4 trees red-black trees References: Algorithms in Java (handout) Balanced search trees Dynamic sets Search Insert Delete Maximum Minimum Successor(x) (find minimum element x)
References: Algorithms in Java (handout)
2
3
Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)
O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h)
5
S V K R M O X A C L N Q Y Z
smaller than K larger than R between K and R
B D F G J E
6
S V F G J K R C E M O X A D L N Q Y Z
7
between K and R smaller than M
S V F G J K R C E M O X A D L N Q Y Z
found L
8
between K and R smaller than M
S V F G J K R C E M O X A D L N Q Y Z
found L
9
10
smaller than K B not found smaller than C
11
smaller than K smaller than C B fits here
12
X not found larger than R larger than U
13
X fits here larger than R larger than W
larger than E H not found
14
smaller than K
larger than E H does not fit here!
15
smaller than K
16
H does not fit here
H does fit here!
17
F G J B G B F J
x y z v x y z v
F G J
x y z v
F
x y
J
z v
B D B D G
x y z v
B D G D B G
x y z v
root
18
not a 4-node not a 4-node 4-node
19
20
A-C E-J L-P R-V X-Z A-C E-J L-P R-V X-Z
could be huge unchanged
21
A-C I-J L-P R-V X-Z I-J L-P R-V X-Z
could be huge unchanged E-G
A-C E-G
22
23
24
S A B C E R H I N A B C E R H I N S U
Insert G Insert U Split
A B C E I R S U H N A B C E I R S U N G H A B C S U N G H E R I A B C S T U N G H E R I
Insert G Split Insert T
Insert U Insert G Insert T
25
A is minimum
26
Delete A
27
Delete B?
28
z w y x z w y x
A B C A C B
r s w z y x r s w z y x
A B C D E A B C D E
r s w z y x r s w z y x
A C D E B F G C F G A B D E
z w y x z w y x
A B D E C D E A B C
29
not a 2-node 2-node
30
31
32
33
34
35
36
37
38
39
not a 2-node
40
not a 2-node 2-node
41
42
43
44
45
46
47
Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)
O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n)
B B
49
A A C H I N E R S
R H N C A
E I A S
50
≈
≈
A A C H I N E R S
R H N C A
E I A S
51
≈
≈
52
53
R H N C A
E I A S
54
R H N C A
E I A S
E
R
H N
I
C A
A S
55
56
Insert C Insert H
61
a b c d
b c d a
a b c d
a b c d
62
A B C a b c d B A C b c d a A B C a b c d A B C a b c d B A C b c d a C B A a b c d C A B a b c d C B A a b c d
Insert x: Search to bottom after key (x) Insert red leaf Balance: 3 cases (+ symmetric)
63
a a b Keep balancing with z b d z c d b d b c z a c b d z d a b c z z a b a z c c d a b c z d d
64
U C
E
R H N
B I S
A C
E
R H N
B I S
A
Insert U Insert V
S C
E
R H N
B I U
A V V U C
E
R H N
B I S
A
Rotate U
65
S C
E
R H N
B I U
A V
Insert G
S C
E
R H N
B I
U A V G S C
E
R H N
B
I
U
A V G
C E R H N B I A S U V G
I C
B
A R N S
U
V H G
E
Rotate I Rotate I
66
67
Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)
O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) red-black tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n)
68