在一个问题中,我从整数文件的元素初始化一棵树(请注意,树不一定必须是平衡的)。
然后我必须按升序打印同一棵树,即从最低到最高。问题如下:
该文件的元素是:1 - 5 - 90 - 0 - 50 - 70
所以,预期的打印是:(0 - 1 - 5 - 50 - 70 - 90
从低到高),但控制台显示: 5 - 0 - 50 - 1 - 70 - 90
program loadTree;
type
IntegerFile = file of Integer
; pointer = ^treeP
; treeP = record
value : Integer
; low : pointer
; big : pointer
end
;
procedure loadTree ( var tree: pointer; var file: integerFile; p : Integer );
var value
:Integer
;
begin
if ( tree = NIL ) then
begin
read ( file, value );
new ( tree );
tree^.value := value;
tree^.low := NIL;
tree^.big := NIL;
end;
if ( p < ( filesize ( file ) - 1 ) ) then
begin
read ( file, value );
seek ( file, ( p + 1 ) );
if ( value < tree^.value ) then
loadTree ( tree^.low, file, ( p + 1 ) )
else
loadTree ( tree^.big, file, ( p + 1 ) );
end;
end;
procedure printTree ( tree: pointer );
begin
if ( tree <> nil ) then
begin
printTree ( tree^.low );
writeln ( arbol^.value );
printTree ( tree^.big );
end;
end;
(...)
Begin
(...)
cargarArbol ( tree, file, 0 );
imprimirArbol ( tree );
(...)
end.
假设我已经声明了变量并初始化了文件
问题是:我能做些什么来解决它?
我找不到错误,我想知道问题是在树的负载上还是在我尝试打印它时。