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
> 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...

> --
>          -=-=-= [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


       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  
 
 [ 1 post ] 

 Relevant Pages 

1. Problem with recursion and binary search trees

2. The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)

3. Stack overflow at binary tree balance

4. binary search tree

5. Binary Tree

6. How to determine the height of a binary tree

7. Binary Search tree runtime error 103

8. Binary Search Tree help needed

9. Recursion and Binary Tree

10. Recursion and Binary Tree

11. Binary tree proof.

 

 
Powered by phpBB® Forum Software