?
Solved

Binary Trees in Pascal

Posted on 2003-03-26
4
Medium Priority
?
3,921 Views
Last Modified: 2012-08-14
I want to build a binary tree in pascal from data in a file and then traverse it In-Order and display on screen. So far i have the following but am a bit confused about what to pass into the Traversal procedure as a nodepointer. Any ideas?

Program BinarySearchTrees;


type nodepointer = ^node;

     node = record
     data : string[30];
     left : nodepointer;
     right : nodepointer;
end;


const Datafile = 'C:\MYDOCU~1\UNI\DATAST~1\ASSIGN~1\WORDS425.TXT';

{------------------------------------------------------------------}

Procedure InOrderTraversal (root : nodepointer) ;

begin
   If root^.left <> nil then begin
      InOrderTraversal (root^.left);
      writeln (root^.data);
      end;
   If root^.right <> nil then
      InOrderTraversal (root^.right);

end;

{------------------------------------------------------------------}


Procedure Insert_Into_Tree (n : nodepointer; var root : nodepointer);

begin
     if root = nil then
        root := n
     else begin
        if n^.data > root^.data then
           Insert_Into_Tree(n, root^.right)
        else
           Insert_Into_Tree(n, root^.left)
     end;


end;

{------------------------------------------------------------------}

Procedure Build_Tree;

var s : string;
    newnode: nodepointer;
    root : nodepointer;
begin
     root := nil ;
     assign (input, DataFile);
     reset (input);
     while not eof(input) do begin
           new(newnode);
           readln (input, s);
           newnode^.data := s;
           Insert_Into_Tree (newnode, root);

     end;
     close (input);
     
end;

{---------------------------------------------------------}


begin

Build_Tree;

end.
0
Comment
Question by:Creamer72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 101

Accepted Solution

by:
mlmcc earned 300 total points
ID: 8214370
Buildtree needs to have the root pointer passed in otherwise when it ends the tree will also go away.

Procedure Build_Tree (var Root : nodepointer);

var TreeRoot : nodepointer;

begin

Build_Tree (TreeRoot);

end.

Now you can pass TreeRoot to the traversal procedure.

mlmcc
0
 

Expert Comment

by:CleanupPing
ID: 9446753
Creamer72:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 9447765
I believe I answered it.

mlmcc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question