Advertisement
Singasking

Untitled

Nov 20th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. // C++ program for the above approach
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // AVL tree node
  6. struct Node {
  7. struct Node* left;
  8. struct Node* right;
  9. int data;
  10. struct Node* parent;
  11. int height;
  12. };
  13.  
  14.  
  15. void preorder(struct Node* root)
  16. {
  17.  
  18. cout<<root->data<<" ";
  19.  
  20. if (root->left != NULL) {
  21. preorder(root->left);
  22. }
  23.  
  24.  
  25. if (root->right != NULL) {
  26. preorder(root->right);
  27. }
  28. }
  29.  
  30. // Function to update the height of
  31. // a node according to its children's
  32. // node's heights
  33. void fixHeight(struct Node* root)
  34. {
  35. if (root != NULL) {
  36.  
  37. // Store the height of the
  38. // current node
  39. int tempHeight = 1;
  40.  
  41. // Store the height of the left
  42. // and right subtree
  43. if (root->left != NULL) {
  44. tempHeight = root->left->height + 1;
  45. }
  46.  
  47. if (root->right != NULL) {
  48. tempHeight = max(tempHeight, root->right->height + 1);
  49. }
  50.  
  51. // Update the height of the
  52. // current node
  53. root->height = tempHeight;
  54. }
  55. }
  56.  
  57. // Function to handle Left Left Case
  58. struct Node* LeftLeftRotate(
  59. struct Node* root)
  60. {
  61.  
  62. struct Node* temp = root->left;
  63.  
  64.  
  65. root->left = temp->right;
  66.  
  67. if (temp->right != NULL)
  68. temp->right->parent = root;
  69.  
  70.  
  71. temp->right = root;
  72.  
  73.  
  74. temp->parent = root->parent;
  75.  
  76. root->parent = temp;
  77.  
  78. if (temp->parent != NULL and root->data < temp->parent->data) {
  79. temp->parent->left = temp;
  80. }
  81. else {
  82. if (temp->parent != NULL)
  83. temp->parent->right = temp;
  84. }
  85.  
  86.  
  87. root = temp;
  88.  
  89.  
  90. fixHeight(root->left);
  91. fixHeight(root->right);
  92. fixHeight(root);
  93. fixHeight(root->parent);
  94.  
  95. return root;
  96. }
  97.  
  98. // Function to handle Right Right Case
  99. struct Node* RightRightRotate(
  100. struct Node* root)
  101. {
  102.  
  103. struct Node* temp = root->right;
  104.  
  105.  
  106. root->right = temp->left;
  107.  
  108. if (temp->left != NULL)
  109. temp->left->parent = root;
  110.  
  111.  
  112. temp->left = root;
  113.  
  114.  
  115. temp->parent = root->parent;
  116.  
  117.  
  118. root->parent = temp;
  119.  
  120.  
  121. if (temp->parent != NULL
  122. && root->data < temp->parent->data) {
  123. temp->parent->left = temp;
  124. }
  125. else {
  126. if (temp->parent != NULL)
  127. temp->parent->right = temp;
  128. }
  129.  
  130. // Make temp as the new root
  131. root = temp;
  132.  
  133. // Update the heights
  134. fixHeight(root->left);
  135. fixHeight(root->right);
  136. fixHeight(root);
  137. fixHeight(root->parent);
  138.  
  139. // Return the root node
  140. return root;
  141. }
  142.  
  143. // Function to handle Left Right Case
  144. struct Node* LeftRightRotate(
  145. struct Node* root)
  146. {
  147. root->left = RightRightRotate(root->left);
  148. return LeftLeftRotate(root);
  149. }
  150.  
  151. // Function to handle right left case
  152. struct Node* RightLeftRotate(
  153. struct Node* root)
  154. {
  155. root->right = LeftLeftRotate(root->right);
  156. return RightRightRotate(root);
  157. }
  158.  
  159. // Function to balance the tree after
  160. // deletion of a node
  161. struct Node* Balance( struct Node* root)
  162. {
  163.  
  164. int leftHeight = 0;
  165. int rightHeight = 0;
  166.  
  167. if (root->left != NULL)
  168. leftHeight = root->left->height;
  169.  
  170. if (root->right != NULL)
  171. rightHeight = root->right->height;
  172.  
  173. // If current node is not balanced
  174. if (abs(leftHeight - rightHeight) == 2) {
  175. if (leftHeight < rightHeight) {
  176.  
  177.  
  178. int rightheight1 = 0;
  179. int rightheight2 = 0;
  180. if (root->right->right != NULL)
  181. rightheight2 = root->right->right->height;
  182.  
  183. if (root->right->left != NULL)
  184. rightheight1 = root->right->left->height;
  185.  
  186. if (rightheight1 > rightheight2) {
  187.  
  188. // Right Left Case
  189. root = RightLeftRotate(root);
  190. }
  191. else {
  192.  
  193. // Right Right Case
  194. root = RightRightRotate(root);
  195. }
  196. }
  197. else {
  198.  
  199.  
  200. int leftGrandchildHeight = 0;
  201. int rightGrandchildHeight = 0;
  202. if (root->left->right != NULL)
  203. rightGrandchildHeight = root->left->right->height;
  204.  
  205. if (root->left->left != NULL)
  206. leftGrandchildHeight = root->left->left->height;
  207.  
  208. if (leftGrandchildHeight > rightGrandchildHeight) {
  209.  
  210. // Left Left Case
  211. root = LeftLeftRotate(root);
  212. }
  213. else {
  214.  
  215. // Left Right Case
  216. root = LeftRightRotate(root);
  217. }
  218. }
  219. }
  220.  
  221. // Return the root node
  222. return root;
  223. }
  224.  
  225.  
  226. struct Node* Insert(struct Node* root,struct Node* parentent,int data)
  227. {
  228.  
  229. if (root == NULL) {
  230.  
  231. // Create and assign tempHeightues
  232. // to a new node
  233. root = new struct Node;
  234. if (root == NULL)
  235. cout << "Error in memory" << endl;
  236. else {
  237. root->height = 1;
  238. root->left = NULL;
  239. root->right = NULL;
  240. root->parent = parentent;
  241. root->data = data;
  242. }
  243. }
  244.  
  245. else if (root->data > data) {
  246.  
  247. // Recur to the left subtree
  248. // to insert the node
  249. root->left = Insert(root->left,
  250. root, data);
  251.  
  252. // Store the heights of the
  253. // left and right subtree
  254. int leftHeight = 0;
  255. int rightHeight = 0;
  256.  
  257. if (root->left != NULL)
  258. leftHeight = root->left->height;
  259.  
  260. if (root->right != NULL)
  261. rightHeight = root->right->height;
  262.  
  263. // Balance the tree if the
  264. // current node is not balanced
  265. if (leftHeight-rightHeight==2 or rightHeight-leftHeight==2) {
  266.  
  267. if (root->left != NULL
  268. && data < root->left->data) {
  269.  
  270. // Left Left Case
  271. root = LeftLeftRotate(root);
  272. }
  273. else {
  274.  
  275. // Left Right Case
  276. root = LeftRightRotate(root);
  277. }
  278. }
  279. }
  280.  
  281. else if (root->data < data) {
  282.  
  283. // Recur to the right subtree
  284. // to insert the node
  285. root->right = Insert(root->right, root, data);
  286.  
  287. // Store the heights of the left
  288. // and right subtree
  289. int leftHeight = 0;
  290. int rightHeight = 0;
  291.  
  292. if (root->left != NULL)
  293. leftHeight = root->left->height;
  294.  
  295. if (root->right != NULL)
  296. rightHeight = root->right->height;
  297.  
  298. // Balance the tree if the
  299. // current node is not balanced
  300. if (abs(leftHeight - rightHeight) == 2) {
  301. if (root->right != NULL
  302. && data < root->right->data) {
  303.  
  304. // Right Left Case
  305. root = RightLeftRotate(root);
  306. }
  307. else {
  308.  
  309. // Right Right Case
  310. root = RightRightRotate(root);
  311. }
  312. }
  313. }
  314.  
  315. // Case when given data is
  316. // already in tree
  317. else {
  318. }
  319.  
  320. // Update the height of the
  321. // root node
  322. fixHeight(root);
  323.  
  324. // Return the root node
  325. return root;
  326. }
  327.  
  328.  
  329. struct Node* Delete( struct Node* root, int data)
  330. {
  331. if (root != NULL) {
  332.  
  333.  
  334. if (root->data == data) {
  335.  
  336.  
  337. if (root->right == NULL
  338. && root->left != NULL) {
  339. if (root->parent != NULL) {
  340. if (root->parent->data
  341. < root->data)
  342. root->parent->right = root->left;
  343. else
  344. root->parent->left = root->left;
  345.  
  346.  
  347. fixHeight(root->parent);
  348. }
  349.  
  350. root->left->parent = root->parent;
  351.  
  352.  
  353. root->left = Balance(
  354. root->left);
  355.  
  356. return root->left;
  357. }
  358.  
  359. // Replace root with its
  360. // right child
  361. else if (root->left == NULL && root->right != NULL) {
  362. if (root->parent != NULL) {
  363. if (root->parent->data < root->data)
  364. root->parent->right = root->right;
  365. else
  366. root->parent->left = root->right;
  367.  
  368. // Update the height
  369. // of the root's parentent
  370. fixHeight(root->parent);
  371. }
  372.  
  373. root->right->parent = root->parent;
  374.  
  375.  
  376. root->right = Balance(root->right);
  377. return root->right;
  378. }
  379.  
  380.  
  381. else if (root->left == NULL
  382. && root->right == NULL) {
  383. if (root->parent->data < root->data) {
  384. root->parent->right = NULL;
  385. }
  386. else {
  387. root->parent->left = NULL;
  388. }
  389.  
  390. if (root->parent != NULL)
  391. fixHeight(root->parent);
  392.  
  393. root = NULL;
  394. return NULL;
  395. }
  396.  
  397.  
  398. else {
  399. struct Node* temp = root;
  400. temp = temp->right;
  401. while (temp->left != NULL) {
  402. temp = temp->left;
  403. }
  404.  
  405. int tempHeight = temp->data;
  406.  
  407. root->right= Delete(root->right, temp->data);
  408.  
  409. root->data = tempHeight;
  410.  
  411.  
  412. root = Balance(root);
  413. }
  414. }
  415.  
  416.  
  417. else if (root->data < data) {
  418. root->right = Delete(root->right, data);
  419.  
  420. root = Balance(root);
  421. }
  422.  
  423.  
  424. else if (root->data > data) {
  425. root->left = Delete(root->left, data);
  426.  
  427. root = Balance(root);
  428. }
  429.  
  430.  
  431. if (root != NULL) {
  432. fixHeight(root);
  433. }
  434. }
  435.  
  436.  
  437. else {
  438. return NULL;
  439. }
  440.  
  441. // Return the root node
  442. return root;
  443. }
  444.  
  445. // Driver Code
  446. int main()
  447. {
  448. struct Node* root;
  449. root = NULL;
  450.  
  451. // Function call to insert the nodes
  452. root = Insert(root, NULL, 9);
  453. root = Insert(root, NULL, 5);
  454. root = Insert(root, NULL, 10);
  455. root = Insert(root, NULL, 0);
  456. root = Insert(root, NULL, 6);
  457.  
  458. // Print the tree before deleting node
  459. cout << "Before deletion:\n";
  460. preorder(root);
  461.  
  462. // Function Call to delete node 10
  463. root = Delete(root, 10);
  464.  
  465. // Print the tree after deleting node
  466. cout << "After deletion:\n";
  467. preorder(root);
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement