Advertisement
Guest User

Untitled

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