我正在尝试从整数文件中以升序形式对二叉树进行充电,但它不起作用。
program trees;
type
fl = file of Integer
; ltree = ^puntTree
; puntTree = record
val : Integer
; big : ltree
; low : ltree
end
;
procedure openFile ( var iFile: fl; name: String; var error: Boolean );
begin
error := false;
assign ( iFile, name );
{$I-}
reset ( iFile );
{$I+}
if ( ioResult <> 0 ) then
error := true;
end;
procedure loadTree ( var tree: ltree; var iFile: fl; var principalNode: ltree; position: Integer );
var val
:Integer
; node
:ltree
;
begin
if ( tree = nil ) then
begin
seek ( iFile, position );
read ( iFile, val );
new ( tree );
tree^.val := val;
tree^.low := nil;
tree^.big := nil;
end;
if not eof ( iFile ) then
begin
read ( iFile, val );
node := principalNode;
while ( node <> nil ) do
if ( node^.val < val ) then
node := node^.big
else
node := node^.low;
cargartree ( node, iFile, principalNode, ( position + 1 ) );
end;
end;
procedure printTree ( tree: ltree );
begin
if ( tree <> nil ) then
begin
printTree ( tree^.low );
writeln ( tree^.val, ' - ' );
printTree ( tree^.big );
end;
end;
var
tree
, principalNode
:ltree
; iFile
:fl
; fileName
:String
; error
:Boolean
;
begin
readln ( fileName );
abrirfile ( iFile, fileName, error );
if not error then
loadTree ( tree, principalNode, iFile, 0 );
printTree(principalNode);
end.
这是代码。
该文件有 9 个元素:
1 5 9 20 58 95 1 3 8
但是当我打印树时,控制台只显示:
1 -
有办法解决吗?谢谢!