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
> been ecountered the program keeps reading integers. Instead of
> 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;
readln(number);
while number<>0 do begin
insertnumber(number,tree);
readln(number)
end;
readln(number);
while number<>0 do begin
if searchfornumber(number,tree) then writeln('Yep. ',number:0,' is here.')
else writeln('Nope. ',number:0,' isn''t here.');
readln(number);
end
end.
__________________________________________________________________________________
--
Fredrik Arnerup
e-mail: md95-...@nada.kth.se
url: http://www.nada.kth.se/~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------