Saturday, August 11, 2007

Create Mirror of Binary Tree.

Following is programme to create Mirror of given input Binary Tree.
i.e. Left Subtree will become Right Subtree and Right Subtree will become Left subtree.

struct Binary_tree
{
char info;
Binary_tree *left, *right;
};

Binary_tree * CreateMirror(Binary_tree *root)
{

/* Check if root is Null */
if (root == NULL)
{
return NULL;
}

/* Allocate Memore for Binary_tree Pointer*/
mirror = CreateNode() ;

mirror-> info = root-> info;

mirror-> left = mirror -> right = NULL;

/* Assign Left Subtree to Right Subtree of Mirror */
mirror->right = CreateMirror (root-> left);

/* Assign Right Subtree to Left Subtree of Mirror */
mirror->left = CreateMirror (root-> right);

return mirror;
}