Jopa balanced by Bel feat Andrew

  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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
program exam4;
type
tp=^tprec;
tprec=record
inf:integer;
next:tp
end;
tree=^treerec;
treerec=record
inf:integer;
left:tree;
right:tree;
end;
var genroot:tree;
genfirst:tp;
temp:tp;
listlen:integer;
q:boolean;
f:text;
procedure create_sortlist(var first:tp; inp:string);
var p,q:tp; c:integer; f:text;
begin
assign(f,inp);
reset(f);
new(p);
p^.next:=nil;
read(f,p^.inf);
first:=p;
while not eof(f) do
begin
read(f,c);
p:=first;
if (p^.next<>nil) and (c>first^.inf)
then
begin
while not((c>p^.inf)and(c<=p^.next^.inf)) and (p^.next<>nil) do
p:=p^.next;
q:=p^.next;
new(p^.next);
p:=p^.next;
p^.inf:=c;
p^.next:=q
end
else if c<p^.inf
then
begin
new(first);
first^.inf:=c;
first^.next:=p
end
else
begin
q:=p^.next;
new(p^.next);
p:=p^.next;
p^.inf:=c;
p^.next:=q
end
end;
close(f)
end;
procedure output_list(first:tp; out: string);
var p:tp; f:text;
begin
assign(f,out);
rewrite(f);
p:=first;
while p<>nil do
begin
write(f,p^.inf,' ');
p:=p^.next
end;
close(f)
end;
procedure CreateTree(var parent:tree;k,l:integer;q:boolean;lf:boolean);
var o,i:integer;
m:integer;
temp:tp;
begin
m:=0;
if (k = l) and not q then exit;
if (l-k<2) then
begin
if q then
begin
if k = 1 then o:=1 else
if l = listlen then o:=l else exit
end else
begin
if l = listlen then o:=l else
if k = 1 then o:=1 else exit;
end;
end else
begin
if lf then
o := (k+l) div 2
else
o:=(k+l+1) div 2;
end;
temp:=genfirst;
for i:=2 to o do
temp:=temp^.next;
if q then
begin
parent^.inf := temp^.inf;
parent^.left:=nil;
parent^.right:=nil;
CreateTree(parent,k,o,false,true);
CreateTree(parent,o,l,false,false);
q:=false;
end else
begin
if (parent^.inf>temp^.inf) then
begin
new(parent^.left);
parent^.left^.left:=nil;
parent^.left^.right:=nil;
parent^.left^.inf := temp^.inf;
if (o = 1)or(o=listlen) then exit;
CreateTree(parent^.left,k,o,false,true);
CreateTree(parent^.left,o,l,false,false);
end else
begin
new(parent^.right);
parent^.right^.left:=nil;
parent^.right^.right:=nil;
parent^.right^.inf := temp^.inf;
if (o = 1)or(o=listlen) then exit;
CreateTree(parent^.right,k,o,false,true);
CreateTree(parent^.right,o,l,false,false);
end;
end;
end;
procedure OutputTree(parent:tree);
begin
if parent^.left<>nil then OutputTree(parent^.left);
write(f,parent^.inf,' ');
if parent^.right<>nil then OutputTree(parent^.right);
end;
begin
create_sortlist(genfirst,'input4.inp');
output_list(genfirst,'output4.out');
temp:=genfirst;
listlen:=0;
while temp <> nil do
begin
temp:=temp^.next;
inc(listlen);
end;
new(genroot);
q:=true;
CreateTree(genroot,1,listlen,q,true);
assign(f,'output4.out');
append(f);
writeln(f);
OutputTree(genroot);
close(f)
end.