Wednesday, June 18, 2008

Binary Tree Search

Btsearch (Bt, k)

The above Function searches the desired element from a given binary tree, and returns ‘true’ if matches. The pointer variable ‘Bt’ holds the address of the root node. Variable ‘k’ holds the key-value of the element to be searched. The Function works in a recursive manner. The structure of a node is same as described earlier.

Step 1 Is Empty ?
if (BtEmpty (Bt)) then
message : “Empty tree”
return false
Step 2 Root node is the node to be searched
if (data (Bt) = k) then
return true
Step 3 If key value is less than root
if (k < data (Bt)) then
return (Btsearch (Lchild (Bt), k))
Step 4 If key value is greater than root.
if (k > data (Bt)) then
return (Btsearch (Rchild (Bt), K))

No comments: