m2skills

mirror iter bt cpp

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