Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. typedef struct node {
  8. char* key;
  9. struct node *left;
  10. struct node *right;
  11. int height;
  12. }node;
  13.  
  14. void preOrder(node *root)
  15. {
  16. if(root == NULL)
  17. return;
  18.  
  19. printf("%sn", root->key);
  20. preOrder(root->left);
  21. preOrder(root->right);
  22.  
  23. }
  24.  
  25. int max(int a, int b);
  26.  
  27. // A utility function to get height of the tree
  28. int height(node* N)
  29. {
  30. if(N==NULL)
  31. return 0;
  32.  
  33. return N->height;
  34. }
  35.  
  36. // A utility function to get maximum of two integers
  37. int max(int a, int b)
  38. {
  39. if(a > b)
  40. {
  41. return a;
  42. }
  43. else
  44. return b;
  45. }
  46.  
  47. int avlHeight(node* N) {
  48. if(N == NULL)
  49. return -1;
  50. else
  51. return max(height(N->left), height(N->right)) + 1;
  52. }
  53.  
  54. int nodeCount(node *root)
  55. {
  56. int count = 0;
  57. if (root != NULL) {
  58. count = 1 + nodeCount(root->left) + nodeCount(root->right);
  59. }
  60. return count;
  61. }
  62.  
  63. /* Helper function that allocates a new node with the given key and
  64. NULL left and right pointers. */
  65. node* newNode(node* node, char* key)
  66. {
  67. struct node* node2 = (struct node*)malloc(sizeof(struct node));
  68. node2->key = key;
  69. node2->left = NULL;
  70. node2->right = NULL;
  71. node2->height = 1; // new node is initially added at leaf
  72. return(node2);
  73. }
  74.  
  75. // A utility function to right rotate subtree rooted with y
  76. // See the diagram given above.
  77. node *rightRotate(node *y)
  78. {
  79. node *x = y->left;
  80. node *T2 = x->right;
  81.  
  82. // Perform rotation
  83. x->right = y;
  84. y->left = T2;
  85.  
  86. // Update heights
  87. y->height = max(height(y->left), height(y->right))+1;
  88. x->height = max(height(x->left), height(x->right))+1;
  89.  
  90. // Return new root
  91. return x;
  92. }
  93.  
  94. // A utility function to left rotate subtree rooted with x
  95. // See the diagram given above.
  96. node *leftRotate(node *x)
  97. {
  98. printf("3rdn");
  99. node *y = x->right;
  100. node *T2 = y->left;
  101. // Perform rotation
  102.  
  103. y->left = x;
  104. x->right = T2;
  105.  
  106. // Update heights
  107. x->height = max(height(x->left), height(x->right))+1;
  108. y->height = max(height(y->left), height(y->right))+1;
  109.  
  110. // Return new root
  111. return y;
  112. }
  113.  
  114. // Get Balance factor of node N
  115. int getBalance(node *N)
  116. {
  117. if (N == NULL)
  118. return 0;
  119. return height(N->left) - height(N->right);
  120. }
  121.  
  122. node* insert(node* node, char* key)
  123. {
  124. /* 1. Perform the normal BST rotation */
  125.  
  126. if (node == NULL)
  127. return(newNode(node, key));
  128.  
  129. if (strcmp(key, node->key) < 0) {
  130. node->left = insert(node->left, key);
  131. }
  132. else {
  133. node->right = insert(node->right, key);
  134. }
  135.  
  136. /* 2. Update height of this ancestor node */
  137. //node->height = max(height(node->left), height(node->right)) + 1;
  138. //printf("%d ",node->height);
  139. /* 3. Get the balance factor of this ancestor node to check whether
  140. this node became unbalanced */
  141. //int balance = getBalance(node);
  142.  
  143. // If this node becomes unbalanced, then there are 4 cases
  144.  
  145. // Left Left Case
  146.  
  147.  
  148. /*if (balance > 1 && strcmp(key, (node->left)->key)<0){
  149. printf("1stn");
  150. return rightRotate(node);
  151. }
  152.  
  153. // Right Right Case
  154. if (balance < -1 && strcmp(key, (node->right)->key)>0){
  155. printf("2ndn");
  156. return leftRotate(node);
  157. }
  158. // Left Right Case
  159. //i = strcmp(key, (node->left)->key);
  160. //printf("!%d!",i);
  161. if (balance > 1 && strcmp(key, (node->left)->key)>0){
  162. printf("3rdn");
  163. node->left = leftRotate(node->left);
  164. return rightRotate(node);
  165. }
  166. // Right Left Case
  167. if (balance < -1 && strcmp(key, node->right->key)<0)
  168. {
  169. printf("4thn");
  170. node->right = rightRotate(node->right);
  171. return leftRotate(node);
  172. }
  173. /* return the (unchanged) node pointer */
  174. //printf("%s ",key);
  175. //preOrder(node);
  176. return node;
  177. }
  178.  
  179. // A utility function to print preorder traversal of the tree.
  180. // The function also prints height of every node
  181.  
  182. void nick() {//initial banner addition
  183. printf("**********************************************************n");
  184. printf("Welcome to the Arethmatic Expression calculator!n");
  185. printf("Created by: Nicholas Bastiann");
  186. printf("Student ID #: 0843261n");
  187. printf("**********************************************************nn");
  188. }
  189.  
  190. void menu()//menu function
  191. {
  192. printf("nWhat would you like to do? (Please input a single number)n");
  193. printf("1. Initializationn");
  194. printf("2. Findn");
  195. printf("3. Insertn");
  196. printf("4. Removen");
  197. printf("5. Check Height and Sizen");
  198. printf("6. Find All (above a given frequency)n");
  199. printf("7. Exitn");
  200. printf("avl/> ");
  201. }
  202.  
  203. int main()
  204. {
  205. char value[100];
  206. char str[100];
  207. int counter = 1, choice, height, size;
  208. char*token, *key;//, *flr239='endnue783', *ene039='irhwr32';
  209. node* root= NULL;
  210. static const char input[] = "avl.dat";//"A4_data_file.dat";
  211. FILE* textFile = fopen(input,"r");
  212.  
  213. while(fgets(str, 100, textFile)) {
  214.  
  215. token = strtok(str," n");
  216.  
  217. while (token != NULL)
  218. {
  219. key = strdup(token);
  220. root = insert(root, key);
  221. free(key);
  222. token = strtok (NULL, " n");
  223. }
  224. if(ferror(textFile))
  225. {
  226. printf("you done fucked up a-a-ron");
  227. return(0);
  228. }
  229. }
  230.  
  231. nick();
  232. do{
  233.  
  234. menu();
  235. fgets(value,100,stdin);
  236. choice = atoi(value);//user proofed menu
  237.  
  238. switch(choice)
  239. {
  240. case 1:
  241. printf("filename: ");
  242. printf("Pre order traversal of the constructed AVL tree is n");
  243. preOrder(root);
  244. break;
  245.  
  246. case 2:
  247. /*printf("findkey: %s");
  248. printf(", frequency: %d");*/
  249. break;
  250.  
  251. case 3:
  252. printf("insert key: ");
  253. break;
  254.  
  255. case 4:
  256. printf("remove key: ");
  257. break;
  258.  
  259. case 5:
  260. height = avlHeight(root);
  261. size = nodeCount(root);
  262. printf("height: %d", height);
  263. printf(", size: %d", size);
  264. break;
  265.  
  266. case 6:
  267. printf("frequency: ");
  268. break;
  269.  
  270. case 7:
  271. printf("Exiting program...n");
  272. exit(0);
  273. break;
  274.  
  275. default:
  276. printf("nPlease put input a number from 1 to 7 (only a single integer).nn");
  277. break;
  278. }
  279.  
  280. }while(counter==1);
  281.  
  282. return(0);
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement