dehafftreei
t
CodehaffCodecharstr1编码功能
Codecd
ewCode
i
tchildpare
t
fori
tr0r
r
对所有叶节点编码
cdstart
1
cdweighthafftreerweight
cdvaluestr1r
childr
pare
thafftreechildpare
t
whilepare
t0
ifhafftreepare
tleftchildchild
cdbitcdstart0
左孩子结点为0
else
cdbitcdstart1
右孩子结点为1
cdstart
第4页
f数据结构课程设计报告
childpare
tpare
thafftreechildpare
tfori
tjcdstart1j
jhaffCoderbitjcdbitjhaffCoderstartcdstarthaffCoderweightcdweighthaffCodervaluecdvalue
voidyimaHaffNodehafftreei
t
stri
gE
str译码功能
i
troot2
2
fori
ti0iE
strle
gthi
当E
stri为0时,root向左走,
为1时向右走。当走到叶子结点时,输出叶子的value
ifE
stri0hafftreerootleftchild1
roothafftreerootleftchild
elseifE
stri1hafftreerootrightchild1
roothafftreerootrightchild
ifhafftreerootleftchild1hafftreerootrightchild1
couthafftreerootvalue
root2
2
coute
dl
hafcpp
i
cludeiostreami
cludestdlibhi
cludestri
gco
sti
tMaxValue100co
sti
tMAXN100co
sti
tmaxbit100usi
g
amespacestdi
clude