-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_tree_maximum_and_maximumnode.pas
More file actions
72 lines (72 loc) · 1.14 KB
/
Binary_tree_maximum_and_maximumnode.pas
File metadata and controls
72 lines (72 loc) · 1.14 KB
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
program maximum_and_maximumnode;
type
tree = ^node;
node = record
data:integer;
cl:tree;
cr:tree;
end;
procedure add(var a:tree;n:integer);
begin
if (t = nil) then
begin
new(t);
t^.data:=n;
t^.cl:=nil;
t^.cr:=nil;
end
else
begin
if (t^.data<=n) then
add(t^.cl,n);
else
add(t^.cr,n);
end;
end;
procedure chargetree(abb:tree;num:integer);
begin
writeln('write some number: (50 stop the process)');
readln(num);
while (num<>50) do
begin
add(abb,num);
writeln('write some number: (50 stop the process)');
readln(num);
end;
end;
function maximum (t:tree):integer;
begin
if (t<>nil) then
begin
if (t^.cr=nil) then
maximum:= t^.data;
else
maximum:= maximum(t^.cr,maximum);
end;
end;
function maximumnode(t:tree):tree;
begin
if (t<>nil) then
begin
if (t=nil) then
maximumnode:= nil;
else
begin
if (t^.cr=nil) then
maximumnode:= t;
else
maximumnode:= maximumnode(t^.cr,maximumnode);
end;
end;
end;
var
tr,maximnode:tree;
maxim,num:integer;
begin
tr:=nil;
chargetree(tr,num);
maxim:=maximum(tr);
maximnode:=maximumnode(tr);
writeln(maxim);
writeln(maximnode);
end.