program asdkj;
type
tree=^treetype;
treetype=record
wd:string;
tm:integer;
lt,rt:tree;
end;
link=^linktype;
linktype=record
wd:string;
tm:integer;
next:link;
end;
const
letter=['a'..'z','A'..'Z'];
var
head:link;
root:tree;
n,st:string;
procedure readword;
var
q,p:link;
w:string;
begin
head:=nil;
repeat
readln(w);
if (w<>'') then
begin
p:=head;
while (p<>nil) and (p^.wd<>w) do
p:=p^.next;
if p=nil then
begin
new(q);
q^.wd:=w;
q^.tm:=1;
q^.next:=head;
head:=q;
end
else
inc(p^.tm);
end;
until (w='');
end;
procedure create;
var
p,r:tree;
f:boolean;
q:link;
begin
new(root);
with root^ do
begin
wd:=head^.wd;
tm:=head^.tm;
lt:=nil;
rt:=nil;
end;
q:=head^.next;
while q<>nil do
begin
p:=root;
new(r);
r^.lt:=nil;
r^.rt:=nil;
r^.wd:=q^.wd;
r^.tm:=q^.tm;
f:=true;
while f do
begin
if (q^.wd<p^.wd) then
if (p^.lt<>nil) then p:=p^.lt
else begin
p^.lt:=r;
f:=false;
end
else
if (p^.rt<>nil) then p:=p^.rt
else begin
p^.rt:=r;
f:=false;
end;
end;
q:=q^.next;
end;
end;
procedure pr_tree(p:tree);
begin
if p^.lt<>nil then pr_tree(p^.lt);
write(p^.wd:20,p^.tm:5);
if p^.rt<>nil then pr_tree(p^.rt);
end;
begin
readword;
create;
pr_tree(root);
end.
posted on 2005-08-19 14:36
李青 阅读(457)
评论(0) 编辑 收藏 引用