Question: C++ lab array, huffman code For this lab, you will writ a program that can rad in a huffman tr and gnrat th huffman cod
C++ lab array, huffman code
For this lab, you will writ a program that can rad in a huffman tr and gnrat th huffman cod for th itms stord in th lavs of th tr (th tr will hav no mor than 20 nods total). Th input will b a lft-child, right-child array rprsntation of th tr. For xampl:
6
7
119 10 -1 -1
113 12 -1 -1
104 20 -1 -1
101 58 -1 -1
45 22 0 1
45 78 2 3
45 100 4 5
Th root of th tr is listd first, so in this cas it is row 6 (C++ indxing). Th numbr of nods in th tr is listd scond. Thn ach of th subsqunt rows rprsnts a nod in th tr. Th first itm in a row rprsnts a charactr. This charactr is to b ignord unlss th nod is a laf. If th nod is a laf, thn this numbr is th ASCII valu for a char, and can b cast as a char and printd. Th scond numbr is th frquncy associatd with th nod in th Huffman tr. This numbr can b ignord sinc it was only usd as a dvic to build th tr. Th third numbr rprsnts th lft child of th currnt nod (i.., th row containing th information about th lft child). Similarly, th fourth numbr rprsnts th right child of th currnt nod. Notic that th lft and right child valus ar using th C++ standard of starting with array indx 0. Thus a -1 indicats a NULL valu. By augmnting th prordr travrsal algorithm to pass not only th currnt subtrs root, but also a on dimnsional array for th cod, a 0 or 1 for lft or right travrsal, and a dpth-in-th-tr countr, it is asy to do a prordr travrsal of th huffman tr and print out th cod words as th lavs ar ncountrd. For stack spac fficincy, mak th tr a global variabl. Th output for th abov xampl would b:
w = 00
q = 01
h = 10
= 11
If th input wr:
6
7
119 10 -1 -1
113 12 -1 -1
104 20 -1 -1
101 58 -1 -1
45 22 0 1
45 42 2 4
45 100 5 3
Th corrsponding output would b:
h = 00
w = 010
q = 011
= 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
