aboutsummaryrefslogtreecommitdiff
path: root/CS2501/trees/tree1.c
blob: 06153778272631296d1d09001d724952c9e68b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
}