Beruflich Dokumente
Kultur Dokumente
0
1 Keaton 1
2 Laurel 3 Hardy 4
3
4 Chaplin 1
5
Hashing
• Whenever more than N different identifiers are encountered,
causing the load factor to exceed 1, it is time to double the
size of the array used to implement the hash table. At that
point, the hash function must be changed to double its range
and every entry already in the table must be rehashed to its
new position in a list of the larger array.
• Rehashing is expensive.
• Rehashing is repeated each time the load factor exceeds 1;
after rehashing the load factor drops back to ½
T
0
U
(Universe of keys) h(K1)
K1 h(K4)
K
K4 h(K2)=h(K5)
(actual K5
keys) K2
K3 h(K3)
m-1
U K1 K4
(Universe of keys)
K1
K
K4 K5 K2 K7
(actual K5
keys) K7
K3 K3
K2
K8 K8 K6
K6
T[3]
T[2]
7 9
4 7 5 2
2 1 6
10 7 9 4 7 5 2 2 1 6
• The crucial characteristic of this data structure is that the heap
property can be restored efficiently if the value of a node is modified.
• If the value of a node increases to the extent that it becomes greater
than the value of its parent, it suffices to exchange these two values,
and then to continue the same process upwards in the tree if
necessary until the heap property is restored.
• The modified value is percolated up to its new position in the heap
• This operation is often called sifting up
• If the value 1 in Figure is modified so that it becomes 8, we can
restore the heap property by exchanging the 8 with its parent 4, and
then exchanging it again with its new parent 7.
The heap, after percolating 8 to its place
10
8 9
7 7 5 2
2 4 6
Heaps
• If on the contrary the value of a node is decreased so that it
becomes less than the value of at least one of its children, it
suffices to exchange the modified value with the larger of the
values in the children, and then to continue this process
downwards in the tree if necessary until the heap property is
restored.
• The modified value has been sifted down to its new position.
9
8 5
The heap, after sifting 3 (originally 10)
down to its place
7 7 3 2
2 4 6
Heaps
• The following procedures describe more formally the basic
processes for manipulating a heap.
1 6 9 2 7 5 2 7 4 10
1
The starting situation
9
6
2 7 5 2
7 4 10
Heaps
• We first make each of the subtrees whose roots are at level 1
into a heap, this is done by sifting down these tools, as
illustrated in Figure.
10
7 5 2
2 4 7
1 10 9 7 7 5 2 2 4 6
6 10 10
7 10 7 6 7 7
2 4 7 2 4 7 2 4 6
One level 2 subtree is made into a heap (the other already is a heap)
It only remains to sift down its root to obtain the desired
heap. This process thus goes as follows:
10 1 9 7 7 5 2 2 4 6
10 7 9 1 7 5 2 2 4 6
10 7 9 4 7 5 2 2 1 6
10
7 9
4 7 5 2
2 1 6
Formal description of the algorithm is as under:-
Procedure make-heap(T[1..n])
{This procedure makes the array T[1..n] into a
heap}
for i [n/2 ]downto 1 do sift-down(T,i)
Procedure heapsort (T[1..n])
{T is an array to be sorted}
make-heap(T)
for i n downto 2 do
exchange T[1] and T[i]
sift-down(T[1..i – 1], 1)
• There is no good way of searching for a particular item in a
heap.
• There is no efficient way of merging two heaps. The best
way we can do is to put the contents of the two heaps side
by side in an array, and then call procedure make-heap on
the combined array.
• In any kind of heap searching for a particular item is
inefficient.
Heap viewed as a binary tree
16
14 10
8 7 9 3
2 4 1
16 14 10 8 7 9 3 2 4 1
4 10
The initial configuration
14 7 9 3
2 8 1 16
14 10
8 7 9 3
Maintaining heap property
2 4 1
Making a heap
A 4 1 3 2 16 9 10 14 8 7
1 2 3 4 5 6 7 8 9 10
T[1]
4
T[2]
1 3 T[3]
T[6]
T[4]
2 T[5] 16 9 10 T[7]
T[8]
14 8 7 T[10] Procedure make-heap (T[1..10])
For i5 to 1 do sift-down(T,i)
T[9]
a. i = 5, Sift-down (T, 5); both if = false no exchange
4 1 3 2 16 9 10 14 8 7
b. i = 4, Sift-down (T, 4); first if = true exchange T[4] & T[8]
4 1 3 2 16 9 10 14 8 7
c. i = 3, Sift-down (T, 3); first & 2nd if = true exchange T[3]
& T[7]
4 1 3 14 16 9 10 2 8 7
d. i = 2, Sift-down (T, 2); 2nd if = true exchange T[2] & T[5]
4 1 10 14 16 9 3 2 8 7
e. i = 1, Sift-down (T, 1); first if = true exchange T[1] & T[2]
Repeat ……
4 16 10 14 7 9 3 2 8 1
16
Final heap after all operations
14 10
8 7 9 3
2 4 1
1
A= 16 14 10 8 7 9 3 2 4 1 16
3
2 14
10
for in down 2 do
5 7
exchange T(1) and T(i) 4
8 6
7 9 3
Sift-down(T[..i-1],1)
9
8 10
2 4 1 Make-heap (T)
i = 10, exchange T[1] & T[10] and sift-down (T[1..9],1)
14 8 10 4 7 9 3 2 1 16
1 2 3 4 5 6 7 8 9 10
i = 9, exchange T[1] & T[9] and sift-down (T[1..8],1)
10 8 9 4 7 1 3 2 14 16
1 2 3 4 5 6 7 8 9 10
i = 8, exchange T[1] & T[8] and sift-down (T[1..7],1)
9 8 3 4 7 1 2 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 7, exchange T[1] & T[7] and sift-down (T[1..6],1)
8 7 3 4 2 1 9 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 6, exchange T[1] & T[6] and sift-down (T[1..5],1)
7 4 3 1 2 8 9 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 5, exchange T[1] & T[5] and sift-down (T[1..4],1)
i = 5, exchange T[1] & T[5] and sift-down (T[1..4],1)
4 2 3 1 7 8 9 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 4, exchange T[1] & T[4] and sift-down (T[1..3],1)
1 2 3 4 7 8 9 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 3, exchange T[1] & T[3] and sift-down (T[1..2],1)
2 1 3 4 7 8 9 10 14 16
1 2 3 4 5 6 7 8 9 10
i = 2, exchange T[1] & T[2] and sift-down (T[1..1],1)
1 2 3 4 7 8 9 10 14 16
1 2 3 4 5 6 7 8 9 10
End of Sorting
Sorted heap
1
2 3
4 7 8 9
10 14 16
B0 B1 B2 B3 B4
Binomial trees B0 to B4
max
3 25 23
12 9 14 20
1 5 17
1 9 4 8 1 9
12
6 3 6 4 8
3 1 11 merged with 2 5 8
6 4
yields 9 13 12
2 7 1 11 10
3 6 5 8
25 28 33
41
BINOMIAL-HEAP MERGE
head [H2] 18 3 6
8 29 10 44
37
30 23 22 48 31 17
45 32 24 50
55
head [H] 12 3 15 6
18 37 28 33 8 29 10 44
7
25 41 30 23 22 48 31 17
45 32 24 50
55
head [H] 12 3 6
18 15 37 8 29 10 44
7
28 33 25 30 23 22 48 31 17
41 45 32 24 50
55
head [H] 37 10 1
41 28 13 6 16 12 25
77 8 14 29 26 23 18
11 17 38 42
27
41 28 13 6 16 12 25
77 8 14 29 26 23 18
11 17 38 42
41 28 13 18 26 23 8 14 29
77 42 11 17 38
27
head [H] 25 12 6
37 18 10 8 14 29
41 16 28 13 11 17 38
26 23 77 27
42
Merging heaps H & H
head [H] 25 12 6
37 10 8 14 29
18
41 16 28 13 11 17 38
y 7 23 77 27
42
37 10 8 14 29
18
41 7 28 13 11 17 38
16 23 77 27
42
head [H] 25 12 6
37 8 14 29
18 7
41 10 28 13 11 17 38
16 23 77 27
42
head [H] 10 47 8 12 4
61 26 11 13 14 15 24
75 19 22 36 27 31 16
30 29 44 43
50
head [H] 10 47 8 12 4
61 26 11 13 14 15 24
75 19 22 36 27 31 16
30 29 44 43
50
Quiz # 3. Delete node with value 26 and readjust the binomial heap.