Beruflich Dokumente
Kultur Dokumente
Continued..
Continued
Operations on heaps
creation of an empty heap
insertion of a new element into the heap;
deletion of the largest element from the heap
MinHeap
0
4
3
MaxHeap
68
1
26
65
24
7
21
31
8
16
13
5
19
32
Insertion in a heap
1 Add the new node to the heap
1.Add
heap.
2K
2.Keep
th
thatt att the
th right
i ht place
l
iin th
the h
heap
Insert step
p1
13 21 16 24 31 19 68 65
26 32
Insert step 2
13 21 16 24 31 19 68 65
26 32 14 ....
Insert step 3
13 21 16 24 14 19 68 65
26 32 31 ....
Insert step 4
13 14 16 24 21 19 68 65
26 32 31 ....
Insert step 5
Now 14 ,at its right place
Deletion of 6
6
18
20
28
8
39
29
37 26 76 32 74 89 66
Continue
Continue..
After removal of 6
66
18
20
37
28
26 76
8
39
32
74
29
89
Continue
Continue..
Swapping of smaller child with 66
8
18
66
20
28
39
29
37
26 76 32 74
89
Continue
Continue..
Swapping of smaller child with 66
8
18
29
20
28
39
66
37
26
76
32
74
89
i->internal nodes
E->external nodes
i
i
i
i
E
E=i+1
E
E
i
E
T
Total
t l path
th length
l
th for
f internal
i t
l and
d external
t
l
nodes(no.of min. nodes traversed from
roott to
t a particular
ti l node)
d ) arePi =0+1+2+1+2+3=9
PE=2+3+3+2+4+4+3=21
Weighted
g
Extended
Binary Tree
12
8
7
12
(fig. 1)
4
7
(fig 2)
12
(fig 3)
7
Pathlength of fig 1:
4x1+7x3+12x3+8x2=77
P
Pathlength
thl
th off fifig 2
2:
4x2+7x2+8x2+12x2=62
Pathlength
g of fig
g 3:
12x1+4x3+7x3+8x2=61
Though the fig 2 & 3 hav same tree ,they
have different path lengths
Huffman Algorithm
Take n weights
weights,W1,W2,W3.Wn
W1 W2 W3
Wn
T
Take
k two
t
minimum
i i
weights
i ht and
d create
t a
subtree.Suppose W1 and W2 are first two
minimum weights will be
Example
Let us take 7 elements and the minimum
weight extended binary tree will be created
using Huffman algorithm
Weights:
A B C D E F G
16 11 7 20 25 5 16
Step 1
Taking 2 nodes with minimum weights 7
and 5
12
5
Step 2
Taking two nodes with minimum weight which
are 11 and 12
23
11
12
5
7
Now the elements of the list are16 23 20 25 16
Step 3
Taking two nodes with minimum weight 16
16
32
16
16
Step 4
Taking 2 nodes with minimum weight
which
hi h are 23 and
d 20
43
23
20
12
11
5
Step 5
Now the elements in list are 32 43 25
Taking 2 nodes with minimum weights
which are 32 and 25
57
32
25
16
16
Step 6
Now the elements in the list are 43 57
Taking 2 nodes with minimum weight
which are 43 and 57
100
57
43
20
25
23
16
12
11
5
32
16
1
B
1
E
Huffman Coding
-> The code construction process has a complexity of O(Nlog2N)
-> Huffman codes satisfy the prefix-condition
->Uniquely
q y decodable: no codeword is a p
prefix of another codeword
bmajhi@nitrkl.ac.in
34
Huffman Coding
Codeword
Codeword X
length
2
2
3
3
3
4/8/2009
01
10
11
000
001
a
b
c
d
e
Probability
0
01
0.25
10
0.25
11
0.2
000
001
0.15
0.15
00
0.3
01
0.25
10
0.25
11
0.2
bmajhi@nitrkl.ac.in
1
0.45
00
0.3
01
0.25
0
0.55
1
0.45
35