When working with streams, the most complex issue is dealing with variable-length data. In your case that's the FullName and the addrs, because those fields are of type String. The easiest solution is to use Delphi's stream reader and writer helper classes, TReader and TWriter, because they provide easy means of working with string. Other thant that the most obvious solution is to write the tree to the stream recursively, one node at a time.
Warning, code written in browser window:
// This will save one node to the stream, using the TWriter helper. Takes
// care of potential NIL's.
procedure SaveBinaryTreeToStreamWriter(RootNode: TPMyTree; W: TWriter);
begin
if Assigned(RootNode) then
begin
W.WriteBoolean(True);
W.WriteInteger(RootNode^.ID);
W.WriteString(RootNode^.FullName);
W.WriteString(RootNode^.addres);
SaveBinaryTreeToStreamWriter(RootNode^.LeftT, W);
SaveBinaryTreeToStreamWriter(RootNode^.RightT, W);
end
else
W.WriteBoolean(False);
end;
// This will read one NODE from the stream, using the TReader helper.
// Uses the boolean "nil" marker saved by the writing routine to also
// return "nil" if needed.
function ReadBinaryTreeNodeFromReader(R: TReader):TPMyTree;
begin
if R.ReadBoolean then
begin
Result := AllocMem(SizeOf(TMyTree));
Result^.ID := R.ReadInteger;
Result^.FullName := R.ReadString;
Result^.addres := R.ReadString;
Result^.LeftT := ReadBinaryTreeNodeFromReader(R);
Result^.RightT := ReadBinaryTreeNodeFromReader(R);
end
else
Result := nil;
end;
// This simply creates the TWriter and then starts the recursive process of
// writing the tree to stream.
procedure SaveBinaryTreeToStream(RootNode: TPMyTree; Stream: TStream);
var W:TWriter;
begin
W := TWriter.Create(Stream, 128);
try
SaveBinaryTreeToStreamWriter(RootNode, W);
finally W.Free;
end;
end;
// This simply creates the TReader and then starts the recursive process of
// reading the tree one node at a time:
function ReadFromStream(Stream:TStream):TPMyTree;
var R: TReader;
begin
R := TReader.Create(Stream, 128);
try
Result := ReadBinaryTreeNodeFromReader(R);
finally R.Free;
end;
end;
string[number]) and structuring your tree so Left and Right sub-nodes are not actual pointers but indexes into a big buffer. That way you'll be able to read and write the whole index in one go, not one node at a time. And if this gets to large numbers of nodes, make sure you look into tree data structures designed to work with the disk, like the B-Tree.