diff options
Diffstat (limited to 'CS2501')
-rw-r--r-- | CS2501/bstAssignment.c | 215 | ||||
-rw-r--r-- | CS2501/graph.c | 116 | ||||
-rw-r--r-- | CS2501/sorting/sortAssignment1.c | 80 | ||||
-rw-r--r-- | CS2501/sorting/sortAssignment2.c | 94 | ||||
-rwxr-xr-x | CS2501/trees/tree0.c | 54 | ||||
-rwxr-xr-x | CS2501/trees/tree1.c | 106 | ||||
-rwxr-xr-x | CS2501/trees/tree2.c | 50 | ||||
-rwxr-xr-x | CS2501/trees/tree3.c | 60 | ||||
-rwxr-xr-x | CS2501/trees/tree4.c | 87 | ||||
-rwxr-xr-x | CS2501/trees/tree5.c | 65 | ||||
-rwxr-xr-x | CS2501/trees/tree6.c | 111 |
11 files changed, 1038 insertions, 0 deletions
diff --git a/CS2501/bstAssignment.c b/CS2501/bstAssignment.c new file mode 100644 index 0000000..6fff4a7 --- /dev/null +++ b/CS2501/bstAssignment.c @@ -0,0 +1,215 @@ +#include<stdlib.h> +#include<stdio.h> +#include <string.h> + +typedef struct anode { + char *name; + struct anode *r, *l; +} node; + + + +node* contains(tree, q) +node **tree; +char *q; +{ + node *tmp; + if(!(*tree)) return 0; + + if ( strcmp((*tree)->name, q) == 0){ + return *tree; + } + tmp = contains(&(*tree)->l, q); + return tmp ? tmp : contains(&(*tree)->r, q); +} + + + +insert(tree, item) +node **tree; +node *item; +{ + if(!(*tree)) { + *tree = item; + return 1; + } + + if(strcmp(item->name, (*tree)->name) < 0) + insert(&(*tree)->l, item); + + else if(strcmp(item->name, (*tree)->name) > 0) + insert(&(*tree)->r, item); +} + +node* getParent(tree,item) +node **tree; +node *item; +{ + node *tmp; + if(!(*tree)) return 0; + if(*tree == item) + return -1; + if((*tree)->l == item || (*tree)->r == item) + return *tree; + tmp = getParent(&(*tree)->l, item); + return tmp ? tmp : getParent(&(*tree)->r, item); +} + +delete(tree, item) +node **tree; +node *item; +{ + node *tmp, *parent, *tmp_parent; + int cnt; + if(!(*tree)) return 1; + tmp = contains(tree, item->name); + if(!tmp){ + printf("%s dose not exist\n", item->name); + return 1; + } + parent = getParent(tree, tmp); + if(!tmp->l && !tmp->r) + { + if (parent == -1){ + *tree = 0; + return 1; + free(tmp); + } + if(parent->l == tmp){ + parent->l = 0; + free(tmp); + return 1; + } + parent->r = 0; + free(tmp); + return 1; + } + if(tmp->l && tmp->r){ + item = tmp->r; + while(item->l) + item = item->l; + + tmp_parent = getParent(tree, item); + if(parent == -1){ + *tree = item; + item->l = tmp->l; + item->r = tmp->r; + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + + } + if(parent->l == tmp){ + parent->l = item; + item->l = tmp->l; + item->r = tmp->r; + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + } + parent->r = item; + item->l = tmp->l; + item->r = tmp->r; + + + if(tmp_parent == tmp) + item->r = 0; + else + tmp_parent->l = 0; + + tree = &item; + free(tmp); + return 1; + + } + + if (parent == -1) { + *tree = tmp->l ? tmp->l : tmp->r; + free(tmp); + return 1; + } + if(parent->l == tmp){ + parent->l = tmp->l ? tmp->l : tmp->r; + free(tmp); + return 1; + } + + parent->r = tmp->l ? tmp->l : tmp->r; + return 1; + + +} + + + + +void printTree(tree) +node **tree; +{ + if (!(* tree)) return; + printTree(&(*tree)->l); + printTree(&(*tree)->r); + printf("\t%s\n",(*tree)->name); + return; +} + + +/* +The loop below reads from stdin until EOF, then searches +to see if the BST contains the word found in argv[1]. + +Modify the loop to read a series of lines of one of two types: + +i value +d value + +Lines starting with the ASCII i are to be inserted into the binary search tree. +Those beginning with 'd' are to be deleted if they exist. If they do not exist, +output a line of text saying so and do nothing else. + +Stop your read loop at EOF (like it is now). Instead of +calling contains(), print out the tree; make it a preorder print */ + + + +void main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + int i; + char buf[1024]; + node *curr, *root; + + root = 0; + + while (gets(buf)) { + curr = (node *)malloc(sizeof(node)); + curr->l = curr->r = NULL; + curr->name = strdup(buf + 2); + if('i' == *buf){ + printf("insert: %s %s\n", buf, curr->name); + insert(&root, curr); + } + else if('d' == *buf){ + printf("delete:%s %s\n", buf, curr->name); + delete(&root, curr); + } + else if ('p' == *buf) + printTree(&root); + + } + printTree(&root); + +} diff --git a/CS2501/graph.c b/CS2501/graph.c new file mode 100644 index 0000000..75d2353 --- /dev/null +++ b/CS2501/graph.c @@ -0,0 +1,116 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct agraph{ + int matrix[100][100]; +} graph; + +extern graph *g = 0; + +void init_graph() +{ + int i; + g = malloc(sizeof(graph)); + for (i = 0; i < 100; i++){ + g->matrix[i][i] = -1; + } +} + +int adjacent(x,y) +int x,y; +{ + return g->matrix[x][y]; +} + +int* neighbors(x) +int x; +{ + int i, j; + j = 1; + for(i = 0; i < 100; i++) + { + if(g->matrix[x][i]){ + j++; + } + } + int *y; + y = calloc(j * sizeof(int)); + j = 0; + for(i = 0; i < 100; i++) { + if (g->matrix[x][i]){ + y[j++] = i; + } + } + return y; +} + +int add_vertex(x) +int x; +{ + if(!g){ + init_graph(); + } + if (g->matrix[x][x] == -1){ + g->matrix[x][x] = 0; + return 1; + } + return 0; +} + +int remove_vertex(x) +int x; +{ + if(g->matrix[x][x] == -1){ + return 0; + } + int i; + for(i = 0; i < 100; i++){ + g->matrix[x][i] = 0; + g->matrix[i][x] = 0; + } + return 1; +} + +int add_edge(x,y) +int x,y; +{ + if (g->matrix[x][y]){ + return 0; + } + + g->matrix[x][y] = 1; + return 1; +} + +int remove_edge(x,y) +int x,y; +{ + if(g->matrix[x][y]){ + g->matrix[x][y] = 0; + return 1; + } + return 0; + +} + +void printEdges() +{ + printf("Edges: \n" ); + int i,j; + for (i = 0; i < 100; i++){ + for(j = 0; j < 100; j++){ + if(g->matrix[i][j] == 1) + printf("\t%d -> %d\n", i, j); + } + } +} + +void printVertices() +{ + printf("Vertices:\n"); + int i; + for(i = 0; i <100; i++) + if(g->matrix[i][i] != -1) + printf("\t%d\n", i); + +} diff --git a/CS2501/sorting/sortAssignment1.c b/CS2501/sorting/sortAssignment1.c new file mode 100644 index 0000000..1ff5662 --- /dev/null +++ b/CS2501/sorting/sortAssignment1.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + + +printData(d,n) +int *d,n; +{ + int i; + for (i=0; i<n; i++) + printf("data[%d] = %d\n", i, d[i]); +} + + +selectionSortAscending(d, n) +int *d, n; +{ + int i, j, tmp; + + for (i = 0; i < n; i++) { + tmp = d[i]; + for (j = i + 1; j < n; j++) { + if(tmp > d[j]){ + d[i] = d[j]; + d[j] = tmp; + tmp = d[i]; + } + } + + } +} + +insertionSortDescending(d, n) +int *d, n; +{ + int i,j,tmp; + + for (i = 1; i < n; i++){ + for (j = i; d[j] > d[j-1] && j != 0; j--){ + tmp = d[j]; + d[j] = d[j-1]; + d[j-1] = tmp; + } + } +} + + + + +main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + char buf[1024]; + int n, *data, i; + if (argc !=2 ) { + printf("usage: sortAssignment1 #elements\n"); + exit(1); + } + + n = atoi(argv[1]); + data = calloc(n, sizeof(int)); + for (i=0; i<n; i++) { + gets(buf); + data[i] = atoi(buf); + } + + + printf("unsorted data:\n"); sleep(1); + printData(data, n); + + + selectionSortAscending(data, n); + printf("After selection sort\n"); sleep(1); + printData(data, n); + + insertionSortDescending(data, n); + printf("After insertion sort\n"); sleep(1); + printData(data, n); +} diff --git a/CS2501/sorting/sortAssignment2.c b/CS2501/sorting/sortAssignment2.c new file mode 100644 index 0000000..508690d --- /dev/null +++ b/CS2501/sorting/sortAssignment2.c @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + + +printData(d,n) +int *d,n; +{ + int i; + for (i=0; i<n; i++) + printf("data[%d] = %d\n", i, d[i]); +} + + +/* implement (only) one of your choosing */ +quickSort(d, n) +int *d, n; +{ +} + +mergeSort(d, n) +int *d, n; +{ + if (n == 1) + return; + + + int i, n1, n2, *tmp1, *tmp2; + + n1 = n/2; + n2 = (n%2 == 0) ? n1 : (n1 + 1); + + /* + printf("recursion!%d %d %d %x\n", n1, n2, n, d ); + */ + mergeSort(d, n1); + mergeSort(&d[n1], n2); + + tmp1 = malloc(sizeof(int) * n1); + tmp2 = malloc(sizeof(int) * n2); + + memcpy(tmp1, d, sizeof(int) * n1); + memcpy(tmp2, &d[n1], sizeof(int) * n2); + + + for (i = n-1; i >= 0; i--) { + if(n2 == 0) + d[i] = tmp1[--n1]; + + else if (n1 == 0) + d[i] = tmp2[--n2]; + else + d[i] = (tmp1[n1-1] < tmp2[n2-1]) ? tmp1[--n1] : tmp2[--n2]; + } + printf("\n"); + free(tmp1); + free(tmp2); + return; +} + +heapSort(d, n) +int *d, n; +{ +} + + + + +main(argc, argv, envp) +int argc; +char **argv, **envp; +{ + char buf[1024]; + int n, *data, i; + if (argc !=2 ) { + printf("usage: sortAssignment1 #elements\n"); + exit(1); + } + + n = atoi(argv[1]); + data = calloc(n, sizeof(int)); + for (i=0; i<n; i++) { + gets(buf); + data[i] = atoi(buf); + } + + + printf("unsorted data:\n"); sleep(1); + printData(data, n); + + mergeSort(data, n); + printf("After sort\n"); + printData(data, n); +} diff --git a/CS2501/trees/tree0.c b/CS2501/trees/tree0.c new file mode 100755 index 0000000..f9d6585 --- /dev/null +++ b/CS2501/trees/tree0.c @@ -0,0 +1,54 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *a, *b, *c, *d, *z; + + z = malloc(sizeof(treenode)); + z->id = 'z'; + z->left = 0; + z->right= 0; + + d = malloc(sizeof(treenode)); + d->id = 'd'; + d->left = 0; + d->right= 0; + + c = malloc(sizeof(treenode)); + c->id = 'c'; + c->left = d; + c->right= z; + + b = malloc(sizeof(treenode)); + b->id = 'b'; + b->left = 0; + b->right= 0; + + a = malloc(sizeof(treenode)); + a->id = 'a'; + a->left = b; + a->right= c; + +/* manually create nodes to build this tree: + + a + / \ + b c + / \ + d z + +*/ +} diff --git a/CS2501/trees/tree1.c b/CS2501/trees/tree1.c new file mode 100755 index 0000000..0615377 --- /dev/null +++ b/CS2501/trees/tree1.c @@ -0,0 +1,106 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +/* FILL ME in */ +inorder(t) +treenode *t; +{ +if(!t) return 1; +inorder(t->left); +printf("%c\n", t->id); +inorder(t->right); +} + + + +/* FILL ME in */ +postorder(t) +treenode *t; +{ +if(!t) return 1; + postorder(t->left); + postorder(t->right); +printf("%c\n",t->id); +} + + + +preorder(t) +treenode *t; +{ +if(!t) return 1; +printf("%c",t->id); +printf("--%x %x\n",t->left,t->right); +preorder(t->left); +preorder(t->right); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + +/* print out the tree from the first assignment: + + a + / \ + b c + / \ + d z + + print the nodes out three times: + preorder + inorder + postorder + +you will have to create the inorder() and postorder() +print functions to do this +*/ +treenode *a, *b, *c, *d, *z; + +z = malloc(sizeof(treenode)); +z->id = 'z'; +z->left = 0; +z->right= 0; + +d = malloc(sizeof(treenode)); +d->id = 'd'; +d->left = 0; +d->right= 0; + +c = malloc(sizeof(treenode)); +c->id = 'c'; +c->left = d; +c->right= z; + +b = malloc(sizeof(treenode)); +b->id = 'b'; +b->left = 0; +b->right= 0; + +a = malloc(sizeof(treenode)); +a->id = 'a'; +a->left = b; +a->right= c; + +printf("INORDER\n"); +inorder(a); +printf("PREORDER\n"); +preorder(a); +printf("POSTORDER\n"); +postorder(a); + +return 1; +} diff --git a/CS2501/trees/tree2.c b/CS2501/trees/tree2.c new file mode 100755 index 0000000..25bf50b --- /dev/null +++ b/CS2501/trees/tree2.c @@ -0,0 +1,50 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +int countNodes(t) +treenode *t; +{ + int i = 0; + if(!t) return 0; + return 1 + countNodes(t->left) + countNodes(t->right); + +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *t; + t = malloc(sizeof(treenode)); + /* build a binary tree with at least 6 nodes */ + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + printf("the tree has %d nodes\n", countNodes(t)); +} diff --git a/CS2501/trees/tree3.c b/CS2501/trees/tree3.c new file mode 100755 index 0000000..89e68c7 --- /dev/null +++ b/CS2501/trees/tree3.c @@ -0,0 +1,60 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + + +int countLevels(t) +treenode *t; +{ + if(!t) return 0; + return 1 + ((countLevels(t->left) > countLevels(t->right)) ? countLevels(t->left) : countLevels(t->right)); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + treenode *t; + t = malloc(sizeof(treenode)); + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + t->left->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + + + t->left->left->left->left = 0; + t->left->left->left->right = 0; + + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + + printf("the tree has %d levels\n", countLevels(t)); +} diff --git a/CS2501/trees/tree4.c b/CS2501/trees/tree4.c new file mode 100755 index 0000000..77b64d9 --- /dev/null +++ b/CS2501/trees/tree4.c @@ -0,0 +1,87 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + + +/* WRITE A RECURSIVE routine which swaps the + left and right child for every node in a given tree */ +/* will this operate in pre, in, or post order fashion? +does it matter? if so, why does it matter? */ + +rotate(t) +treenode *t; +{ + treenode *tmp; + if(!t) return; + rotate(t->left); + rotate(t->right); + tmp = t->left; + t->left = t->right; + t->right = tmp; +} + + +preorder(t) +treenode *t; +{ + if (!t) return; + + printf("%d\n", t->id); + preorder(t->left); + preorder(t->right); +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + treenode *t; + t = malloc(sizeof(treenode)); + /* build a binary tree with at least 6 nodes */ + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + /* build a binary tree with at least 6 nodes */ + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + + + /* build a binary tree with at least 6 nodes */ + + /* now show your rotate function works */ + preorder(t); + rotate(t); + printf("\n"); + preorder(t); + +} diff --git a/CS2501/trees/tree5.c b/CS2501/trees/tree5.c new file mode 100755 index 0000000..f2482f5 --- /dev/null +++ b/CS2501/trees/tree5.c @@ -0,0 +1,65 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + int id; + struct node *left; + struct node *right; +} treenode; + + +/* FILL ME in */ +breadthFirst(t) +treenode *t; +{ + treenode **q; + int front, back; + front = 0; + back = 0; + q = malloc(100 * sizeof(treenode*)); + *q = t; + while(q[back]){ + printf("%d\n", q[back]->id); + q[++front] = q[back]->left; + q[++front] = q[back++]->right; + } +} + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + + treenode *t; + t = malloc(sizeof(treenode)); + t->right = malloc(sizeof(treenode)); + t->left = malloc(sizeof(treenode)); + t->right->right = malloc(sizeof(treenode)); + t->right->left = malloc(sizeof(treenode)); + t->left->right = malloc(sizeof(treenode)); + t->left->left = malloc(sizeof(treenode)); + + t->right->right->right = 0; + t->right->right->left = 0; + t->right->left->right = 0; + t->right->left->left = 0; + t->left->right->right = 0; + t->left->right->left = 0; + t->left->left->right = 0; + t->left->left->left = 0; + + t->id = 1; + t->right->id = 3; + t->left->id = 2; + + t->right->right->id = 7; + t->right->left->id = 6; + t->left->right->id = 5; + t->left->left->id = 4; + /* build a tree any way you like with at least 5 nodes */ + /* print it out breadth first, use a queue (so write one first) */ + breadthFirst(t); +} diff --git a/CS2501/trees/tree6.c b/CS2501/trees/tree6.c new file mode 100755 index 0000000..cbe95c2 --- /dev/null +++ b/CS2501/trees/tree6.c @@ -0,0 +1,111 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + + +typedef struct node { + char *name; + struct node *left; + struct node *right; +} treenode; + +typedef struct queue { + int front,back; + treenode **data; +} queue; + + + + +inorder(t) +treenode *t; +{ + if (!t) return; + inorder(t->left); + printf("%s\n",t->name); + inorder(t->right); +} + + + +addNode(n,v,q) +treenode **n; +char *v; +queue *q; +{ + treenode *nxt; + nxt = malloc(sizeof(treenode)); + memset(nxt, 0, sizeof(nxt)); + + nxt->name = v; /* safe - caller creates new storage for each */ + enqueue(nxt,q); + +} + +enqueue(t,q) +treenode *t; +queue *q; +{ + q->data[(q->front)++] = t; +} + + + + +main (argc, argv, envp) +int argc; +char **argv, **envp; +{ + +/* + +prompt the user for a series of names and add them +in the order given to create a complete binary tree + + +the user enters + giselle + magda + aviva + ahnk + havi + minna + monique + ^d + + +you build: + + + giselle + / \ + magda aviva + / \ / \ + ahnk havi minna monique + + +then print it out inorder +this should work for any number of name entered. +(consider using a queue) +*/ + + treenode *t = 0; + char buf[1024]; + queue q; + q.back = 0; + q.front = 0; + q.data = calloc(100,sizeof(treenode*)); + + while (gets(buf)) + addNode(&t, strdup(buf),&q); + + printf("q.back %d q.front %d", q.back, q.front); + while(q.back < q.front){ + printf("bulid tree pass %d %x\n", q.back,q.data[q.back]); + q.data[q.back]->left = q.data[((2 * q.back) +1)]; + q.data[q.back]->right = q.data[((2 * q.back) + 2)]; + q.back++; + } + t = q.data[0]; + inorder(t); +} |