Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. // Refernce : Eternally Confuzzled Website
  2.  
  3.  
  4. #include <bits/stdc++.h>
  5. #define NUMBER_OF_NODES 100000
  6. #define LL long long
  7. #define height(p) ((p) == NULL ? -1 : (p)->balance)
  8. #define jsw_max(a,b) ((a) > (b) ? (a) : (b))
  9. using namespace std;
  10.  
  11.  
  12.  
  13. struct AVL_node
  14. {
  15. LL data;
  16. LL balance;
  17. struct AVL_node *link[2];
  18. };
  19.  
  20.  
  21.  
  22. LL height1(struct AVL_node * root)
  23. {
  24. if(root==NULL) return 0;
  25. else
  26. return 1+max(height1(root->link[0]),height1(root->link[1]));
  27. }
  28.  
  29.  
  30. struct AVL_tree
  31. {
  32. struct AVL_node *root;
  33. };
  34.  
  35. int jsw_find(struct AVL_tree *tree, LL item)
  36. {
  37. struct AVL_node *it = tree->root;
  38.  
  39. while (it != NULL)
  40. {
  41. if (it->data == item)
  42. {
  43. return 1;
  44. }
  45. else
  46. {
  47. int dir = it->data < item;
  48.  
  49. it = it->link[dir];
  50. }
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57. struct AVL_node *make_node(LL data)
  58. {
  59. //cout<<" I reached here 3a"<<endl;
  60. struct AVL_node *rn = (struct AVL_node *) malloc(sizeof *rn);
  61. // cout<<" I reached here 4"<<endl;
  62. if (rn != NULL)
  63. {
  64. rn->data = data;
  65. rn->link[0] = rn->link[1] = NULL;
  66. }
  67.  
  68. return rn;
  69. }
  70.  
  71. struct AVL_node *jsw_single(struct AVL_node *root, LL dir)
  72. {
  73. struct AVL_node *save = root->link[!dir];
  74.  
  75. root->link[!dir] = save->link[dir];
  76. save->link[dir] = root;
  77.  
  78. return save;
  79. }
  80.  
  81. struct AVL_node *jsw_double(struct AVL_node *root, LL dir)
  82. {
  83. struct AVL_node *save = root->link[!dir]->link[dir];
  84.  
  85. root->link[!dir]->link[dir] = save->link[!dir];
  86. save->link[!dir] = root->link[!dir];
  87. root->link[!dir] = save;
  88.  
  89. save = root->link[!dir];
  90. root->link[!dir] = save->link[dir];
  91. save->link[dir] = root;
  92.  
  93. return save;
  94. }
  95.  
  96. void jsw_adjust_balance(struct AVL_node *root, LL dir, LL bal)
  97. {
  98. struct AVL_node *n = root->link[dir];
  99. struct AVL_node *nn = n->link[!dir];
  100.  
  101. if (nn->balance == 0)
  102. {
  103. root->balance = n->balance = 0;
  104. }
  105. else if (nn->balance == bal)
  106. {
  107. root->balance = -bal;
  108. n->balance = 0;
  109. }
  110. else /* nn->balance == -bal */
  111. {
  112. root->balance = 0;
  113. n->balance = bal;
  114. }
  115.  
  116. nn->balance = 0;
  117. }
  118.  
  119. struct AVL_node *AVL_node_insert_balance(struct AVL_node *root, LL dir)
  120. {
  121. struct AVL_node *n = root->link[dir];
  122. LL bal = dir == 0 ? -1 : +1;
  123.  
  124. if (n->balance == bal)
  125. {
  126. root->balance = n->balance = 0;
  127. root = jsw_single(root, !dir);
  128. }
  129. else /* n->balance == -bal */
  130. {
  131. jsw_adjust_balance(root, dir, bal);
  132. root = jsw_double(root, !dir);
  133. }
  134.  
  135. return root;
  136. }
  137.  
  138. //struct AVL_node *AVL_node_insert_r(struct AVL_node *root, LL item, LL *done) {
  139. //
  140. // cout<<" I reached here 2"<<endl;
  141. // if (root == NULL) {
  142. // root = make_node(item);
  143. // cout<<" I reached here 3"<<endl;
  144. // } else {
  145. // cout<<" I reached here 2a"<<endl;
  146. //
  147. // int dir = ((root->data) < item);
  148. //
  149. // // cout<<" (root->data) :"<<(root->data)<<" data : "<<data<<endl;
  150. //
  151. // cout<<" I reached here 2Aaa"<<endl;
  152. // root->link[dir] = AVL_node_insert_r(root->link[dir], item, done);
  153. // cout<<" I reached here 2b"<<endl;
  154. //
  155. // if (!*done) {
  156. // /* Update balance factors */
  157. // root->balance += dir == 0 ? -1 : +1;
  158. //
  159. // /* Rebalance as necessary and terminate */
  160. // if (root->balance == 0) {
  161. // *done = 1;
  162. // } else if (abs(root->balance) > 1) {
  163. // cout<<" I reached here 2c"<<endl;
  164. // root = AVL_node_insert_balance(root, dir);
  165. // *done = 1;
  166. // cout<<" I reached here 2d"<<endl;
  167. // }
  168. // }
  169. // }
  170. //
  171. // return root;
  172. //}
  173. struct AVL_node *AVL_node_insert_r(struct AVL_node *root, LL item, LL *done)
  174. {
  175. if (root == NULL)
  176. {
  177. root = make_node(item);
  178. }
  179. else
  180. {
  181. // cout<<" (root->data)"<< (root->data)<< " item "<<item<<endl;
  182. int dir = (root->data) < item;
  183. int lh, rh, max;
  184.  
  185. root->link[dir] = AVL_node_insert_r(root->link[dir], item, done);
  186.  
  187. if (!*done)
  188. {
  189. /* Rebalance if necessary */
  190. lh = height(root->link[dir]);
  191. rh = height(root->link[!dir]);
  192.  
  193. if (lh - rh >= 2)
  194. {
  195. struct AVL_node *a = root->link[dir]->link[dir];
  196. struct AVL_node *b = root->link[dir]->link[!dir];
  197.  
  198. if (height(a) >= height(b))
  199. {
  200. root = jsw_single(root, !dir);
  201. }
  202. else
  203. {
  204. root = jsw_double(root, !dir);
  205. }
  206.  
  207. *done = 1;
  208. }
  209.  
  210. /* Update balance factors */
  211. lh = height(root->link[dir]);
  212. rh = height(root->link[!dir]);
  213. max = jsw_max(lh, rh);
  214.  
  215. root->balance = max + 1;
  216. }
  217. }
  218.  
  219. return root;
  220. }
  221.  
  222. LL AVL_node_insert(struct AVL_tree *tree, LL item)
  223. {
  224. LL done = 0;
  225. //cout<<" I reached here 1";
  226.  
  227. tree->root = AVL_node_insert_r(tree->root, item, &done);
  228.  
  229. return 1;
  230. }
  231.  
  232. ////////////////////////////DELETION/////////////////////////////////
  233.  
  234. struct AVL_node *AVL_node_remove_balance(struct AVL_node *root, LL dir, LL *done)
  235. {
  236. struct AVL_node *n = root->link[!dir];
  237. LL bal = dir == 0 ? -1 : +1;
  238.  
  239. if (n->balance == -bal)
  240. {
  241. root->balance = n->balance = 0;
  242. root = jsw_single(root, dir);
  243. }
  244. else if (n->balance == bal)
  245. {
  246. jsw_adjust_balance(root, !dir, -bal);
  247. root = jsw_double(root, dir);
  248. }
  249. else /* n->balance == 0 */
  250. {
  251. root->balance = -bal;
  252. n->balance = bal;
  253. root = jsw_single(root, dir);
  254. *done = 1;
  255. }
  256.  
  257. return root;
  258. }
  259.  
  260. struct AVL_node *AVL_node_remove_r(struct AVL_node *root, LL data, LL *done)
  261. {
  262. if (root != NULL)
  263. {
  264. LL dir;
  265.  
  266. /* Remove node */
  267. if (root->data == data)
  268. {
  269. /* Unlink and fix parent */
  270. if (root->link[0] == NULL || root->link[1] == NULL)
  271. {
  272. struct AVL_node *save;
  273.  
  274. dir = root->link[0] == NULL;
  275. save = root->link[dir];
  276. free(root);
  277.  
  278. return save;
  279. }
  280. else
  281. {
  282. /* Find inorder predecessor */
  283. struct AVL_node *heir = root->link[0];
  284.  
  285. while (heir->link[1] != NULL)
  286. {
  287. heir = heir->link[1];
  288. }
  289.  
  290. /* Copy and set new search data */
  291. root->data = heir->data;
  292. data = heir->data;
  293. }
  294. }
  295.  
  296. dir = root->data < data;
  297. root->link[dir] = AVL_node_remove_r(root->link[dir], data, done);
  298.  
  299. if (!*done)
  300. {
  301. /* Update balance factors */
  302. root->balance += dir != 0 ? -1 : +1;
  303.  
  304. /* Terminate or rebalance as necessary */
  305. if (abs(root->balance) == 1)
  306. {
  307. *done = 1;
  308. }
  309. else if (abs(root->balance) > 1)
  310. {
  311. root = AVL_node_remove_balance(root, dir, done);
  312. }
  313. }
  314. }
  315.  
  316. return root;
  317. }
  318.  
  319. LL AVL_node_remove(struct AVL_tree *tree, LL data)
  320. {
  321. LL done = 0;
  322.  
  323. tree->root = AVL_node_remove_r(tree->root, data, &done);
  324.  
  325. return 1;
  326. }
  327.  
  328.  
  329. /* Given a binary tree, print its nodes in preorder*/
  330. void printPreorder(struct AVL_node* node)
  331. {
  332. if (node == NULL)
  333. return;
  334.  
  335.  
  336. /* then recur on left sutree */
  337. printPreorder(node->link[0]);
  338. /* first print data of node */
  339. printf("%d ", node->data);
  340.  
  341. /* now recur on right subtree */
  342. printPreorder(node->link[1]);
  343. }
  344. int main()
  345. {
  346. freopen("in.txt", "r", stdin);
  347. freopen("out.txt","w",stdout);
  348. struct AVL_tree r1;
  349. r1.root = NULL;
  350. LL i, choice, value;
  351.  
  352. cin>>choice>>value;
  353. while(1)
  354. {
  355.  
  356. if (choice == 1)
  357. {
  358. LL i = AVL_node_insert(&r1, value);
  359. if (i != 1) cout << " Couldn't insert the node" << value << endl;
  360. else cout << " Node inserted -> " << value << endl;
  361. }
  362. if (choice == 0)
  363. {
  364. LL i = jsw_find(&r1, value);
  365. if (i != 1) cout << value <<" Not Found " << endl;
  366. else cout << " Node exists -> " << value << endl;
  367.  
  368. }
  369. if (choice == 2)
  370. {
  371. LL j = jsw_find(&r1, value);
  372. if (j != 1) cout << "Not Found --> " << value << endl;
  373. else
  374. {
  375. LL i = AVL_node_remove(&r1, value);
  376. if (i != 1) cout << " Couldn't delete the node" << value << endl;
  377. else cout << " Node deleted -> " << value << endl;
  378. }
  379. }
  380. cin>>choice>>value;
  381. if(choice == -1) break;
  382. }
  383. cout<<" Node --> "<<(*(r1.root)).data;
  384. cout<<" height --> "<<height1(&(*(r1.root)));
  385. cout<<" Preorder traversal :"<<endl;
  386. printPreorder(&(*(r1.root)));
  387.  
  388. return 0;
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement