program lab10 uses crt type ref node node record fio string 20 count i

 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
program lab10;
uses crt;
type
ref=^node;
node=record
fio:string[20];
count:integer;
left,right:ref;
end;
var root:ref;
st:string[20];
name:string[10];
f:text;
n:integer;
procedure createfile(n:integer);
var i:integer;
begin
clrscr;
writeln('name of file>');
readln(name);
assign(f,name);
rewrite(f);
writeln('vvedit dani>');
for i:=1 to n do
begin
readln(st);
writeln(f,st);
end;
end;
procedure search(x:string; var t:ref);
begin
if t=nil then
begin
new(t);
t^.fio:=x;
t^.count:=1;
t^.left:=nil;
t^.right:=nil;
end
else if x<t^.fio then search(x,t^.left)
else if x>t^.fio then search(x,t^.right)
else t^.count:=t^.count+1;
end;
procedure printtree(t:ref; h:integer);
var i:integer;
begin
if t<>nil then
with t^ do
begin
printtree(right,h+1);
for i:=1 to h do write(' ');
writeln(fio);
printtree(left,h+1);
end;
end;
procedure inorder(t:ref);
begin
if t<>nil then
begin
inorder(t^.left);
write(t^.fio,' ');
inorder(t^.right);
end;
end;
begin
clrscr;
writeln('kilkist studentiv u grupi');
readln(n);
createfile(n);
reset(f);
root:=nil;
while not eof(f) do
begin
readln(f,st);
search(st,root);
end;
printtree(root,0);
inorder(root);
readln;
end.