Binary tree problem
Author Message
Binary tree problem

Quote:

> hi there... i'm a newbie when it comes to Pascal programming and would
> appeciate any help given...

> here is the problem.
> I need a program which will read integers until a zero is encountered.
> The integers are to be read into a sorted binary tree. Once a zero has
> inserting these integers in the tree, it searches the tree for them and
> reports either success or failure in its search.  The program should
> employ binary search.

> thanks in advance for any help...

> --
>          -=-=-= [5;1;37m Been there, Done that, Heard it !  [0m -=-=-=

You are quite welcome.
Here it is. Send me a letter if there is a problem.
____________________________________________________________________

program binarytree;

type treepointer=^treetype;
treetype=record
number:integer;
lower,higherorequal:treepointer;
end;

var number:integer;
tree:treepointer;

procedure insertnumber(number:integer;var tree:treepointer);
begin
if tree=nil then begin
new(tree);
tree^.number:=number;
tree^.lower:=nil;
tree^.higherorequal:=nil
end else if number<tree^.number then insertnumber(number,tree^.lower)
else insertnumber(number,tree^.higherorequal)
end;

function searchfornumber(number:integer;tree:treepointer):boolean;
begin
if tree=nil then searchfornumber:=false
else if tree^.number=number then searchfornumber:=true
else if number<tree^.number then
searchfornumber:=searchfornumber(number,tree^.lower)
else searchfornumber:=searchfornumber(number,tree^.higherorequal)
end;

begin
tree:=nil;
while number<>0 do begin
insertnumber(number,tree);
end;
while number<>0 do begin
if searchfornumber(number,tree) then writeln('Yep. ',number:0,' is here.')
else writeln('Nope. ',number:0,' isn''t here.');
end
end.

__________________________________________________________________________________

--
Fredrik Arnerup

url: http://www.*-*-*.com/ ~md95-far/

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS d- s:- a--- C++(+++) US P+ L(+) E W+++ N+++ o? K? w---()
O- M-- V? PS+ PE? Y PGP-
t+>++ 5- X- !R- tv b++ DI+++>++++ D+ G e- h*! r? y?
------END GEEK CODE BLOCK------

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages