Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:50:55+00:00 2026-05-28T02:50:55+00:00

please , suppose i have this Binary Tree structure : Type TPMyTree = ^TMyTree;

  • 0

please , suppose i have this Binary Tree structure :

Type
    TPMyTree = ^TMyTree;
    TMyTree = Record
        ID:Integer;
        FullName:String[50];//<-------- fixed
        addrs:String[50] // <----------- fixed
        LeftT:TPMyTree;
        RightT:TPMyTree;
    end;

How could i save and load it from a Stream ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:50:56+00:00Added an answer on May 28, 2026 at 2:50 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please suppose I have a function that accepts a pointer as a parameter. This
Suppose I have something like this int strLen; printf(Please enter a number: ); scanf(%d,
Suppose I have an abstract class FactorizedDialog . It looks like this (please note
Suppose to have a code like this: <div class=notSelected> <label>Name <input type=text name=name id=name
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I have a class like this: #include <iostream> using namespace std; class Boda
Suppose I have 3 classes as follows (as this is an example, it will
Suppose I have a big database, about 800 tables. In this database there is
I have to convert a string into binary using UTF-8, but i am unable
I am new to CSharp.I have seen this() in some code.My question is Suppose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.