Solved

Binary Trees in Pascal

Posted on 2003-03-26
Medium Priority
3,921 Views
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);
newnode^.data := s;
Insert_Into_Tree (newnode, root);

end;
close (input);

end;

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

begin

Build_Tree;

end.
0
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
• 2

LVL 101

Accepted Solution

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

ID: 9446753
Creamer72:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 101

Expert Comment

ID: 9447765

mlmcc
0

Featured Post

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
Course of the Month13 days, 22 hours left to enroll