m2skills

mirror rec bt cpp

May 16th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <map>
  4. #include <list>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. // node class
  10. class node{
  11. public:
  12.     int data;
  13.     node* left;
  14.     node* right;
  15. };
  16.  
  17. // function that returns a pointer to new node
  18. node* createNode(int element){
  19.     node* temp = (node*) malloc(sizeof(node));
  20.     temp->data = element;
  21.     temp->left = NULL;
  22.     temp->right = NULL;
  23.     return temp;
  24. }
  25.  
  26. // function to print the preorder traversal of a binary tree
  27. void preorder(node* root){
  28.     if(root == NULL){
  29.         return;
  30.     }
  31.     cout<<root->data<<" ";
  32.     preorder(root->left);
  33.     preorder(root->right);
  34. }
  35.  
  36. // function to return mirror of the binary tree
  37. void mirror_tree_recursive(node *root){
  38.     if (root == NULL){
  39.         return;
  40.     }else{
  41.         // creating mirror using postorder traversal
  42.         mirror_tree_recursive(root->left);
  43.         mirror_tree_recursive(root->right);
  44.  
  45.         node* temp = root->left;
  46.         root->left = root->right;
  47.         root->right = temp;
  48.     }
  49.  
  50. }
  51.  
  52. int main() {
  53.  
  54.     node* m1 = createNode(8);
  55.     m1->left = createNode(3);
  56.     m1->right = createNode(10);
  57.     m1->left->left = createNode(1);
  58.     m1->left->right = createNode(6);
  59.     m1->left->right->left = createNode(4);
  60.     m1->left->right->right = createNode(7);
  61.     m1->right->right = createNode(14);
  62.     m1->right->right->left = createNode(13);
  63.  
  64.     node* m2 = createNode(8);
  65.     m2->right = createNode(3);
  66.     m2->left = createNode(10);
  67.     m2->left->left = createNode(14);
  68.     m2->right->left = createNode(6);
  69.     m2->right->right = createNode(1);
  70.     m2->left->left->right = createNode(13);
  71.     m2->right->left->left = createNode(7);
  72.     m2->right->left->right = createNode(4);
  73.  
  74.     cout<<"Tree #1 before mirroring : ";
  75.     preorder(m1);
  76.     cout<<endl;
  77.     cout<<"Tree #1 after mirroring : ";
  78.     mirror_tree_recursive(m1);
  79.     preorder(m1);
  80.     cout<<endl;
  81.     cout<<"\n\nTree #2 is mirror of Tree #1 to check the correctness : "<<endl;
  82.     cout<<"TREE #1 : ";
  83.     preorder(m1);
  84.     cout<<endl;
  85.     cout<<"TREE #2 : ";
  86.     preorder(m2);
  87.     cout<<endl;
  88.  
  89. }
  90.  
  91.  
  92.  
  93. /*
  94.  
  95. Tree #1 before mirroring : 8 3 1 6 4 7 10 14 13
  96. Tree #1 after mirroring : 8 10 14 13 3 6 7 4 1
  97.  
  98.  
  99. Tree #2 is mirror of Tree #1 to check the correctness :
  100. TREE #1 : 8 10 14 13 3 6 7 4 1
  101. TREE #2 : 8 10 14 13 3 6 7 4 1
  102.  
  103. Process finished with exit code 0
  104.  
  105. */
Add Comment
Please, Sign In to add comment