diff --git a/semester3/pset1/7-1-Balancing-Symbols.c b/semester3/pset1/7-1-Balancing-Symbols.c new file mode 100644 index 0000000..aa42892 --- /dev/null +++ b/semester3/pset1/7-1-Balancing-Symbols.c @@ -0,0 +1,93 @@ +#include +#include +#define SIZE 100 + +int parenToInt(char ch) { + if (ch == '(') { + return 0; + } else if (ch == ')') { + return 1; + } else if (ch == '[') { + return 2; + } else if (ch == ']') { + return 3; + } else if (ch == '{') { + return 4; + } else if (ch == '}') { + return 5; + } else { + return -1; + } +} + +typedef struct stack { + int top; + int capacity; + int *array; +} stack; + +stack *newStack(int capacity) { + stack *st = malloc(sizeof(stack)); + st->capacity = 50; + st->top = -1; + st->array = malloc(st->capacity * sizeof(int)); + + return st; +} + +void push(stack *st, int val) { + st->top++; + st->array[st->top] = val; +} + +int peek(stack *st) { return st->array[st->top]; } + +void pop(stack *st) { st->top--; } + +int isEmpty(stack *st) { return (st->top == -1); } + +int main(void) { + char input[SIZE]; + fgets(input, SIZE, stdin); + int num; + int ans[3] = {}; + + stack *st = newStack(SIZE); + + for (int i = 0; input[i] != '\0'; i++) { + num = parenToInt(input[i]); + if (num != -1) { + if (num % 2 == 0) { + // left paren , push to stack + push(st, num); + } else { + // right paren + if (!isEmpty(st) && peek(st) == num - 1) { + pop(st); + } else { + // a error + ans[num / 2] = 1; + } + } + } + } + + while (!isEmpty(st)) { + num = peek(st); + ans[num / 2] = 1; + pop(st); + } + + int incorrect = ans[0] | ans[1] | ans[2]; + if (incorrect) { + for (int i = 0; i < 3; i++) { + if (ans[i]) { + printf("%d,", i + 1); + } + } + } else { + printf("0"); + } + + return 0; +} diff --git a/semester3/pset0/7-1-Balancing-Symbols.cpp b/semester3/pset1/7-1-Balancing-Symbols.cpp similarity index 100% rename from semester3/pset0/7-1-Balancing-Symbols.cpp rename to semester3/pset1/7-1-Balancing-Symbols.cpp diff --git a/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c new file mode 100644 index 0000000..925e484 --- /dev/null +++ b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.c @@ -0,0 +1,26 @@ +#include +#define SIZE 100 + +typedef struct stack { + int top; + int capacity; + int* array; +} stack; + +void push(stack* st, int val) { + st->array[++st->top] = val; +} + +void pop(stack* st) { + st->top--; +} + +int peek(stack* st) { + return st->array[st->top]; +} + +int main(void) { + char input[SIZE]; + + fscanf(input, SIZE, stdin); +} diff --git a/semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.cpp similarity index 92% rename from semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp rename to semester3/pset1/7-2-Infix-to-Postfix-Conversion.cpp index 23a5dad..3de1f3d 100644 --- a/semester3/pset0/7-2-Infix-to-Postfix-Conversion.cpp +++ b/semester3/pset1/7-2-Infix-to-Postfix-Conversion.cpp @@ -12,7 +12,9 @@ int prec(char ch) { } else if (ch == '*' || ch == '/') { return 1; } else { - return -1; + // the precidence is the lowest, to make sure it stays in the stack. + // calculate stuff outside the paren last + return -1; } } diff --git a/semester3/pset0/7-3-central-list.c b/semester3/pset1/7-3-central-list.c similarity index 100% rename from semester3/pset0/7-3-central-list.c rename to semester3/pset1/7-3-central-list.c diff --git a/semester3/pset0/7-4-who-is-the-last.c b/semester3/pset1/7-4-who-is-the-last.c similarity index 100% rename from semester3/pset0/7-4-who-is-the-last.c rename to semester3/pset1/7-4-who-is-the-last.c diff --git a/semester3/pset2/2-1-Binary-Search-Tree.c b/semester3/pset2/2-1-Binary-Search-Tree.c new file mode 100644 index 0000000..32fa115 --- /dev/null +++ b/semester3/pset2/2-1-Binary-Search-Tree.c @@ -0,0 +1,88 @@ +#include +#include + +typedef struct treeNode { + int val; + struct treeNode *l; + struct treeNode *r; +} treeNode; + +treeNode *newNode(int val) { + treeNode *root = malloc(sizeof(treeNode)); + root->val = val; + root->l = NULL; + root->r = NULL; + return root; +} + +void insertTree(treeNode *root, int val) { + // Only insert at leaf. + treeNode *par; + int isL; + treeNode *ptr = root; + while (ptr != NULL) { + par = ptr; + if (ptr->val < val) { + ptr = ptr->r; + isL = 0; + } else { + ptr = ptr->l; + isL = 1; + } + } + + if (isL) { + par->l = newNode(val); + } else { + par->r = newNode(val); + } +} + +void freeTree(treeNode *root) { + if (root) { + freeTree(root->l); + freeTree(root->r); + free(root); + } +} + +void printLevel(treeNode *root, int level, int *hasOutput) { + if (root == NULL) { + return; + } + if (level == 1) { + *hasOutput = 1; + printf("%d,", root->val); + } else { + printLevel(root->l, level - 1, hasOutput); + printLevel(root->r, level - 1, hasOutput); + } +} + +int main(void) { + int size; + int level; + scanf("%d", &size); + + int tmp; + scanf("%d,", &tmp); + treeNode *root = newNode(tmp); + + for (int i = 1; i < size; i++) { + scanf("%d,", &tmp); + insertTree(root, tmp); + } + + scanf("%d", &level); + int *hasOutput = malloc(sizeof(int)); + *hasOutput = 0; + printLevel(root, level, hasOutput); + + if (*hasOutput == 0) { + printf("-1"); + } + + free(hasOutput); + freeTree(root); + return 0; +} diff --git a/semester3/pset2/2-2-Traverse-Binary-Search-Tree.c b/semester3/pset2/2-2-Traverse-Binary-Search-Tree.c new file mode 100644 index 0000000..1229a78 --- /dev/null +++ b/semester3/pset2/2-2-Traverse-Binary-Search-Tree.c @@ -0,0 +1,70 @@ +#include +#include + +typedef struct treeNode { + int val; + struct treeNode *l; + struct treeNode *r; +} treeNode; + +treeNode *newNode(int val) { + treeNode *root = malloc(sizeof(treeNode)); + root->val = val; + root->l = NULL; + root->r = NULL; + return root; +} + +void insertTree(treeNode *root, int val) { + // Only insert at leaf. + treeNode *par; + int isL; + treeNode *ptr = root; + while (ptr != NULL) { + par = ptr; + if (ptr->val < val) { + ptr = ptr->r; + isL = 0; + } else { + ptr = ptr->l; + isL = 1; + } + } + + if (isL) { + par->l = newNode(val); + } else { + par->r = newNode(val); + } +} + +void freeTree(treeNode *root) { + if (root) { + freeTree(root->l); + freeTree(root->r); + free(root); + } +} + +void preorderTraversal(treeNode *root) { + if (root) { + printf("%d,", root->val); + preorderTraversal(root->l); + preorderTraversal(root->r); + } +} + +int main(void) { + int tmp; + scanf("%d,", &tmp); + treeNode *root = newNode(tmp); + + while (scanf("%d,", &tmp) != EOF) { + insertTree(root, tmp); + } + + preorderTraversal(root); + + freeTree(root); + return 0; +} diff --git a/semester3/pset2/2-3-AVL-Tree.c b/semester3/pset2/2-3-AVL-Tree.c new file mode 100644 index 0000000..5a0602a --- /dev/null +++ b/semester3/pset2/2-3-AVL-Tree.c @@ -0,0 +1,131 @@ +#include +#include + +typedef struct treeNode { + int val; + struct treeNode *l; + struct treeNode *r; + int height; // used to calculate balance factor +} treeNode; + +int max(int a, int b) { return (a > b) ? a : b; } + +treeNode *newNode(int val) { + treeNode *root = malloc(sizeof(treeNode)); + root->val = val; + root->l = NULL; + root->r = NULL; + root->height = 1; // update this manually + return root; +} + +int height(treeNode *tr) { + // This function can be optimized. + if (!tr) { + return 0; + } else { + return 1 + max(height(tr->l), height(tr->r)); + } +} + +treeNode *leftRotate(treeNode *x) { + treeNode *y = x->r; + x->r = y->l; + y->l = x; + + // Update height here. + x->height = height(x); + y->height = height(y); + + return y; +} + +treeNode *rightRotate(treeNode *y) { + treeNode *x = y->l; + y->l = x->r; + x->r = y; + + x->height = height(x); + y->height = height(y); + + return x; +} + +int getBalance(treeNode *tr) { + if (!tr) { + return 0; + } else { + return height(tr->l) - height(tr->r); + } +} + +treeNode *insertTree(treeNode *root, int val) { + // Use recursion. + if (!root) { + return newNode(val); + } else { + if (val > root->val) { + root->r = insertTree(root->r, val); + } else { + root->l = insertTree(root->l, val); + } + + root->height = height(root); + + int bal = getBalance(root); + // check the balance using balance and comparing val with childs + if (bal > 1) { + if (val < root->l->val) { + // left left + return rightRotate(root); + } else { + // left right + root->l = leftRotate(root->l); + return rightRotate(root); + } + } else if (bal < -1) { + if (val > root->r->val) { + // right right + return leftRotate(root); + } else { + // right left + root->r = rightRotate(root->r); + return leftRotate(root); + } + } else { + // the balance is correct, don't change anything + return root; + } + } +} + +void freeTree(treeNode *root) { + if (root) { + freeTree(root->l); + freeTree(root->r); + free(root); + } +} + +void preorderTraversal(treeNode *root) { + if (root) { + printf("%d,", root->val); + preorderTraversal(root->l); + preorderTraversal(root->r); + } +} + +int main(void) { + int tmp; + treeNode *root = NULL; + + while (scanf("%d,", &tmp) != EOF) { + // update every time, since root can be changed. + root = insertTree(root, tmp); + } + + preorderTraversal(root); + + freeTree(root); + return 0; +}