aboutsummaryrefslogtreecommitdiff
path: root/CS2501/trees/tree1.c
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
commitb4dbd2cfa724476162fa6d35941a5d7cdc9c9524 (patch)
tree431af0b75efa29dfa3bab2868a78ab0eb29173c7 /CS2501/trees/tree1.c
parente8b1808eaf87a49e4c34ebbfb66854baa627418c (diff)
Adds all assignments not previously in a git repo
Diffstat (limited to 'CS2501/trees/tree1.c')
-rwxr-xr-xCS2501/trees/tree1.c106
1 files changed, 106 insertions, 0 deletions
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;
+}