1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Open Addressing Separate Chaining Ordered ACs Balanced Search Trees 2-3-4 Trees Red-Black Trees Lecture 11 Associative Containers
Feb 09, 2016
1
Road Map
Associative Container Impl.
Unordered ACsHashing
Collision Resolution Open Addressing Separate Chaining
Ordered ACs
Balanced Search Trees
2-3-4 Trees
Red-Black Trees
Lecture 11 Associative Containers
2
Road Map Implementing Associative Containers (ACs)
– Hash Tables (Unordered ACs; Ch. 5)– 2-3-4 Trees (Ordered; 4)– Red-Black Trees (Ordered; 12)
Inheritance and Polymorphism revisited Heaps (PQ implementation: 6) Divide and Conquer Algs.
– Mergesort, Quicksort (7) Intro to Graphs (9)
– Representations– Searching– Topological Sorting, Shortest Path
Associative Containers Categories
– Ordered (OAC) – iterate through elements in key order
– Unordered (UAC) – cannot iterate … OACS use binary search trees
– set, multiset, map, multimap UACs use hash tables
– unordered_set– unordered_multiset– unordered_map– unordered_multimap
3
Hash Tables Hash table
– Array of slots– A slot holds
One object (open addressing) Collection of objects (separate chaining)
Average insert, erase, find ops. take O(1)!– Worst case is O(N), but easy to avoid– Makes for good unordered set ADT
4
Hash Tables (Cont’d) Main idea
– Store key k in slot hf (k)– hf: KeySet SlotSet
Complications– | KeySet | >> | SlotSet |, so hf cannot be 1-1– If two keys map to same slot have a collision– Deletion can be tricky
5
Hash Tables (Cont’d) Collision resolution strategies
– Open addressing (probe table for open slot) linear, quadratic probing double hashing
– Separate chaining (map to slot that can hold multiple values
In this case slot is called bucket Approach taken by STL
6
7
Graphical Overview
Table size = mis prime to help distribute keys evenly
8
Open Addressing
h f(2 2 ) = 2 2 2 2 % 7 = 1
h f(4 ) = 4 4 % 7 = 4
01
4
6
23
5
t ab leE n t ry [1 ]
tab leE n t ry [4 ]
2 Steps to Compute Slot1) i = hf (key)2) Slot = i % m
Open Addressing:Each slot holds just 1 key
9
Open Addressing (Cont’d)
22
10
Collision Resolution Using OA: (Linear Probing)
7 7
8 9
1 4
9 4
0
1
2
3
4
5
6
7
8
9
1 0
(a)
1
1
1
1
1
In s ert5 4 , 7 7 , 9 4 , 8 9 , 1 4
2
7 7
8 9
4 5
1 4
9 4
0
1
2
3
4
5
6
7
8
9
1 0
(b )
1
1
1
1
1
In s ert4 5
2
7 7
8 9
4 5
1 4
3 5
9 4
0
1
2
3
4
5
6
7
8
9
1 0
(c)
1
1
1
1
1
In s ert3 5
3
2
7 7
8 9
4 5
1 4
3 5
7 6
9 4
0
1
2
3
4
5
6
7
8
9
1 0
(d )
1
1
1
1
1
In s ert7 6
3
7
5 4 5 4 5 45 4
11
Collision Resolution: Chaining (Cont’d)
12
Collision Resolution with Separate Chaining
< B uc k e t 1 > 8 9 ( 1 ) 4 5 ( 2 )
< B uc k e t 0 >
< B uc k e t 3 > 1 4 ( 1 )
< B uc k e t 2 > 3 5 ( 1 )
< B uc k e t 1 0 > 5 4 ( 1 ) 7 6 ( 2 )
< B uc k e t 6 > 9 4 ( 1 )
< B uc k e t 9 >
< B uc k e t 8 >
< B uc k e t 7 >
< B uc k e t 5 >
< B uc k e t 4 >
7 7 ( 1 )
const size_t TABLE_SIZE = 11; // Prime vector <list<int> > table (TABLE_SIZE); index = hf (key) % TABLE_SIZE;table[index].push_back (key);
13
Coding Hash Functions// Code hash fn. as function object in C++// Stateful and easier to use than function pointerstruct HashString { size_t operator () (const string& key) const { size_t n = 5381; // Prime size_t i; for (i = 0; i < key.length (); ++i) n = (n * 33 ) + key[i]; // Horner
return n; }};
14
Efficiency of Hashing MethodsLoad factor = n / m = # elems / table sizeChaining
– represents avg. list length– Avg. probes for successful search ≈ 1 + /2– Avg. probes for unsuccessful search = – Avg. find, insert, erase: O(1)
Worst case O(1) for ?Open Addressing
– represents ?– If > 0.5, double table size and rehash all
elements to new table
Quadratic probing f(i) = i2 or f(i) = ±i2
If the table size is prime, a new element can always be inserted if the table is at least half empty
15
Rehashing If the table gets too full, operations begin to
bog down
Solution: build a new table twice the size (at least – keep prime) and hash all values from the old table into the new table
16
17
Problems w/ BSTs Can degenerate completely to lists Can become skewed Most ops are O(d)
– Want d to be close to lg(N) How to correct skewness?
18
Two BSTs: Same Keys
5
1 8
3 5
2 5
1 7 7 5
6
1 27
2 09
1 53
4 0
5
1 8
3 5
2 5
1 7
7 5
6 1 2
7
2 0
9
1 53
4 0
D ep t h = 6A v erage co m p aris o n s p er s earch = 4 .0
D ep t h = 4A v erage co m p aris o ns p er s earch = 3 .4 7
(a) (b )
1 0 0
1 0 0
Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 (N = 15)
19
Notions of Balance For any node N, depth (N->left) and
depth (N->right) differ by at most 1– AVL Trees
All leaves exist at same level (perfectly balanced!)– 2-3-4 Trees
Number of black nodes on any path from root to leaf is same (black height of tree)– Red-black Trees
20
Binary Search Tree, Red-Black Tree, and AVL Tree
B in ary Search T ree (a)
5 0
7 8
7 5
8 0
7 0
9 0
6 0
R ed -B lack T ree (b )
7 8
8 07 05 0
9 06 0
7 5
8 07 5
7 85 0
9 06 0
7 0
A VL T ree (c)
1 0 01 0 0
1 0 0
2-3-4 Trees 3 node types
– 2-node: 2 children, 1 key– 3-node: 3 children, 2 keys– 4-node: 4 children, 3 keys
All leaves at same level Logarithmic find, insert, erase
21
22
2-3-4 Tree Node Types
A
2 -n o de
valu e < A valu e > A
A B
3 -n o de
A < B
valu e < A A < v alu e < B valu e > B
A B C
4 -no d e
A < B < C
valu e < A A < v alu e < B v alu e > CB < value < C
23
2-3-4 Tree
2 1 5 3 5 5 5
1 2
2 54 8 1 0
1 15 7 9
How to Search?Space for 4-Node?
Insert for a 2-3-4 Tree Top-down
– Split 4-nodes as you search for insertion point– Ensures node splits don’t keep propagating
upwards Key operation is split of 4-node
– Becomes three 2-nodes– Median key is hoisted up and added to parent node
25
Splitting a 4-Node
C
A B C
S T VU
A
B
S T VU
26
Insertion into 2-3-4 Tree
2 1 2 1 52 2 1 5
In s ert 2 In s ert 1 2In s ert 1 5
1 52
1 2
Sp lit 4 -n od e (2 , 12 , 1 5 )
1 5
1 2
2 4
In s ert 4
1 5
1 2
2 4 8
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
Insert 8
Insert 4
Insertion (Cont’d)
27
2
4 1 2
1 58 2
4 1 2
1 58 1 0
Sp lit 4 -n od e (2 , 4 , 8 ) In s ert 1 0
2
4 1 2
8 1 0 1 5 2 5 2
4 1 2
2
4 1 2 2 5
8 1 0 1 5 3 5 2
4 1 2 2 5
8 1 0 1 5 3 5 5 5
1 5 2 5 3 58 1 0
I n s er t 2 5 In s er t 3 5
In s er t 5 5S p lit 4 - n o d e ( 1 5 , 2 5 , 3 5 )
Insert 55
Insert 10
28
Insertion (Cont’d)
2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 18 2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 18 9
Sp lit 4 -n o d e (8 , 1 0 , 1 1 ) In s ert 9
2 8 10 15 35 55
254
12
2 8 10 11 15 35 55
254
12
Split 4-node (4, 12, 25) Insert 11
Insert 11
Insert 9
29
2 1 5 3 5 5 5
2 5
1 2
4 8 1 0
1 195 7
2 1 5 3 5 5 5
2 5
1 2
4 1 0
1 15 8 9 2 1 5 3 5 5 5
2 5
1 2
4 8 1 0
1 195
In s ert 5 Sp lit 4 -n o d e (5 , 8 , 9 )
Insertion into 2-3-4 Tree (cont’d)Insert 7
Red-Black Trees Can represent 2-3-4 tree as binary tree Use 2 colors
– Red indicates node is “bound” to parent– Red node cannot have red child
Preserves logarithmic find, insert, erase More efficient in time and space
30
31
Red-Black Repr. of 2-3-4 Tree
B
CA
A B C
S T U VS T
R ep res en t at io n w it h a b lackp aren t an d t w o red ch ild ren
4 -n o de (A , B , C )in a 2 -3 -4 T ree
U V
A B
S T U
3 -n o d e (A , B )in a 2 -3 -4 T ree
A
BS
R ep res en t at io n w it h a b lackp aren t an d a red righ t ch ild
T U
B
A
S T
R ep res en t at ion w it h a b lackp aren t an d a red left ch ild
U
32
Converting a 2-3-4 Tree to Red-Black Tree
1 2 1 5
8 1 0 2 0
9 3 0 4 01 3 4 1 2 1 59 3 0 4 01 3 4
1 0
8 2 0
1 2 1 5 3 0 4 0
1 0
8 2 0
3 9
1 4
1 0
8 2 0
3 9
1 4 3 0
1 2 4 0
1 5
33
Red-Black Tree Ops Find? – easy Insertions
– Insert as red node– Require splitting of “4-node” (top-down insertion)– Use color-flip for split (4 cases)– Requires rotations
Deletions– Hard– Several cases – color fix-ups
Remember: RB Trees guarantee lg(N) find’s, insert’s, and erase’s
34
Four Cases in Splitting of a 4-Node
P a r e n t P is B L A C KX is a le f t - c h ild
X
A B
P
C
P a r e n t P is R E D X is a r igh t - c h ild
P a r e n t P is R E D X is a le f t - c h ild
X
A B
P
C
P a r e n t P is B L A C KX is a r igh t - c h ild
X
A B
P
C X
A B
P
C
Case 1 Case 2 Case 3 Case 4
35
Left child of a Black Parent P
2 - 3 - 4 t r e e v ie wR e d- bla c k t r e e
be f o r e t h e c o lo r f lip
A X B
P
C X P
BA
C
X
A B
P
C
R e d- bla c k t r e ea f t e r t h e c o lo r f lip 2 - 3 - 4 t r e e v ie w
X
A B
P
C
Case 1
36
Prior to inserting key 55
2 -3 -4 t ree v iew
5 0
4 0 6 0
3 0
2 04 0 5 0 6 0 5 0
4 0 6 0
3 0
2 0
5 5
5 5 6 0
2 0 3 0 5 0
2 -3 -4 t ree v iew
4 -n o d eb efo re co lo r-fl ip
4 -n o d e after co lo r-fl i pan d in s ert io n o f 5 5
2 0 3 0
C D
C D C D
C D 4 0
Case 2
37
Oriented left-left from G Using A Single Right Rotation
S in g le rig h t ro tat io n w i thp iv o t P an d co lo r ch an g es 2 -3 -4 - t ree v iew
2 -3 -4 t ree v i ew R ed -b l ack t reeb efo re co lo r fl i p
R ed -b l ack t reeaft er co lo r fl ip
P G
A X B C D
G
BA
X
P
C
D
G
BA
X
P
C
D
G
BA
X
P
C D
X P G
A B C DCase 3(and G, P, X linear (zig-zig)
P rotated right
38
Oriented Left-Right From G After the Color Flip
P G
A X B
BA
X
G
P
C
DC D
BA
X
G
P
C
D
2 -3 -4 t ree v iew R ed -b lack t reeb efo re co lo r flip
R ed -b lack t reeaft er co lo r flip
Case 4 (and G, P, X zig-zag)
39
After X is Double Rotated
X
P G
A BC D
(X is rotated left-right)
40
Building A Red-Black Tree
2 2
In s ert 2 m ak e ro o tB LA C K
R ed -B lack T ree 2 - 3 - 4 T r e e
22
R e d- B la c k T r e e
B LA C K
2 1 2 1 51 5
22 -3 -4 T ree
R ed -B lack T ree
1 2
A B
C
DX
G
1 52
1 2
C A B D
D o u b le l eftro t at io n
P
2 4
2 -3 -4 T reeR ed -B lack T ree
1 2
1 51 52
1 2
41 52
1 2
1 52
1 2
C o lo r F l ip
1 52
1 2
M ak e ro o tB L A C K
In s ert 4
4
right-leftrotate
41
Building A Red-Black Tree (Cont…)
2 4 8
1 54
2 -3 -4 T reeR ed -B lack T ree
1 21 2
1 582
1 54
2 -3 -4 T ree
R ed -B lack T ree
1 2
1 582
4 1 2
2 8 1 0
1 54
1 2
82
1 54
1 2
82
1 0
C o lo r fl ip In sert 1 0
8
1 5
1 2
82
1 2
82
2 5
2 -3 -4 T reeR ed -B lack T ree
4 1 2
2 8 1 0
4
1 0
1 5 2 52 5
In s ert 2 5
2 -3 -4 T reeR ed -B lack T ree
4 1 2
2 8 1 01 5
4
2
1 0
1 5 2 5 3 53 5
In s ert 3 5
42
Repr. of Red-Black Node
3 5
8 02 5
R ed -B lack T ree rb n o d e R ep res en t at io n o f R ed -B lack T ree
2 5 R E D 8 0 R E D
5 0 B L A C K35