-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path_Tree.c
255 lines (223 loc) · 6.29 KB
/
Path_Tree.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "Knight_Tour.h"
/******************* Function Prototypes *******************/
static void findAllPossiblePathsAux(treeNode* head, boardPosArray** boardPosArr, TrackList* trace);
static void makeEmptyList(pathTree* list);
static treeNode* createNewTNode(boardPos data, treeNodeListCell* list);
static treeNodeListCell* createNewListCell(treeNode* node, treeNodeListCell* nextCell);
static trackListCell* createTrackListCell(treeNode* pos, trackListCell* next);
static void addToEndOfList(treeNodeListCell** head, treeNode* node);
static bool isCellAlreadyBeenVisited(TrackList* list, boardPos currPos);
static void convertToboardLoc(int* row, int* col, boardPos pos);
static void freeList(treeNode* list);
static void printTreeAux(treeNode* node, int level);
static void freeTraceList(TrackList* list);
static void insertToEndTrackList(TrackList* list, treeNode* pos);
static void addToTrackTail(TrackList* list, trackListCell* cellToAdd);
static void deleteFromEndTrackList(TrackList* list);
static bool TrackListIsEmpty(TrackList* list);
/*********************************************************/
/******************* Function Implementation *******************/
pathTree findAllPossiblePaths(boardPos start, movesArray** moves, char** board)
{
pathTree path;
TrackList* traceList = (TrackList*)malloc(INIT_SIZE * sizeof(TrackList));
boardPosArray** boardPosArr;
int size = ROWS * COLS;
boardPosArr = (boardPosArray**)malloc(size * sizeof(boardPosArray*));
CheckMemoryAllocation(boardPosArr);
boardPosArr = validMoves(moves, board);
path.head = createNewTNode(start, NULL);
traceList->head = createTrackListCell(path.head, NULL);
traceList->tail = traceList->head;
findAllPossiblePathsAux(path.head, boardPosArr, traceList);
freeTraceList(traceList);
return path;
}
static void findAllPossiblePathsAux(treeNode* head, boardPosArray** boardPosArr, TrackList* trace)
{
boardPos* positions;
treeNode* curr;
int row, col, posSize, i;
row = col = 0;
convertToboardLoc(&row, &col, head->position);
positions = boardPosArr[row + col]->positions;
posSize = boardPosArr[row + col]->size;
if (posSize == 0)
return;
for (i = 0; i < posSize; i++) {
curr = createNewTNode(&positions[i], NULL);
CheckMemoryAllocation(curr);
if (!isCellAlreadyBeenVisited(trace, positions[i])) {
addToEndOfList(&head->next_possible_positions, curr);
insertToEndTrackList(trace, curr);
findAllPossiblePathsAux(curr, boardPosArr, trace);
deleteFromEndTrackList(trace);
}
else
free(curr);
}
}
static bool isCellAlreadyBeenVisited(TrackList* list, boardPos currPos)
{
trackListCell* curr = list->head;
while (curr)
{
if (curr->position[0] == currPos[0] && curr->position[1] == currPos[1])
return true;
curr = curr->next;
}
return false;
}
void convertToboardLoc(int* row, int* col, boardPos pos)
{
*row = (pos[0] - 65) * COLS;
if ((pos[1] % COLS) != 0)
*col = (pos[1] % COLS) - 1;
else
*col = COLS - 1;
}
static void makeEmptyList(pathTree* list)
{
list->head = NULL;
}
static void addToEndOfList(treeNodeListCell** head, treeNode* node)
{
treeNodeListCell* curr = *head;
treeNodeListCell* prev = NULL;
if (curr) {
while (curr->next)
curr = curr->next;
curr->next = createNewListCell(node, NULL);
CheckMemoryAllocation(curr->next);
}
else {
curr = createNewListCell(node, NULL);
CheckMemoryAllocation(curr);
*head = curr;
}
}
static treeNodeListCell* createNewListCell(treeNode* node, treeNodeListCell* nextCell)
{
treeNodeListCell* newCell;
newCell = (treeNodeListCell*)malloc(sizeof(treeNodeListCell));
CheckMemoryAllocation(newCell);
newCell->node = node;
newCell->next = nextCell;
return newCell;
}
static trackListCell* createTrackListCell(treeNode* pos, trackListCell* next)
{
trackListCell* newCell = NULL;
newCell = (trackListCell*)malloc(sizeof(trackListCell));
CheckMemoryAllocation(newCell);
newCell->position[0] = pos->position[0];
newCell->position[1] = pos->position[1];
newCell->next = next;
return newCell;
}
static void insertToEndTrackList(TrackList* list, treeNode* pos)
{
trackListCell* newTail = createTrackListCell(pos, NULL);
addToTrackTail(list, newTail);
}
static treeNode* createNewTNode(boardPos data, treeNodeListCell* list)
{
treeNode* res;
res = (treeNode*)malloc(sizeof(treeNode));
CheckMemoryAllocation(res);
res->position[0] = data[0];
res->position[1] = data[1];
res->next_possible_positions = list;
return res;
}
/* free all memory of a tree */
void freeTree(pathTree* tr)
{
freeList(tr->head);
}
static void freeTraceList(TrackList* list)
{
trackListCell* curr = list->head;
trackListCell* next = NULL;
while (curr)
{
next = curr->next;
free(curr->position);
curr = next;
}
}
static void freeList(treeNode* root)
{
treeNodeListCell* curr = root->next_possible_positions;
treeNodeListCell* next;
while (curr)
{
next = curr->next;
free(curr->node);
free(curr);
curr = next;
}
root->next_possible_positions = NULL;
}// need to add free list func
void printTree(pathTree tree)
{
printTreeAux(tree.head, 0);
}
static void printTreeAux(treeNode* node, int level)
{
int i;
treeNodeListCell* curr = node->next_possible_positions;
if (!(curr))
{
for (i = 0; i < level; i++)
{
printf(" ");
}
printf("L%d: %c%d leaf\n", level, node->position[0], node->position[1]);
}
else
{
for (i = 0; i < level; i++)
{
printf(" ");
}
printf("L%d: %c%d\n", level, node->position[0], node->position[1]);
while (curr)
{
printTreeAux(curr->node, level + 1);
curr = curr->next;
}
}
}
static void addToTrackTail(TrackList* list, trackListCell* cellToAdd)
{
if (TrackListIsEmpty(list))
list->head = list->tail = cellToAdd;
else
{
list->tail->next = cellToAdd;
list->tail = cellToAdd;
}
}
static bool TrackListIsEmpty(TrackList* list)
{
if (list->head == NULL)
return true;
return false;
}
static void deleteFromEndTrackList(TrackList* list)
{
trackListCell* head, * prev;
head = list->head;
prev = head;
if (TrackListIsEmpty(list))
return;
while (head->next != NULL)
{
prev = head;
head = head->next;
}
free(prev->next);
list->tail = prev;
prev->next = NULL;
}