- 合并果子
- 2009-07-13 22:59:56 @
源程序如下:
Program HuffmanTree;
Type
bitreptr=^node;
node=record
data:integer;
p,l,r:bitreptr;
length:integer;
end;
treetype=array[1..20000] of bitreptr;
Var
w:array[1..20000] of integer;
tree:treetype;
root:bitreptr;
m,n,i,tot:integer;
Procedure build(var tree:treetype; var bt:bitreptr);
var
i,k:integer;
temp1,temp2:bitreptr;
function min(h:integer):bitreptr;
var ml:integer;
p:integer;
begin
ml:=30000;
for p:=1 to h do
if(tree[p]^.p=nil)and(ml>tree[p]^.data)
then begin
i:=p; ml:=tree[p]^.data;
end;
min:=tree[i];
end;
begin
readln(n);
m:=2*n-1;
for i:=1 to n do
begin
new(tree[i]);
read(tree[i]^.data);
end;
for k:=n+1 to m do
begin
new(tree[k]);
temp1:=min(k-1); temp1^.p:=tree[k]; tree[k]^.l:=temp1;
temp2:=min(k-1); temp2^.p:=tree[k]; tree[k]^.r:=temp2;
tree[k]^.data:=temp1^.data+temp2^.data;
end;
bt:=tree[m];
end;
Procedure getLength(var t:bitreptr);
begin
if t=nil then exit;
if t=root then t^.length:=0
else t^.length:=t^.p^.length+1;
if t^.lnil then getLength(t^.l);
if t^.rnil then getLength(t^.r);
end;
BEGIN
build(tree,root);
getLength(root);
tot:=0;
for i:=1 to n do
tot:=tot+(tree[i]^.data)*(tree[i]^.length);
writeln(tot);
END.
为什么只通过了一个点?还有,128错误是什么回事?
1 条评论
-
贱人在我右边 LV 9 @ 2016-12-14 15:20:35
he
- 1