I wrote a small routines to create a mirror for any binary tree. I am just sharing my necessary routines instead of full class. could you please let me know your comments on this. I just curious to find the complexity of this but I am new to algorithms.
void tree_sample::mirror_image()
{
create_mirror_for_left_subtree(root->left);
create_mirror_for_right_subtree(root->right);
tree_node* temp = 0;
if(root->left)
temp = root->left;
root->left = root->right;
if(temp)
root->right = temp;
else
root->right = NULL;
}
void tree_sample::create_mirror_for_left_subtree(tree_node *temp)
{
while(temp!=NULL)
{
do_mirror_for_inidividual_node(temp);
temp = temp->left;
}
}
void tree_sample::create_mirror_for_right_subtree(tree_node *temp)
{
while(temp!=NULL)
{
do_mirror_for_inidividual_node(temp);
temp = temp->right;
}
}
void tree_sample::do_mirror_for_inidividual_node(tree_node *temp)
{
tree_node* temp2 = 0;
if(temp->left)
temp2 = temp->left;
temp->left = temp->right;
if(temp2)
temp->right = temp2;
else
temp->right= 0;
}