Advertisement
Guest User

AVL completo

a guest
Oct 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. // C program to insert a node in AVL tree
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. // An AVL tree node
  6. struct Node
  7. {
  8. int key;
  9. struct Node *left;
  10. struct Node *right;
  11. int height;
  12. };
  13.  
  14. // A utility function to get maximum of two integers
  15. int max(int a, int b);
  16.  
  17. // A utility function to get the height of the tree
  18. int height(struct Node *N)
  19. {
  20. if (N == NULL)
  21. return 0;
  22. return N->height;
  23. }
  24.  
  25. // A utility function to get maximum of two integers
  26. int max(int a, int b)
  27. {
  28. return (a > b)? a : b;
  29. }
  30.  
  31. /* Helper function that allocates a new node with the given key and
  32. NULL left and right pointers. */
  33. struct Node* newNode(int key)
  34. {
  35. struct Node* node = (struct Node*)
  36. malloc(sizeof(struct Node));
  37. node->key = key;
  38. node->left = NULL;
  39. node->right = NULL;
  40. node->height = 1; // new node is initially added at leaf
  41. return(node);
  42. }
  43.  
  44. // A utility function to right rotate subtree rooted with y
  45. // See the diagram given above.
  46. struct Node *rightRotate(struct Node *y)
  47. {
  48. struct Node *x = y->left;
  49. struct Node *T2 = x->right;
  50.  
  51. // Perform rotation
  52. x->right = y;
  53. y->left = T2;
  54.  
  55. // Update heights
  56. y->height = max(height(y->left), height(y->right))+1;
  57. x->height = max(height(x->left), height(x->right))+1;
  58.  
  59. // Return new root
  60. return x;
  61. }
  62.  
  63. // A utility function to left rotate subtree rooted with x
  64. // See the diagram given above.
  65. struct Node *leftRotate(struct Node *x)
  66. {
  67. struct Node *y = x->right;
  68. struct Node *T2 = y->left;
  69.  
  70. // Perform rotation
  71. y->left = x;
  72. x->right = T2;
  73.  
  74. // Update heights
  75. x->height = max(height(x->left), height(x->right))+1;
  76. y->height = max(height(y->left), height(y->right))+1;
  77.  
  78. // Return new root
  79. return y;
  80. }
  81.  
  82. // Get Balance factor of node N
  83. int getBalance(struct Node *N)
  84. {
  85. if (N == NULL)
  86. return 0;
  87. return height(N->left) - height(N->right);
  88. }
  89.  
  90. // Recursive function to insert a key in the subtree rooted
  91. // with node and returns the new root of the subtree.
  92. struct Node* insert(struct Node* node, int key)
  93. {
  94. /* 1. Perform the normal BST insertion */
  95. if (node == NULL)
  96. return(newNode(key));
  97.  
  98. if (key < node->key)
  99. node->left = insert(node->left, key);
  100. else if (key > node->key)
  101. node->right = insert(node->right, key);
  102. else // Equal keys are not allowed in BST
  103. return node;
  104.  
  105. /* 2. Update height of this ancestor node */
  106. node->height = 1 + max(height(node->left),
  107. height(node->right));
  108.  
  109. /* 3. Get the balance factor of this ancestor
  110. node to check whether this node became
  111. unbalanced */
  112. int balance = getBalance(node);
  113.  
  114. // If this node becomes unbalanced, then
  115. // there are 4 cases
  116.  
  117. // Left Left Case
  118. if (balance > 1 && key < node->left->key)
  119. return rightRotate(node);
  120.  
  121. // Right Right Case
  122. if (balance < -1 && key > node->right->key)
  123. return leftRotate(node);
  124.  
  125. // Left Right Case
  126. if (balance > 1 && key > node->left->key)
  127. {
  128. node->left = leftRotate(node->left);
  129. return rightRotate(node);
  130. }
  131.  
  132. // Right Left Case
  133. if (balance < -1 && key < node->right->key)
  134. {
  135. node->right = rightRotate(node->right);
  136. return leftRotate(node);
  137. }
  138.  
  139. /* return the (unchanged) node pointer */
  140. return node;
  141. }
  142.  
  143. struct Node * minValueNode(struct Node* node)
  144. {
  145. struct Node* current = node;
  146.  
  147. /* loop down to find the leftmost leaf */
  148. while (current->left != NULL)
  149. current = current->left;
  150.  
  151. return current;
  152. }
  153.  
  154. struct Node* deleteNode(struct Node* root, int key)
  155. {
  156. // STEP 1: PERFORM STANDARD BST DELETE
  157.  
  158. if (root == NULL)
  159. return root;
  160.  
  161. // If the key to be deleted is smaller than the
  162. // root's key, then it lies in left subtree
  163. if ( key < root->key )
  164. root->left = deleteNode(root->left, key);
  165.  
  166. // If the key to be deleted is greater than the
  167. // root's key, then it lies in right subtree
  168. else if( key > root->key )
  169. root->right = deleteNode(root->right, key);
  170.  
  171. // if key is same as root's key, then This is
  172. // the node to be deleted
  173. else
  174. {
  175. // node with only one child or no child
  176. if( (root->left == NULL) || (root->right == NULL) )
  177. {
  178. struct Node *temp = root->left ? root->left :
  179. root->right;
  180.  
  181. // No child case
  182. if (temp == NULL)
  183. {
  184. temp = root;
  185. root = NULL;
  186. }
  187. else // One child case
  188. *root = *temp; // Copy the contents of
  189. // the non-empty child
  190. free(temp);
  191. }
  192. else
  193. {
  194. // node with two children: Get the inorder
  195. // successor (smallest in the right subtree)
  196. struct Node* temp = minValueNode(root->right);
  197.  
  198. // Copy the inorder successor's data to this node
  199. root->key = temp->key;
  200.  
  201. // Delete the inorder successor
  202. root->right = deleteNode(root->right, temp->key);
  203. }
  204. }
  205.  
  206. // If the tree had only one node then return
  207. if (root == NULL)
  208. return root;
  209.  
  210. // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
  211. root->height = 1 + max(height(root->left),
  212. height(root->right));
  213.  
  214. // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
  215. // check whether this node became unbalanced)
  216. int balance = getBalance(root);
  217.  
  218. // If this node becomes unbalanced, then there are 4 cases
  219.  
  220. // Left Left Case
  221. if (balance > 1 && getBalance(root->left) >= 0)
  222. return rightRotate(root);
  223.  
  224. // Left Right Case
  225. if (balance > 1 && getBalance(root->left) < 0)
  226. {
  227. root->left = leftRotate(root->left);
  228. return rightRotate(root);
  229. }
  230.  
  231. // Right Right Case
  232. if (balance < -1 && getBalance(root->right) <= 0)
  233. return leftRotate(root);
  234.  
  235. // Right Left Case
  236. if (balance < -1 && getBalance(root->right) > 0)
  237. {
  238. root->right = rightRotate(root->right);
  239. return leftRotate(root);
  240. }
  241.  
  242. return root;
  243. }
  244.  
  245. void preOrder(struct Node *root)
  246. {
  247. if(root != NULL)
  248. {
  249. printf("%d, ", root->key);
  250. preOrder(root->left);
  251. preOrder(root->right);
  252. }
  253. }
  254.  
  255. void postOrder(struct Node *root)
  256. {
  257. if(root!=NULL)
  258. {
  259. postOrder(root->left);
  260. postOrder(root->right);
  261. printf("%d, ",root->key);
  262. }
  263. }
  264.  
  265. void simetrico (struct Node *root)
  266. {
  267. if(root!=NULL)
  268. {
  269. simetrico(root->left);
  270. printf("%d, ",root->key);
  271. simetrico(root->right);
  272. }
  273. }
  274.  
  275. void por_nivel (struct Node * root)
  276. {
  277. int n,h;
  278. h=calcular_altura(root);
  279. for(n=0; n<h; n++)
  280. mostrar_nivel(root,n);
  281. }
  282.  
  283. void mostrar_nivel (struct Node * root, int n)
  284. {
  285. if(root==NULL)
  286. return;
  287. else if (n==0)
  288. {
  289. printf("%d, ",root->key);
  290. return;
  291. }
  292. else
  293. {
  294. mostrar_nivel(root->left,n-1);
  295. mostrar_nivel(root->right,n-1);
  296. }
  297. }
  298.  
  299. int calcular_altura (struct Node * root)
  300. {
  301. int der, izq;
  302. if(root==NULL)
  303. return 0;
  304. else
  305. {
  306. izq=calcular_altura(root->left);
  307. der=calcular_altura(root->right);
  308. if(izq>der)
  309. return izq+1;
  310. else
  311. return der+1;
  312. }
  313. }
  314.  
  315. /* Drier program to test above function*/
  316. int main()
  317. {
  318. struct Node *root = NULL;
  319.  
  320. /* Constructing tree given in the above figure */
  321. root = insert(root, 10);
  322. root = insert(root, 20);
  323. root = insert(root, 30);
  324. root = insert(root, 40);
  325. root = insert(root, 50);
  326. root = insert(root, 15);
  327. root = insert(root, 12);
  328. root = insert(root, 21);
  329. root = insert(root, 31);
  330. root = insert(root, 32);
  331.  
  332. printf("\nPreorder\n");
  333. preOrder(root);
  334. printf("\nPreorder sin el 20\n");
  335. root = deleteNode(root, 20);
  336. preOrder(root);
  337. /*
  338. printf("\nPostorder\n");
  339. postOrder(root);
  340. printf("\nSimetrico\n");
  341. preOrder(root);
  342. printf("\nPor nivel\n");
  343. preOrder(root);
  344. */
  345. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement