Write a C program to create a mirror copy of a tree (left nodes become right and right nodes become left)!

October 28, 2007 · Filed Under Placement Questions, Tree 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

This C code will create a new mirror copy tree.

mynode *copy(mynode *root)
{
mynode *temp;

if(root==NULL)return(NULL);

temp = (mynode *) malloc(sizeof(mynode));
temp->value = root->value;

temp->left = copy(root->right);
temp->right = copy(root->left);

return(temp);
}

This code will will only print the mirror of the tree

void tree_mirror(struct node* node)
{
struct node *temp;

if (node==NULL)
{
return;
}
else
{
tree_mirror(node->left);
tree_mirror(node->right);

// Swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;
}
}

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email