|
| 1 | +#include <iostream> |
| 2 | +#include<queue> |
| 3 | +#include<climits> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +class node{ |
| 7 | + public: |
| 8 | + int data; |
| 9 | + node*left; |
| 10 | + node*right; |
| 11 | + |
| 12 | + node(int d){ |
| 13 | + data = d; |
| 14 | + left = NULL; |
| 15 | + right = NULL; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +//Accepts the old root node & data and returns the new root node |
| 20 | +node* insertInBST(node *root,int data){ |
| 21 | + //Base Case |
| 22 | + if(root==NULL){ |
| 23 | + return new node(data); |
| 24 | + } |
| 25 | + //Rec Case - Insert in the Subtree and Update Pointers |
| 26 | + if(data<=root->data){ |
| 27 | + root->left = insertInBST(root->left,data); |
| 28 | + } |
| 29 | + else{ |
| 30 | + root->right = insertInBST(root->right,data); |
| 31 | + } |
| 32 | + return root; |
| 33 | +} |
| 34 | + |
| 35 | +bool search(node*root,int data){ |
| 36 | + if(root==NULL){ |
| 37 | + return false; |
| 38 | + } |
| 39 | + if(root->data==data){ |
| 40 | + return true; |
| 41 | + } |
| 42 | + //Recursively search in left and right subtree |
| 43 | + if(data<=root->data){ |
| 44 | + return search(root->left,data); |
| 45 | + } |
| 46 | + else{ |
| 47 | + return search(root->right,data); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +node* build(){ |
| 52 | + //Read a list of numbers till -1 and also these numbers will be inserted into BST |
| 53 | + int d; |
| 54 | + cin>>d; |
| 55 | + |
| 56 | + node*root = NULL; |
| 57 | + |
| 58 | + while(d!=-1){ |
| 59 | + root = insertInBST(root,d); |
| 60 | + cin>>d; |
| 61 | + } |
| 62 | + return root; |
| 63 | +} |
| 64 | +//Print the BST Level By Level |
| 65 | +void bfs(node *root){ |
| 66 | + queue<node*> q; |
| 67 | + q.push(root); |
| 68 | + q.push(NULL); |
| 69 | + |
| 70 | + while(!q.empty()){ |
| 71 | + node* f = q.front(); |
| 72 | + if(f==NULL){ |
| 73 | + cout<<endl; |
| 74 | + q.pop(); |
| 75 | + if(!q.empty()){ |
| 76 | + q.push(NULL); |
| 77 | + } |
| 78 | + } |
| 79 | + else{ |
| 80 | + cout<<f->data<<","; |
| 81 | + q.pop(); |
| 82 | + |
| 83 | + if(f->left){ |
| 84 | + q.push(f->left); |
| 85 | + } |
| 86 | + if(f->right){ |
| 87 | + q.push(f->right); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return; |
| 92 | +} |
| 93 | +//Inorder Print |
| 94 | +void inorder(node*root){ |
| 95 | + if(root==NULL){ |
| 96 | + return; |
| 97 | + } |
| 98 | + inorder(root->left); |
| 99 | + cout<<root->data<<","; |
| 100 | + inorder(root->right); |
| 101 | +} |
| 102 | + |
| 103 | +node* deleteInBST(node*root,int data){ |
| 104 | + if(root==NULL){ |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + else if(data<root->data){ |
| 108 | + root->left = deleteInBST(root->left,data); |
| 109 | + return root; |
| 110 | + } |
| 111 | + else if(data==root->data){ |
| 112 | + //Found the node to delete 3 Cases |
| 113 | + //1. Node with 0 children - Leaf Node |
| 114 | + if(root->left==NULL && root->right==NULL){ |
| 115 | + delete root; |
| 116 | + return NULL; |
| 117 | + } |
| 118 | + //2. Case Only 1 child |
| 119 | + if(root->left!=NULL && root->right==NULL){ |
| 120 | + node* temp = root->left; |
| 121 | + delete root; |
| 122 | + return temp; |
| 123 | + } |
| 124 | + if(root->right!=NULL && root->left==NULL){ |
| 125 | + node* temp = root->right; |
| 126 | + delete root; |
| 127 | + return temp; |
| 128 | + } |
| 129 | + //3. Case 2 children |
| 130 | + node *replace = root->right; |
| 131 | + //Find the inorder successor from right subtree |
| 132 | + while(replace->left!=NULL){ |
| 133 | + replace = replace->left; |
| 134 | + } |
| 135 | + root->data = replace->data; |
| 136 | + root->right = deleteInBST(root->right,replace->data); |
| 137 | + return root; |
| 138 | + } |
| 139 | + else{ |
| 140 | + root->right = deleteInBST(root->right,data); |
| 141 | + return root; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +bool isBST(node *root,int minV = INT_MIN,int maxV = INT_MAX){ |
| 146 | + if(root==NULL){ |
| 147 | + return true; |
| 148 | + } |
| 149 | + if(root->data >= minV && root->data<=maxV && isBST(root->left,minV,root->data) && isBST(root->right,root->data,maxV)){ |
| 150 | + return true; |
| 151 | + } |
| 152 | + return false; |
| 153 | +} |
| 154 | + |
| 155 | +class LinkedList{ |
| 156 | + public: |
| 157 | + node*head; |
| 158 | + node*tail; |
| 159 | +}; |
| 160 | + |
| 161 | +LinkedList flatten(node*root){ |
| 162 | + LinkedList l; |
| 163 | + |
| 164 | + if(root==NULL){ |
| 165 | + l.head = l.tail = NULL; |
| 166 | + return l; |
| 167 | + } |
| 168 | + // Leaf Node |
| 169 | + if(root->left == NULL && root->right==NULL){ |
| 170 | + l.head = l.tail = root; |
| 171 | + return l; |
| 172 | + } |
| 173 | + // Left is Not NULL |
| 174 | + if(root->left!=NULL && root->right==NULL){ |
| 175 | + LinkedList leftLL = flatten(root->left); |
| 176 | + leftLL.tail->right = root; |
| 177 | + |
| 178 | + l.head = leftLL.head; |
| 179 | + l.tail = root; |
| 180 | + return l; |
| 181 | + } |
| 182 | + //Right is Not NULL |
| 183 | + if(root->left==NULL && root->right!=NULL){ |
| 184 | + LinkedList rightLL = flatten(root->right); |
| 185 | + root->right = rightLL.head; |
| 186 | + |
| 187 | + l.head = root; |
| 188 | + l.tail = rightLL.tail; |
| 189 | + return l; |
| 190 | + } |
| 191 | + //Both Sides are not NULL |
| 192 | + LinkedList leftLL = flatten(root->left); |
| 193 | + LinkedList rightLL = flatten(root->right); |
| 194 | + |
| 195 | + leftLL.tail->right = root; |
| 196 | + root->right = rightLL.head; |
| 197 | + |
| 198 | + l.head = leftLL.head; |
| 199 | + l.tail = rightLL.tail; |
| 200 | + return l; |
| 201 | + |
| 202 | +} |
| 203 | + |
| 204 | +int main(){ |
| 205 | + node*root = build(); |
| 206 | + inorder(root); |
| 207 | + cout<<endl; |
| 208 | + bfs(root); |
| 209 | + cout<<endl; |
| 210 | + |
| 211 | + LinkedList l = flatten(root); |
| 212 | + node*temp = l.head; |
| 213 | + |
| 214 | + while(temp!=NULL){ |
| 215 | + cout<< temp->data <<" --> "; |
| 216 | + temp = temp->right; |
| 217 | + } |
| 218 | + cout<<endl; |
| 219 | + cout<<endl; |
| 220 | + cout<<endl; |
| 221 | + cout<<endl; |
| 222 | + cout<<endl; |
| 223 | + |
| 224 | + |
| 225 | +return 0; |
| 226 | +} |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | + |
0 commit comments