aboutsummaryrefslogtreecommitdiff
path: root/CS2501/trees
diff options
context:
space:
mode:
Diffstat (limited to 'CS2501/trees')
-rwxr-xr-xCS2501/trees/tree0.c54
-rwxr-xr-xCS2501/trees/tree1.c106
-rwxr-xr-xCS2501/trees/tree2.c50
-rwxr-xr-xCS2501/trees/tree3.c60
-rwxr-xr-xCS2501/trees/tree4.c87
-rwxr-xr-xCS2501/trees/tree5.c65
-rwxr-xr-xCS2501/trees/tree6.c111
7 files changed, 533 insertions, 0 deletions
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);
+}