Wednesday, June 18, 2008

Postorder Tree traversal

Procedure Postorder (Bt)

The above Procedure traveses a given binary tree using postorder traversal method. The pointer variable ‘Bt’ stores the address of the root node. The Node structure is same as described earlier. The Procedure traverses the binary tree in a recursive manner.
Step 1 Is empty ?
if (BtEmpty (Bt)) then
message : ‘‘Empty tree’’
return.
Step 2 Traverse the left subtree
if (Lchild (Bt)  NULL) then
call to postorder (Lchild (Bt))
Step 3 Travese the right subtree
if (Rchild (Bt)  NULL) then
call to postorder (Rchild (Bt))
Step 4 Process the root node
if (Bt  NULL) then
display : data (Bt)
Step 5 Return at the point of call
return.

code for part order travesal is given below :

void Postorder_Traversal (node*Bt)
{
if (Bt ! = NULL)
{
Postorder_Traversal (Bt  Lchild);
Postorder_Traversal (Bt  Rchild);
printf (“%d”, Bt  item);
}
}

No comments: