jayati

normal bst to balanced but

Aug 21st, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     struct Node *left;
  9.     struct Node *right;
  10.    
  11.     Node(int x){
  12.         data = x;
  13.         left = NULL;
  14.         right = NULL;
  15.     }
  16. };
  17.  
  18. Node* insert(struct Node* node, int key){
  19.     if (node == NULL) return new Node(key);
  20.     if (key < node->data)
  21.         node->left = insert(node->left, key);
  22.     else if (key > node->data)
  23.         node->right = insert(node->right, key);
  24.     return node;
  25. }
  26.  
  27. void preOrder(Node* node)
  28. {
  29.     if (node == NULL)return;
  30.     printf("%d ", node->data);
  31.     preOrder(node->left);
  32.     preOrder(node->right);
  33. }
  34.  
  35. int height(struct Node* node)
  36. {
  37.   if (node==NULL) return 0;
  38.   else
  39.   {
  40.       int lDepth = height(node->left);
  41.       int rDepth = height(node->right);
  42.       if (lDepth > rDepth) return(lDepth+1);
  43.       else return(rDepth+1);
  44.   }
  45. return 2;
  46. }
  47. Node *buildTree(string str) {
  48.     // Corner Case
  49.     if (str.length() == 0 || str[0] == 'N')
  50.         return NULL;
  51.  
  52.     // Creating vector of strings from input
  53.     // string after spliting by space
  54.     vector<string> ip;
  55.  
  56.     istringstream iss(str);
  57.     for (string str; iss >> str;)
  58.         ip.push_back(str);
  59.  
  60.     // Create the root of the tree
  61.     Node *root = new Node(stoi(ip[0]));
  62.  
  63.     // Push the root to the queue
  64.     queue<Node *> queue;
  65.     queue.push(root);
  66.  
  67.     // Starting from the second element
  68.     int i = 1;
  69.     while (!queue.empty() && i < ip.size()) {
  70.  
  71.         // Get and remove the front of the queue
  72.         Node *currNode = queue.front();
  73.         queue.pop();
  74.  
  75.         // Get the current node's value from the string
  76.         string currVal = ip[i];
  77.  
  78.         // If the left child is not null
  79.         if (currVal != "N") {
  80.  
  81.             // Create the left child for the current node
  82.             currNode->left = new Node(stoi(currVal));
  83.  
  84.             // Push it to the queue
  85.             queue.push(currNode->left);
  86.         }
  87.  
  88.         // For the right child
  89.         i++;
  90.         if (i >= ip.size())
  91.             break;
  92.         currVal = ip[i];
  93.  
  94.         // If the right child is not null
  95.         if (currVal != "N") {
  96.  
  97.             // Create the right child for the current node
  98.             currNode->right = new Node(stoi(currVal));
  99.  
  100.             // Push it to the queue
  101.             queue.push(currNode->right);
  102.         }
  103.         i++;
  104.     }
  105.  
  106.     return root;
  107. }
  108.  
  109. Node* buildBalancedTree(Node* root);
  110.  
  111. int main()
  112. {
  113.     int t;
  114.     cin>>t;
  115.     getchar();
  116.     while(t--)
  117.     {
  118.         struct Node *root = NULL;
  119.         int n, temp;
  120.         string tree;
  121.         getline(cin,tree);
  122.         root = buildTree(tree);
  123.         // cout<<height(root)<<endl;
  124.         root = buildBalancedTree(root);
  125.         cout<<height(root)<<endl;
  126.     }
  127.     return 0;
  128. }
  129.  
  130. // } Driver Code Ends
  131.  
  132.  
  133. /*Structure of the Node of the BST is as
  134. struct Node
  135. {
  136.     int data;
  137.     Node* left, *right;
  138. };
  139. */
  140. // Your are required to complete this function
  141. // function should return root of the modified BST
  142. Node* buildbst(vector<Node*> &v,int start,int end)
  143. {
  144.     if (start > end)
  145.         return NULL;
  146.     int mid=(start+end)/2;
  147.     Node* root=v[mid];
  148.     root->left = buildbst(v,start,mid-1);
  149.     root->right = buildbst(v,mid+1,end);
  150.     return root;
  151. }
  152.  
  153. void storearray(Node* root,vector<Node*> &v)
  154. {
  155.     if(root==NULL)
  156.     {
  157.         return;
  158.     }
  159.     storearray(root->left,v);
  160.     v.push_back(root);
  161.     storearray(root->right,v);
  162. }
  163. Node* buildBalancedTree(Node* root)
  164. {
  165.     vector<Node*> v;
  166.     storearray(root,v);
  167.     int n=v.size();
  168.     root=buildbst(v,0,n-1);
  169.     return root;
  170. }
  171. // https://practice.geeksforgeeks.org/problems/normal-bst-to-balanced-bst/
Advertisement
Add Comment
Please, Sign In to add comment