Ac pset 2, change starting number

This commit is contained in:
juan 2022-10-14 18:47:09 +08:00
parent 85c1e39dc8
commit 8aba9a3d64
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
9 changed files with 411 additions and 1 deletions

View file

@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

View file

@ -0,0 +1,26 @@
#include <stdio.h>
#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);
}

View file

@ -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;
}
}

View file

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

View file

@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

View file

@ -0,0 +1,131 @@
#include <stdio.h>
#include <stdlib.h>
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;
}