Обход Это не показывает обход исходного BST, а показывает зеркальный Обход BST.
в
void BST::actmirror(node* current)
{
node* holder=new node;
if(current==NULL)
{
return ;
}
holder=current->left;
current->left=current->right;
current->right=holder;
actmirror(current->left);
actmirror(current->right);
}
выделенныйновый узел потерян из-за holder=current->left;
, и вы изменяете текущий , а не новый узел, как сказал drescherjm в замечании
Предложение: вclass BST изменить сигнатуру BST :: actmirror на node* BST::actmirror(node* current)
, а его определение:
node* BST::actmirror(node* current)
{
if(current==NULL)
{
return 0;
}
node* holder=new node;
holder->data = current->data;
holder->left = actmirror(current->right);
holder->right = actmirror(current->left);
return holder;
}
, как вы видите, что делает зеркало, созданное вв то же время скопируйте
и обновите BST :: mirror () следующим образом:
void BST::mirror()
{ node * copy1 = actmirror(root);
cout<<"Mirror Image of given tree is Created!"<<endl;
cout<<"\nInorder Travesral Is = "<<endl;
inorder(copy1);
cout<<"\nPreorder Travesral Is = "<<endl;
preorder(copy1);
cout<<"\nPostorder Travesral Is = "<<endl;
postporder(copy1);
// WARNING here you need to delete the copy !
}
Как я уже сказал в замечании, лучше, если зеркало сделаетСкопировать и зеркало одновременно, я сделал это выше.
И теперь исполнение:
Enter Your Choice =
1.Insert
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Search an element in tree
6.Find Maximum element in tree
7.Find Minimum elemnt in tree
8.Total No Of Nodes In Longest Path
9.Mirror Image = 1
Enter no you want to insert = 5
Do You want to continue to insert data(y/n) = y
Enter no you want to insert = 6
Do You want to continue to insert data(y/n) = y
Enter no you want to insert = 7
Do You want to continue to insert data(y/n) = y
Enter no you want to insert = 2
Do You want to continue to insert data(y/n) = y
Enter no you want to insert = 3
Do You want to continue to insert data(y/n) = n
Do you want to continue opreations(y/n) = y
Enter Your Choice =
1.Insert
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Search an element in tree
6.Find Maximum element in tree
7.Find Minimum elemnt in tree
8.Total No Of Nodes In Longest Path
9.Mirror Image = 2
---------------------------------------------------
Inorder Treaversal =
2 3 5 6 7
---------------------------------------------------
Do you want to continue opreations(y/n) = y
Enter Your Choice =
1.Insert
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Search an element in tree
6.Find Maximum element in tree
7.Find Minimum elemnt in tree
8.Total No Of Nodes In Longest Path
9.Mirror Image = 9
Mirror Image of given tree is Created!
Inorder Travesral Is =
7 6 5 3 2
Preorder Travesral Is =
5 6 7 2 3
Postorder Travesral Is =
7 6 3 2 5
---------------------------------------------------
Do you want to continue opreations(y/n) = y
Enter Your Choice =
1.Insert
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Search an element in tree
6.Find Maximum element in tree
7.Find Minimum elemnt in tree
8.Total No Of Nodes In Longest Path
9.Mirror Image = 2
---------------------------------------------------
Inorder Treaversal =
2 3 5 6 7
---------------------------------------------------
Do you want to continue opreations(y/n) = n