Board index » delphi » BINARY TREES .. Help

BINARY TREES .. Help

Hello All,

I am a student learning pascal and I would like to ask whether anyone can
help me with some source code I can study to implement a Binary Tree in
Turbo Pascal.  This is not for an assignment, but I would like to learn how
to implement a Binary Tree, how to create it and add a node to it.
Therfore, if anyone has some source code, perhaps a unit, that I may study I
would greatly appreciate it.  If not source code then at least som
pseudocode or direct me to a website that I may study.

Thanking you all in advance,

David.

 

Re:BINARY TREES .. Help


Quote
David wrote in message <374bc4b...@xenon.melitacable.com>...
>Hello All,

>I am a student learning pascal and I would like to ask whether anyone can
>help me with some source code I can study to implement a Binary Tree in
>Turbo Pascal.  This is not for an assignment, but I would like to learn how
>to implement a Binary Tree, how to create it and add a node to it.
>Therfore, if anyone has some source code, perhaps a unit, that I may study
I
>would greatly appreciate it.  If not source code then at least som
>pseudocode or direct me to a website that I may study.

>Thanking you all in advance,

>David.

Here is my advice when thinking about pointers.  Think of the data as people
recruiting more people.
The main person is the big chief who starts it all.
He made need some help so he recruits a couple of people to do a task.
"Mr A.," he states," I want you to hand all the applicants that start with
the letter 'A' to the letter 'M.'"
"Mr Z., I want you to handle the rest of them."
Mr A. sets out to do his duties and finds he is overworked and decides to go
out and find some more people to help him.  He finds Mr B.  and Mr C.
Mr B. has a degree in finding names from "A" to "F" and sets out to do that.
Mr C. handles the rest.
The big chief sees that he has a new applicant and it fits in the "J"
catagory and hands it off to Mr A for processing.
Mr A. thenpasses it off to Mr C. and it get processed.
Papers get passed around all day in this manner and eventually the head
chief thanks Mr A for doing such a fine job, but of course he does not know
that the workload  was split up.
They each only know who it goes to next, they don't need to tell the person
before them where the papers are going as long as the job gets done.

---

For a singly linked list, think of it as central person sending a letter
with only his return address given.  A mail person picks it up and sticks it
in another bin.  Another mail person picks up the letter, not knowing who
the previous mail person is, and brings it to the next bin.  This process
continues until it reaches it's destination.
At no point did any mail carrier no who brought it that far, but knew where
to take it.  If they wanted to leave a message to a mail carrier before
them, the only way they could do this is to follow the trail from the return
address on the envelope and follow its path to him.

A double linked list is basically like a letter, but this time it gets
marked with ink at each location it visits.  Now if the carrier wanted to
know who was before him, he would just look for the markings to see who had
it last.  So if the letter had an invalid address number, he could just give
it to the person who gave it to him.

A Circular linked list is basically what it says, if you followed the linked
list, you would eventually meet the person who started it.

Hope this helps...

Re:BINARY TREES .. Help


Quote
David wrote in message <374bc4b...@xenon.melitacable.com>...
>Hello All,

>I am a student learning pascal and I would like to ask whether anyone can
>help me with some source code I can study to implement a Binary Tree in
>Turbo Pascal.  This is not for an assignment, but I would like to learn how
>to implement a Binary Tree, how to create it and add a node to it.
>Therfore, if anyone has some source code, perhaps a unit, that I may study
I
>would greatly appreciate it.  If not source code then at least som
>pseudocode or direct me to a website that I may study.

>Thanking you all in advance,

>David.

As requested:

Program BinarySort;
{based on "Trees" pp 492..497, "PASCAL PROGRAMMING, A Spiral Approach"
turbo v6.0    <clifp...@airmail.net> Nov 2, 1997}

USES CRT;

CONST MaxNodes = 1000;  {for demo purposes}

TYPE
bPtr = ^node;
node = Record
         num:Integer;
         left, right:bPtr;
       End;
VAR
   ct, n:Integer;
   IntTree:bPtr;

Procedure InsertNum(VAR tree:bPtr; j:Integer);
VAR newitem:bPtr;
Begin
     If tree = NIL then
     Begin
          NEW(newitem);
          newitem^.num := j;
          newitem^.left := NIL;
          newitem^.right := NIL;
          tree := newitem;
     End
     Else
     Begin
          If j < tree^.num then InsertNum(tree^.left, j)
          Else InsertNum(tree^.right, j);
     End;
End;

Procedure PrintTree(tree:bPtr);
Begin
     If tree <> NIL then
     Begin
          Inc(ct);
          If ct mod 448 = 0 then
          Begin
               writeln; writeln('Press <Enter>':30);
               readln;
          End;
          PrintTree(tree^.left);
          Write(tree^.num:4);
          printtree(tree^.right);
     End;
End;

Begin
     ClrScr;
     Randomize;
     IntTree := NIL;
     For ct := 1 to MaxNodes Do
     Begin
          n := Random(1000);
          InsertNum(IntTree, n);
     End;
     ct := 1;
     PrintTree(IntTree);
     Writeln;
readln;
End.

Other Threads