## Binary tree problem

##### Quote
Co Tran wrote:

> 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...
> email preferred: ct...@st.nepean.uws.edu.au
> --
>          -=-=-= [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