program exam7 type tree trectree trectree record num integer fam strin

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
program exam7;
type
tree=^trectree;
trectree=record
num:integer;
fam:string;
left:tree;
right:tree
end;
trec=record
num:integer;
fam:string
end;
procedure put_tree(var root:tree; s:trec);
var p:tree;
begin
if root=nil
then
begin
new(p);
p^.num:=s.num;
P^.fam:=s.fam;
p^.left:=nil;
p^.right:=nil;
root:=p
end
else
if s.num<=root^.num
then put_tree(root^.left,s)
else put_tree(root^.right,s)
end;
procedure create_tree(var root:tree);
var p,prev:tree; s:trec;
begin
if eof then root:=nil
else
begin
new(p);
readln(p^.num,p^.fam);
p^.left:=nil;
p^.right:=nil;
root:=p
end;
while not eof do
begin
p:=root;
readln(s.num,s.fam);
while p<>nil do
begin
prev:=p;
if s.num<=p^.num then p:=p^.left
else p:=p^.right;
end;
new(p);
p^.left:=nil;
p^.right:=nil;
p^.num:=s.num;
p^.fam:=s.fam;
if s.num<=prev^.num then prev^.left:=p
else prev^.right:=p
end
end;
procedure output_tree(p:tree);
begin
if p^.left<>nil then output_tree(p^.left);
writeln(p^.num,' ',p^.fam);
if p^.right<>nil then output_tree(p^.right)
end;
procedure output_sheets(p:tree);
begin
if (p^.left=nil) and (p^.right=nil) then
writeln(p^.num,' ',p^.fam);
if p^.left<>nil then output_sheets(p^.left);
if p^.right<>nil then output_sheets(p^.right)
end;
var genroot:tree; s1:trec;
begin
assign(input,'input7.inp');
reset(input);
assign(output,'output7.out');
rewrite(output);
{ genroot:=nil;
while not eof do
begin
readln(s1.num,s1.fam);
put_tree(genroot,s1)
end; }
create_tree(genroot);
close(input);
output_tree(genroot);
writeln;
output_sheets(genroot);
close(output);
end.