Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int flag = 0;
- struct Node {
- char key[100];
- int balance,number;
- struct Node * parent;
- struct Node * left;
- struct Node * right;
- };
- struct Node * Replace(struct Node * root,struct Node * x,struct Node * y) {
- if (x == root) {
- root = y;
- y->parent = NULL;
- }
- else {
- struct Node * parent = x->parent;
- if (y != NULL) y->parent = parent;
- if (parent->left == x) parent->left = y;
- else parent->right = y;
- }
- return root;
- }
- struct Node * Init(char * key,int balance,int number) {
- struct Node * a = (struct Node*)malloc(sizeof(struct Node));
- strcpy(a->key,key);
- a->number = number;
- a->balance = balance;
- a->parent = NULL;
- a->left = NULL;
- a->right = NULL;
- return a;
- }
- struct Node * RotateLeft(struct Node * root,struct Node * x) {
- struct Node * y = x->right;
- root = Replace(root,x,y);
- struct Node * b = y->left;
- if (b) b->parent = x;
- x->right = b;
- x->parent = y;
- y->left = x;
- --x->balance;
- if (y->balance > 0) x->balance -= y->balance;
- --y->balance;
- if (x->balance < 0) y->balance += x->balance;
- return root;
- }
- struct Node * RotateRight(struct Node * root,struct Node * x) {
- struct Node * y = x->left;
- root = Replace(root,x,y);
- struct Node * b = y->right;
- if (b) b->parent = x;
- x->left = b;
- x->parent = y;
- y->right = x;
- ++x->balance;
- if (y->balance < 0) x->balance -= y->balance;
- ++y->balance;
- if (x->balance > 0) y->balance += x->balance;
- return root;
- }
- struct Node * Insert(int number,char * key,struct Node * root) {
- struct Node * node = Init(key,0,number);
- if (root == NULL) {
- root = node;
- return node;
- }
- struct Node * lst = root;
- for(;;) {
- if (strcmp(key,lst->key) == 0) {
- free(node);
- flag = 1;
- return root;
- };
- if (strcmp(key,lst->key) < 0) {
- if (lst->left == NULL) {
- lst->left = node;
- node->parent = lst;
- break;
- }
- else lst = lst->left;
- }
- else {
- if (lst->right == NULL) {
- lst->right = node;
- node->parent = lst;
- break;
- }
- else lst = lst->right;
- }
- }
- return node;
- }
- struct Node * InsertAVL(int number,char * key,struct Node * root) {
- struct Node * a = Insert(number,key,root);
- if (flag == 1) return root;
- for(;;) {
- struct Node * x = a->parent;
- if (!x) return a;
- if (a == x->left) {
- --x->balance;
- if (x->balance == 0) break;
- if (x->balance == -2) {
- if (a->balance == 1) root = RotateLeft(root,a);
- root = RotateRight(root,x);
- break;
- }
- }
- else {
- ++x->balance;
- if (x->balance == 0) break;
- if (x->balance == 2) {
- if (a->balance == -1) root = RotateRight(root,a);
- root = RotateLeft(root,x);
- break;
- }
- }
- a = x;
- }
- return root;
- }
- struct Node * Search(char * key,struct Node * root) {
- struct Node * lst = root;
- while((lst != NULL) && (strcmp(lst->key,key) != 0)) {
- if (strcmp(key,lst->key) < 0) lst = lst->left;
- else lst = lst->right;
- }
- return lst;
- }
- int SPEC(char symbol) {
- switch(symbol) {
- case '+':
- return 1;
- case '-':
- return 2;
- case '*':
- return 3;
- case '/':
- return 4;
- case '(':
- return 5;
- case ')':
- return 6;
- default : return 0;
- }
- }
- void freeall(struct Node * root) {
- if ((root->right == NULL) && (root->left == NULL)) free(root);
- else {
- if (root->right) freeall(root->right);
- if (root->left) freeall(root->left);
- free(root);
- }
- }
- int main()
- {
- struct Node * root = NULL;
- char str[13] ;
- int n,symbol,ch = 0,j = 0;
- scanf("%d" , &n);
- symbol = getchar();
- symbol = getchar();
- for(int i = 0;i < n;) {
- if (symbol == ' ') {
- symbol = getchar();
- ++i;
- }
- else {
- if (SPEC(symbol) != 0) {
- printf("SPEC %d\n" , SPEC(symbol) - 1);
- symbol = getchar();
- ++i;
- }
- else {
- if ((symbol >= 48) && (symbol <= 57)) {
- printf("CONST ");
- while((symbol >= 48) && (symbol <= 57) && (i < n)) {
- printf("%c" , symbol);
- symbol = getchar();
- ++i;
- }
- printf("\n");
- }
- else {
- j = 0;
- while((SPEC(symbol) == 0) && (symbol != ' ') && (i < n)) {
- str[j] = symbol;
- symbol = getchar();
- ++j;
- ++i;
- }
- str[j] = '\0';
- root = InsertAVL(ch,str,root);
- if (flag == 1) {
- printf("IDENT %d\n" , Search(str,root)->number);
- flag = 0;
- }
- else {
- printf("IDENT %d\n" , Search(str,root)->number);
- flag = 0;
- ++ch;
- }
- }
- }
- }
- }
- freeall(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement