From b4dbd2cfa724476162fa6d35941a5d7cdc9c9524 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 18 Feb 2019 08:10:10 -0500 Subject: Adds all assignments not previously in a git repo --- CS2501/trees/tree6.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100755 CS2501/trees/tree6.c (limited to 'CS2501/trees/tree6.c') 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 +#include +#include + + +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); +} -- cgit v1.1