Advertisement
Guest User

Untitled

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