jayati

Binary Tree to DLL

Oct 11th, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Tree Node
  6. struct Node
  7. {
  8.     int data;
  9.     Node* left;
  10.     Node* right;
  11.     Node(){
  12.         left = right = NULL;
  13.     }
  14.     Node(int x){
  15.         data = x;
  16.         left = right = NULL;
  17.     }
  18. };
  19.  
  20. // Utility function to create a new Tree Node
  21. Node* newNode(int val)
  22. {
  23.     Node* temp = new Node;
  24.     temp->data = val;
  25.     temp->left = NULL;
  26.     temp->right = NULL;
  27.    
  28.     return temp;
  29. }
  30.  
  31. // Function to Build Tree
  32. Node* buildTree(string str)
  33. {  
  34.     // Corner Case
  35.     if(str.length() == 0 || str[0] == 'N')
  36.             return NULL;
  37.    
  38.     // Creating vector of strings from input
  39.     // string after spliting by space
  40.     vector<string> ip;
  41.    
  42.     istringstream iss(str);
  43.     for(string str; iss >> str; )
  44.         ip.push_back(str);
  45.        
  46.     // Create the root of the tree
  47.     Node* root = newNode(stoi(ip[0]));
  48.        
  49.     // Push the root to the queue
  50.     queue<Node*> queue;
  51.     queue.push(root);
  52.        
  53.     // Starting from the second element
  54.     int i = 1;
  55.     while(!queue.empty() && i < ip.size()) {
  56.            
  57.         // Get and remove the front of the queue
  58.         Node* currNode = queue.front();
  59.         queue.pop();
  60.            
  61.         // Get the current node's value from the string
  62.         string currVal = ip[i];
  63.            
  64.         // If the left child is not null
  65.         if(currVal != "N") {
  66.                
  67.             // Create the left child for the current node
  68.             currNode->left = newNode(stoi(currVal));
  69.                
  70.             // Push it to the queue
  71.             queue.push(currNode->left);
  72.         }
  73.            
  74.         // For the right child
  75.         i++;
  76.         if(i >= ip.size())
  77.             break;
  78.         currVal = ip[i];
  79.            
  80.         // If the right child is not null
  81.         if(currVal != "N") {
  82.                
  83.             // Create the right child for the current node
  84.             currNode->right = newNode(stoi(currVal));
  85.                
  86.             // Push it to the queue
  87.             queue.push(currNode->right);
  88.         }
  89.         i++;
  90.     }
  91.    
  92.     return root;
  93. }
  94.  
  95.  
  96.  
  97. // } Driver Code Ends
  98. /* Structure for tree and linked list
  99.  
  100. struct Node
  101. {
  102.     int data;
  103.     struct Node* left;
  104.     struct Node* right;
  105.    
  106.     Node(int x){
  107.         data = x;
  108.         left = right = NULL;
  109.     }
  110. };
  111.  */
  112.  
  113. // This function should return head to the DLL
  114. class Solution
  115. {
  116.     public:
  117.     //Function to convert binary tree to doubly linked list and return it.
  118.     Node* prev=NULL;
  119.     Node* head=NULL;
  120.     Node * bToDLL(Node *root)
  121.     {
  122.         // your code here
  123.         if(root==NULL)
  124.         {
  125.             return NULL;
  126.         }
  127.         bToDLL(root->left);
  128.         if(prev==NULL)
  129.         {
  130.             head=root;
  131.         }
  132.         else
  133.         {
  134.             root->left=prev;
  135.             prev->right=root;
  136.         }
  137.         prev=root;
  138.         bToDLL(root->right);
  139.        return head;
  140.     }
  141. };
  142.  
  143.  
  144.  
  145. //{ Driver Code Starts.
  146.  
  147.  
  148. /* Function to print nodes in a given doubly linked list */
  149. void printList(Node *node)
  150. {
  151.     Node *prev = NULL;
  152.     while (node!=NULL)
  153.     {
  154.         cout << node->data << " ";
  155.         prev = node;
  156.         node = node->right;
  157.     }
  158.     cout << endl;
  159.     while (prev!=NULL)
  160.     {
  161.         cout << prev->data << " ";
  162.         prev = prev->left;
  163.     }
  164.     cout << endl;
  165. }
  166.  
  167. void inorder(Node *root)
  168. {
  169.    if (root != NULL)
  170.    {
  171.        inorder(root->left);
  172.        cout << root->data;
  173.        inorder(root->right);
  174.    }
  175. }
  176.  
  177. /* Driver program to test size function*/
  178. int main()
  179. {
  180.   int t;
  181.   cin >> t;
  182.   getchar();
  183.  
  184.   while (t--)
  185.   {
  186.      string inp;
  187.      getline(cin, inp);
  188.      Node *root = buildTree(inp);
  189.      
  190.      Solution ob;
  191.      Node *head = ob.bToDLL(root);
  192.      printList(head);
  193.  
  194.   }
  195.   return 0;
  196. }
  197.  
  198.  
  199. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment