From 50b315cc2c12deb3cfa5bc855cd03366fd3da395 Mon Sep 17 00:00:00 2001 From: Disha167 Date: Tue, 20 Oct 2020 03:11:40 +0530 Subject: [PATCH] Create mirror_tree.cpp --- CPP/mirror_tree.cpp | 165 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 CPP/mirror_tree.cpp diff --git a/CPP/mirror_tree.cpp b/CPP/mirror_tree.cpp new file mode 100644 index 0000000..8887de0 --- /dev/null +++ b/CPP/mirror_tree.cpp @@ -0,0 +1,165 @@ +#include +using namespace std; + +/* A binary tree node has data, pointer to left child + and a pointer to right child */ +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; + +// Function to Build Tree +Node *buildTree(string str) { + // Corner Case + if (str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for (string str; iss >> str;) + ip.push_back(str); + + // Create the root of the tree + Node *root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while (!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node *currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if (currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if (i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if (currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +void mirror(struct Node* node); + +/* Helper function to test mirror(). Given a binary + search tree, print out its data elements in + increasing sorted order.*/ +void inOrder(struct Node* node) +{ + if (node == NULL) + return; + + inOrder(node->left); + printf("%d ", node->data); + + inOrder(node->right); +} + + + +/* Driver program to test size function*/ +int main() { + int tc; + scanf("%d ", &tc); + while (tc--) { + string str; + getline(cin, str); + Node *root = buildTree(str); + mirror(root); + inOrder(root); + cout << "\n"; + } + + + return 0; +}// } Driver Code Ends + + +//function Template for C++ + +/* A binary tree node has data, pointer to left child + and a pointer to right child / +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; */ + +/* Should convert tree to its mirror */ +Node* temp=new Node(0); + +void mirror(Node* node) +{ + if(node==NULL) + return ; + mirror(node->left); + //👀 👀 we do not calculate temp here as the value of temp changes in every + //recursion call stack state + mirror(node->right); + + if(node->left&&node->right) + { + temp=node->left; + node->left=node->right; + node->right=temp; + + } + else if(!node->left&&node->right) + { + + node->left=node->right; + node->right=NULL; + } + else if(node->left&&!node->right) + { + + node->right=node->left; + node->left=NULL; + } + // Your Code Here +}