Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Trees
Home > Programs > Trees

43. Write a program to convert the tree into its mirror

Answer:

#include<stdio.h>

#include<stdlib.h>

/* A binary tree node has data, pointer to left child 
and a pointer to right child */
struct node 
{
    int data;
    struct node* left;
    struct node* right;
};

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)

{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

void mirror(struct node* node) 
{
if (node==NULL) 
    return; 
else
{
    struct node* temp;
    
    /* do the subtrees */
    mirror(node->left);
    mirror(node->right);

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



/* Helper function to test mirror(). Given a binary
search tree, print out its data elements in 
increasing sorted order.*/
void inOrder(struct node* node) 
{
if (node == NULL) 
    return;

inOrder(node->left);
printf("%d ", node->data);

inOrder(node->right);



/* Driver program to test mirror() */
int main()
{
struct node *root = newNode(1);
root->left     = newNode(2);
root->right     = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5); 

/* Print inorder traversal of the input tree */
printf("\n Inorder traversal of the constructed tree is \n");
inOrder(root);

/* Convert tree to its mirror */
mirror(root); 

/* Print inorder traversal of the mirror tree */
printf("\n Inorder traversal of the mirror tree is \n"); 
inOrder(root);

getchar();
return 0; 
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here: