Sie sind auf Seite 1von 35

Heap

A max tree is a tree in which the key value in


each node is larger than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
A min tree is a tree in which the key value in
each node is smaller than the key values in
i children.
its
hild
A min
i h
heap is
i a complete
l
bi
binary
tree that is also a min tree.

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 root item from a min


heap
1.Remove the item in the root node by
replacing
l i with
ith th
the llastt ititem iin th
the h
heap(LIH).
(LIH)
2.While item(LIH) has children and item larger
than either of its children
3.Swap
p item with the smaller child ,,moving
g
LIH down the heap.

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

Extended binary tree


Binary tree in which every node has zero
or two children ,is known as extended
binary tree
The nodes which have two children are
called internal nodes
The nodes which have no children are
called
ll d external
t
l nodes
d

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

From this it is concluded that PE=Pi+2 x n


where Pi= total path length of internal
nodes ,
PE=total path length of external
nodes,
n=no. of internal nodes.

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

Problem arises for obtaining a minimum


weighted path length
That unique extended binary tree can be
obtained
bt i d b
by H
Huffman
ff
Al
Algorithm
ith
The binary tree obtained by Huffman Algorithm
is known as Huffman tree.

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

Now the remaining weights will be


W1+W2,W3,Wn
Create all subtrees at the last weight.

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

Now the elements in the list are:


16,11,12,20,25,16

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

Now the elements in the list are


32 23 20 25

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

Implementation of huffman tree is very


useful if we want to send a message
A=000,B=001,C=01,D=10,E=11,
0
0
0
A

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

Huffman Coding Algorithm


(1) Order the symbols according to the probabilities
Alphabet set: S1, S2,, SN
Probabilities: P1, P2,, PN
The symbols
y
are arranged
g so that P1 P2 PN
(2) Apply a contraction process to the two symbols with the smallest
probabilities. Replace the last two symbols SN and SN-1 to form a
new symbol
y
HNN-11 that has the p
probabilities P1 +P2.
The new set of symbols has N-1 members: S1, S2,, SN-2 , HN-1
(3) Repeat the step 2 until the final set has only one member.
(4) The codeword for each symbol Si is obtained by traversing the
binary tree from its root to the leaf node corresponding to Si
4/8/2009

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

Das könnte Ihnen auch gefallen