Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. // AVL Implemented
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct BstNode{
  6. int data;
  7. BstNode *left,*right;
  8. int height;
  9. BstNode(int d)
  10. {
  11. data=d;
  12. left=NULL;
  13. right=NULL;
  14. height=0;
  15. }
  16. };
  17.  
  18. int height(BstNode *N){
  19. if(N==NULL){
  20. return -1;
  21. }
  22. return N->height;
  23. }
  24.  
  25. BstNode * L_rotate(BstNode *root){
  26. BstNode *l=root->left;
  27. BstNode *temp=l->right;
  28. l->right=root;
  29. root->left=temp;
  30. // Update Heights
  31. root->height=max(height(root->left),height(root->right))+1;
  32. l->height=max(height(l->left),height(l->right))+1;
  33. return l;
  34. }
  35.  
  36. BstNode * R_rotate(BstNode *root){
  37. BstNode *r=root->right;
  38. BstNode *temp=r->left;
  39. r->left=root;
  40. root->right=temp;
  41. // Update Heights
  42. root->height=max(height(root->left),height(root->right))+1;
  43. r->height=max(height(r->left),height(r->right))+1;
  44. return r;
  45. }
  46.  
  47. int getBalance(BstNode *N)
  48. {
  49. if (N == NULL)
  50. return 0;
  51. return height(N->left) - height(N->right);
  52. }
  53.  
  54. BstNode * Insert(BstNode *root,int value){
  55. BstNode *temp=new BstNode(value);
  56. if(root==NULL){
  57. root=temp;
  58. }
  59. else if(value <= root->data){
  60. root->left=Insert(root->left,value);
  61. }
  62. else
  63. {
  64. root->right=Insert(root->right,value);
  65. }
  66. root->height=max(height(root->left),height(root->right))+1;
  67. // AVL Implementation
  68. // check balance or not
  69. int balance=getBalance(root);
  70. // if unbalanced
  71. // Left case
  72. if(balance>1 && value < root->left->data){
  73. return L_rotate(root);
  74. }
  75. // Right case
  76. if(balance<-1 && value > root->right->data){
  77. return R_rotate(root);
  78. }
  79. // Right Left case
  80. if(balance>1 && value > root->left->data){
  81. root->left = R_rotate(root->left);
  82. return L_rotate(root);
  83. }
  84. // Left Right case
  85. if(balance<-1 && value < root->right->data){
  86. root->right = L_rotate(root->right);
  87. return R_rotate(root);
  88. }
  89. return root;
  90. }
  91.  
  92. bool Search(BstNode *root,int value){
  93. if(root==NULL){
  94. return false;
  95. }
  96. else if(root->data==value){
  97. return true;
  98. }
  99. else if(root->data < value)
  100. {
  101. return Search(root->left,value);
  102. }
  103. else
  104. {
  105. return Search(root->right,value);
  106. }
  107. }
  108.  
  109. int findMin(BstNode *root){
  110. if(root==NULL){
  111. cout<<"BST is empty";
  112. return -1;
  113. }
  114. else if (root->left==NULL)
  115. {
  116. return root->data;
  117. }
  118. return findMin(root->left);
  119. }
  120.  
  121. void LevelOrder(BstNode *root){
  122. if(root==NULL){
  123. return;
  124. }
  125. queue<BstNode *>q;
  126. q.push(root);
  127. while(!q.empty()){
  128. BstNode * curr=q.front();
  129. cout<<curr->data<<" ";
  130. if(curr->left!=NULL){
  131. q.push(curr->left);
  132. }
  133. if(curr->right!=NULL){
  134. q.push(curr->right);
  135. }
  136. q.pop();
  137. }
  138. }
  139.  
  140. void Inorder(BstNode *root){
  141. if(root==NULL){
  142. return;
  143. }
  144. Inorder(root->left);
  145. cout<<root->data<<" ";
  146. Inorder(root->right);
  147. }
  148.  
  149. bool BstUtil(BstNode * root,int min,int max){
  150. if(root==NULL){
  151. return true;
  152. }
  153. if((root->data <= max)&&(root->data > min) &&
  154. BstUtil(root->left,min,root->data) &&
  155. BstUtil(root->right,root->data,max)){
  156. return true;
  157. }
  158. else{
  159. return false;
  160. }
  161. }
  162.  
  163. bool isBst(BstNode * root){
  164. return BstUtil(root,INT_MIN,INT_MAX);
  165. }
  166.  
  167. BstNode * Delete(BstNode *root,int value){
  168. if(root==NULL){
  169. return root;
  170. }
  171. else if(value < root->data){
  172. root->left=Delete(root->left,value);
  173. }
  174. else if(value > root->data){
  175. root->right=Delete(root->right,value);
  176. }
  177. else // I find you & going to delete you
  178. {
  179. // case 1: no child
  180. if(root->left==NULL && root->right==NULL){
  181. delete root;
  182. root=NULL;
  183. return root;
  184. }
  185. // case 2: only 1 child
  186. else if(root->left==NULL){
  187. BstNode *temp=root;
  188. root=root->right;
  189. delete temp;
  190. }
  191. else if(root->right==NULL){
  192. BstNode *temp=root;
  193. root=root->left;
  194. delete temp;
  195. }
  196. // case 3: 2 children
  197. else{
  198. int min=findMin(root->right);
  199. root->data=min;
  200. root->right=Delete(root->right,min);
  201. }
  202. }
  203. root->height=max(height(root->left),height(root->right))+1;
  204. // AVL Implementation
  205. // check balance or not
  206. int balance=getBalance(root);
  207. // if unbalanced
  208. // Left case
  209. if(balance>1 && value < root->left->data){
  210. return L_rotate(root);
  211. }
  212. // Right case
  213. if(balance<-1 && value > root->right->data){
  214. return R_rotate(root);
  215. }
  216. // Right Left case
  217. if(balance>1 && value > root->left->data){
  218. root->left = R_rotate(root->left);
  219. return L_rotate(root);
  220. }
  221. // Left Right case
  222. if(balance<-1 && value < root->right->data){
  223. root->right = L_rotate(root->right);
  224. return R_rotate(root);
  225. }
  226. return root;
  227. }
  228.  
  229. // void storeBst(BstNode *root,vector<BstNode *> &v){
  230. // if(root==NULL){
  231. // return;
  232. // }
  233. // storeBst(root->left,v);
  234. // v.push_back(root);
  235. // storeBst(root->right,v);
  236. // }
  237.  
  238. // BstNode * buildtree(vector<BstNode *>v,int start,int end){
  239. // if(start>end){
  240. // return NULL;
  241. // }
  242. // int mid=(start+end)/2;
  243. // BstNode *root=v[mid];
  244. // root->left=buildtree(v,start,mid-1);
  245. // root->right=buildtree(v,mid+1,end);
  246.  
  247. // return root;
  248. // }
  249.  
  250. // BstNode * balance(BstNode *root){
  251. // vector<BstNode *>v;
  252. // storeBst(root,v);
  253.  
  254. // int n=v.size();
  255. // return buildtree(v,0,n-1);
  256. // }
  257.  
  258. // Before AVL Tree Implementation
  259. // 15
  260. // 10 17
  261. // 1 14 16 18
  262. // 2 12
  263. // 11
  264.  
  265. int main(){
  266. BstNode *root=NULL;
  267. root=Insert(root,15);
  268. root=Insert(root,10);
  269. root=Insert(root,14);
  270. root=Insert(root,11);
  271. root=Insert(root,12);
  272. root=Insert(root,1);
  273. root=Insert(root,2);
  274. root=Insert(root,17);
  275. root=Insert(root,16);
  276. root=Insert(root,18);
  277.  
  278. if(Search(root,-5)){
  279. cout<<"Found\n";
  280. }
  281. else{
  282. cout<<"Not Found\n";
  283. }
  284. cout<<"Min "<<findMin(root);
  285. cout<<"\nHeight "<<height(root);
  286. cout<<"\nLevelOrder: ";
  287. LevelOrder(root);
  288. cout<<"\nInorder: ";
  289. Inorder(root);
  290. cout<<"\nCheck Bst or not? \n";
  291. if(isBst(root)){
  292. cout<<"\tYes,it is bst\n";
  293. }
  294. else{
  295. cout<<"\tNo,it is not a bst";
  296. }
  297. cout<<"DELETE 10,11,2,18: ";
  298. root=Delete(root,11);
  299. root=Delete(root,10);
  300. root=Delete(root,2);
  301. root=Delete(root,18);
  302. LevelOrder(root);
  303. cout<<"\nHeight "<<height(root);
  304. cout<<"\nCheck Bst or not? \n";
  305. if(isBst(root)){
  306. cout<<"\tYes,it is bst\n";
  307. }
  308. else{
  309. cout<<"\tNo,it is not a bst";
  310. }
  311. return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement