Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. int flag = 0;
  5. struct Node {
  6. char key[100];
  7. int balance,number;
  8. struct Node * parent;
  9. struct Node * left;
  10. struct Node * right;
  11. };
  12. struct Node * Replace(struct Node * root,struct Node * x,struct Node * y) {
  13. if (x == root) {
  14. root = y;
  15. y->parent = NULL;
  16. }
  17. else {
  18. struct Node * parent = x->parent;
  19. if (y != NULL) y->parent = parent;
  20. if (parent->left == x) parent->left = y;
  21. else parent->right = y;
  22. }
  23. return root;
  24. }
  25. struct Node * Init(char * key,int balance,int number) {
  26. struct Node * a = (struct Node*)malloc(sizeof(struct Node));
  27. strcpy(a->key,key);
  28. a->number = number;
  29. a->balance = balance;
  30. a->parent = NULL;
  31. a->left = NULL;
  32. a->right = NULL;
  33. return a;
  34. }
  35. struct Node * RotateLeft(struct Node * root,struct Node * x) {
  36. struct Node * y = x->right;
  37. root = Replace(root,x,y);
  38. struct Node * b = y->left;
  39. if (b) b->parent = x;
  40. x->right = b;
  41. x->parent = y;
  42. y->left = x;
  43. --x->balance;
  44. if (y->balance > 0) x->balance -= y->balance;
  45. --y->balance;
  46. if (x->balance < 0) y->balance += x->balance;
  47. return root;
  48. }
  49. struct Node * RotateRight(struct Node * root,struct Node * x) {
  50. struct Node * y = x->left;
  51. root = Replace(root,x,y);
  52. struct Node * b = y->right;
  53. if (b) b->parent = x;
  54. x->left = b;
  55. x->parent = y;
  56. y->right = x;
  57. ++x->balance;
  58. if (y->balance < 0) x->balance -= y->balance;
  59. ++y->balance;
  60. if (x->balance > 0) y->balance += x->balance;
  61. return root;
  62. }
  63. struct Node * Insert(int number,char * key,struct Node * root) {
  64. struct Node * node = Init(key,0,number);
  65. if (root == NULL) {
  66. root = node;
  67. return node;
  68. }
  69. struct Node * lst = root;
  70. for(;;) {
  71. if (strcmp(key,lst->key) == 0) {
  72. free(node);
  73. flag = 1;
  74. return root;
  75. };
  76. if (strcmp(key,lst->key) < 0) {
  77. if (lst->left == NULL) {
  78. lst->left = node;
  79. node->parent = lst;
  80. break;
  81. }
  82. else lst = lst->left;
  83. }
  84. else {
  85. if (lst->right == NULL) {
  86. lst->right = node;
  87. node->parent = lst;
  88. break;
  89. }
  90. else lst = lst->right;
  91. }
  92. }
  93. return node;
  94. }
  95. struct Node * InsertAVL(int number,char * key,struct Node * root) {
  96. struct Node * a = Insert(number,key,root);
  97. if (flag == 1) return root;
  98. for(;;) {
  99. struct Node * x = a->parent;
  100. if (!x) return a;
  101. if (a == x->left) {
  102. --x->balance;
  103. if (x->balance == 0) break;
  104. if (x->balance == -2) {
  105. if (a->balance == 1) root = RotateLeft(root,a);
  106. root = RotateRight(root,x);
  107. break;
  108. }
  109. }
  110. else {
  111. ++x->balance;
  112. if (x->balance == 0) break;
  113. if (x->balance == 2) {
  114. if (a->balance == -1) root = RotateRight(root,a);
  115. root = RotateLeft(root,x);
  116. break;
  117. }
  118. }
  119. a = x;
  120. }
  121. return root;
  122. }
  123. struct Node * Search(char * key,struct Node * root) {
  124. struct Node * lst = root;
  125. while((lst != NULL) && (strcmp(lst->key,key) != 0)) {
  126. if (strcmp(key,lst->key) < 0) lst = lst->left;
  127. else lst = lst->right;
  128. }
  129. return lst;
  130. }
  131. int SPEC(char symbol) {
  132. switch(symbol) {
  133. case '+':
  134. return 1;
  135. case '-':
  136. return 2;
  137. case '*':
  138. return 3;
  139. case '/':
  140. return 4;
  141. case '(':
  142. return 5;
  143. case ')':
  144. return 6;
  145. default : return 0;
  146. }
  147. }
  148. void freeall(struct Node * root) {
  149. if ((root->right == NULL) && (root->left == NULL)) free(root);
  150. else {
  151. if (root->right) freeall(root->right);
  152. if (root->left) freeall(root->left);
  153. free(root);
  154. }
  155. }
  156. int main()
  157. {
  158. struct Node * root = NULL;
  159. char str[13] ;
  160. int n,symbol,ch = 0,j = 0;
  161. scanf("%d" , &n);
  162. symbol = getchar();
  163. symbol = getchar();
  164. for(int i = 0;i < n;) {
  165. if (symbol == ' ') {
  166. symbol = getchar();
  167. ++i;
  168. }
  169. else {
  170. if (SPEC(symbol) != 0) {
  171. printf("SPEC %d\n" , SPEC(symbol) - 1);
  172. symbol = getchar();
  173. ++i;
  174. }
  175. else {
  176. if ((symbol >= 48) && (symbol <= 57)) {
  177. printf("CONST ");
  178. while((symbol >= 48) && (symbol <= 57) && (i < n)) {
  179. printf("%c" , symbol);
  180. symbol = getchar();
  181. ++i;
  182. }
  183. printf("\n");
  184. }
  185. else {
  186. j = 0;
  187.  
  188. while((SPEC(symbol) == 0) && (symbol != ' ') && (i < n)) {
  189.  
  190. str[j] = symbol;
  191. symbol = getchar();
  192. ++j;
  193. ++i;
  194. }
  195.  
  196. str[j] = '\0';
  197. root = InsertAVL(ch,str,root);
  198. if (flag == 1) {
  199. printf("IDENT %d\n" , Search(str,root)->number);
  200. flag = 0;
  201. }
  202. else {
  203. printf("IDENT %d\n" , Search(str,root)->number);
  204. flag = 0;
  205. ++ch;
  206. }
  207.  
  208.  
  209. }
  210. }
  211. }
  212. }
  213. freeall(root);
  214. return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement