Wednesday, June 18, 2008

Pre Order Traversal

Procedure Preorder (Bt)

The above Procedure traverses the binary tree using preorder traversal method. The pointers variable ‘Bt’ stores th address of the root node. The node structure is same as described before. The procedure traverses the binary tree in a recursive manner.
Step 1 Is empty ?
if (BtEmpty (Bt)) then
message : “Empty tree”
Step 2 Process the root node
if (Bt  NULL) then
display : (data (Bt))
Step 3 Traverse the left sub-tree
if (Lchild (Bt)  NULL) then
call to preorder (Lchild (Bt))
Step 4 Traverse the right sub-tree
if (Rchild (Bt)  NULL) then
call to preorder (Rchild (Bt))
Step 5 Return at the point of call
Equivalent C code for preorder traversing is given below:
void Preorder_Traversal (node * Bt)
if (Bt ! = NULL)
printf (“%d”, Bt  item);
Preorder_Traversal (Bt  Lchild);
Preorder_Traversal (Bt  Rchild);

No comments: