-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw4_tree_traversals.c
210 lines (198 loc) · 5.51 KB
/
hw4_tree_traversals.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
typedef struct _binaryTree
{
char keyValue;
char string[11];
struct _binaryTree* leftChild;
struct _binaryTree* rightChild;
} TREENODE;
typedef struct _treeStack
{
TREENODE* root;
struct _treeStack* next;
} ROOTS;
#define ARRAY 11
TREENODE* addChildren(TREENODE* treeTop, char newDatum,
char code[ARRAY]);
int preOrderTraversal(TREENODE* topOfTree);
int inOrderTraversal(TREENODE* topTree);
TREENODE* searchNode(char node, TREENODE *start);
ROOTS* push(TREENODE *save, ROOTS * topOfStack);
TREENODE* pop(ROOTS ** stackAddress);
int main()
{
TREENODE* root = NULL;
TREENODE* findNode = NULL;
char action = '\0';
char addDatum = '\0';
char searchFor = '\0';
char message[ARRAY] = { '\0' };
do
{
printf("[i for insert, f for find, q for quit] \
\n\rEnter action: ");
scanf(" %c", &action);
if(action == 'i')
{
printf("Enter new nodes's key value: ");
scanf(" %c", &addDatum);
printf("Enter a string of up to 10 characters for %c: ",
addDatum);
scanf("%s", message);
root = addChildren(root, addDatum, message);
printf("\nPre-order traversal: ");
preOrderTraversal(root);
printf("\nInorder traversal: ");
inOrderTraversal(root);
}
else if(action == 'f')
{
printf("Enter search key: ");
scanf(" %c", &searchFor);
findNode = searchNode(searchFor, root);
if (findNode != NULL)
printf("Found the string '%s' there. \n",
findNode->string);
else
printf("'%c' not in the tree \n", searchFor);
}
else if (action == 'q')
printf("Bye.\n");
else
printf("Invalid Input \n");
}
while (action != 'q');
return 0;
}
TREENODE* addChildren(TREENODE* treeTop, char newDatum,
char code[ARRAY])
{
TREENODE* newChild = NULL;
TREENODE* trvTree = treeTop;
int index = 0;
newChild = malloc(sizeof(TREENODE));
newChild->keyValue = newDatum;
for(index = 0; index < ARRAY; ++index)
newChild->string[index] = code[index];
if (treeTop == NULL)
treeTop = newChild;
else
while (trvTree != NULL)
if((newChild->keyValue < trvTree->keyValue)
&& (trvTree->leftChild == NULL))
{
trvTree->leftChild = newChild;
trvTree = NULL;
}
else if((newChild->keyValue > trvTree->keyValue)
&& (trvTree->rightChild == NULL))
{
trvTree->rightChild = newChild;
trvTree = NULL;
}
else if((newChild->keyValue < trvTree->keyValue)
&& (trvTree->leftChild != NULL))
trvTree = trvTree->leftChild;
else if((newChild->keyValue > trvTree->keyValue)
&& (trvTree->rightChild != NULL))
trvTree = trvTree->rightChild;
else
{
printf("Key value is already assigned node\n");
trvTree = NULL;
}
return treeTop;
}
int preOrderTraversal(TREENODE *topOfTree)
{
TREENODE* traverse = topOfTree;
ROOTS* findStack = NULL;
int complete = 0;
while(complete != 1)
{
while(traverse != NULL)
{
printf("%c ",traverse->keyValue);
if(traverse->leftChild != NULL)
{
findStack = push(traverse,findStack);
traverse = traverse->leftChild;
}
else
{
traverse = traverse->rightChild;
}
}
traverse = pop(&findStack);
if (traverse == NULL)
complete = 1;
else
traverse = traverse->rightChild;
}
return 1;
}
int inOrderTraversal(TREENODE* topTree)
{
TREENODE* travel = topTree;
ROOTS* stackTop = NULL;
int finished = 0;
while (finished != 1)
{
while(travel != NULL)
{
if(travel->leftChild != NULL)
{
stackTop = push(travel,stackTop);
travel = travel->leftChild;
}
else
{
printf("%c ",travel->keyValue);
travel = travel->rightChild;
}
}
travel = pop(&stackTop);
if (travel == NULL)
finished = 1;
else
{
printf("%c ",travel->keyValue);
travel = travel->rightChild;
}
}
return finished;
}
TREENODE* searchNode(char node, TREENODE* start)
{
TREENODE* traverse = start;
while (traverse != NULL)
if (node == traverse->keyValue)
return traverse;
else if (node < traverse->keyValue)
traverse = traverse->leftChild;
else
traverse = traverse->rightChild;
return NULL;
}
ROOTS* push(TREENODE* save, ROOTS* topOfStack)
{
ROOTS* newRoot = NULL;
newRoot = malloc(sizeof(ROOTS));
newRoot->root = save;
newRoot->next = topOfStack;
topOfStack = newRoot;
return topOfStack;
}
TREENODE* pop(ROOTS** stackAddress)
{
TREENODE* extract = NULL;
if (*stackAddress == NULL)
return NULL;
else
{
extract = (*stackAddress)->root;
*stackAddress = (*stackAddress)->next;
return extract;
}
}