hi i'm trying to write program to create a mirror print of a binary tree
the code is :

typedef struct Node {
	
	int info ;
	
	struct Node *left , *right ;
	
} Node ;

void swapNode ( Node *n1 , Node *n2 ) {
		
		Node *tmp = NULL ;
		
		tmp = n1 ;
		
		n1 = n2 ;
		
		n2 = tmp ;
		
	}
	
	void mirror ( Node *root ) {
		
		if ( !root )
			
			return ;
			
		mirror ( root -> left ) ;
		
		swapNode ( root -> left , root -> right ) ;
		
		mirror ( root -> right ) ;
		
		}

it is not working
can anyone tell me why ?

Recommended Answers

All 2 Replies

>can anyone tell me why ?
swapNode doesn't do anything. When you want to change a value, you pass a pointer to that value, right?

/* Changing the original objects that a and b point to */
void swap ( int *a, int *b )
{
  int save = *a;
  *a = *b;
  *b = save;
}

You do that because not using a pointer to get the the value you want to modify will result in changing just the local variables:

/* Just changing a and b, not the original objects */
void swap ( int a, int b )
{
  int save = a;
  a = b;
  b = save;
}

The principle is the same when you want to modify a pointer value:

/* Just changing a and b, not the original pointers */
void swap ( int *a, int *b )
{
  int *save = a;
  a = b;
  b = save;
}

Just as with the integers, you have to pass a pointer to the value you want to modify:

/* Changing the original pointers that a and b point to */
void swap ( int **a, int **b )
{
  int *save = *a;
  *a = *b;
  *b = save;
}

thank you it really hellped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.