Article...

Kamis, 05 Juni 2008

Binary Search

{Performs a binary search}
TYPE
tree =^node;
node = record
info : char;
left, right : tree
end;
VAR root: tree;
Number: integer;
{$I Tree }
{Activates: Binary_Tree, Infix, subTree, Height}

procedure search( t : tree; var found : boolean; x : char);
{pre: t points to a tree
post : each node is visited and there n is incremented}

begin
if (t <> nil) and not found then
if x = t^.info then begin
found := true;
writeln( x, ' was found')
end
else if x < t^.info then
search( t^.left, found, x)
else
search( t^.right, found, x)
end;

var found: boolean;
x : char;
BEGIN{main}
Binary_Tree(root);
Infix(root);
writeln;
found := false;
writeln('What value of x do you want?');
readln( x );
search( root, found, x);
writeln(x, ' was found is ', found)
END.

My Headlines