Newer
Older
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
#include <stdio.h> // fprintf(), printf(), ...
#include <stddef.h> // NULL
#include <stdlib.h> // malloc(), ...
#include "syntree.h"
#define NODE_LIST_INIT_SIZE_IN_UNITS 1024
#define NODE_LIST_ADD_WHEN_FULL_IN_UNITS 1024
#define SIZE_OF_UNIT (sizeof (Node))
#define NODE_CHILDREN_LIST_INIT_SIZE_IN_UNITS 10
#define NODE_CHILDREN_LIST_ADD_WHEN_FULL_IN_UNITS 10
#define ERR_ALLOCATING_MEM -1
int syntreeInit(Syntree *self) {
self->next_ID = 0;
self->root = self->next_ID;
self->next_ID++;
self->size = 1;
self->node_list = malloc(SIZE_OF_UNIT * NODE_LIST_INIT_SIZE_IN_UNITS);
self->capacity = NODE_LIST_INIT_SIZE_IN_UNITS;
if (self->node_list == NULL) {
fprintf(stderr, "ERROR during initilalization of syntree: received NULL pointer from malloc()\n");
return ERR_ALLOCATING_MEM;
}
self->node_list[self->root].val = 0;
self->node_list[self->root].amount_of_children = 0;
self->node_list[self->root].capacity_children = 0;
self->node_list[self->root].children = NULL;
return 0;
}
void syntreeRelease(Syntree *self) {
for (int i = 0; i < self->size; ++i) {
free(self->node_list[i].children); // free all references to children from all nodes
}
free(self->node_list); // free all nodes
}
SyntreeNodeID syntreeNodeNumber(Syntree *self, int number) {
SyntreeNodeID id = self->next_ID++;
if (self->size < self->capacity) {
self->node_list[id].val = number;
self->node_list[id].amount_of_children = 0;
self->node_list[id].capacity_children = 0;
self->node_list[id].children = NULL;
} else {
/*TODO realloc()*/
}
return id;
}
SyntreeNodeID syntreeNodeTag(Syntree *self, SyntreeNodeID id) {
SyntreeNodeID new_id = self->next_ID++;
if (self->size < self->capacity) {
self->node_list[new_id].val = 0;
self->node_list[new_id].amount_of_children = 1;
self->node_list[new_id].capacity_children = NODE_CHILDREN_LIST_INIT_SIZE_IN_UNITS;
self->node_list[new_id].children = malloc( (sizeof (SyntreeNodeID)) * NODE_CHILDREN_LIST_INIT_SIZE_IN_UNITS);
if (self->node_list[new_id].children == NULL) {
fprintf(stderr, "ERROR in function syntreeNodeTag(...): received NULL pointer from malloc()\n");
exit(ERR_ALLOCATING_MEM);
}
self->node_list[new_id].children[0] = id;
} else {
/*TODO realloc()*/
}
if (self->root == 0) {
self->root = new_id;
}
return new_id;
}
SyntreeNodeID syntreeNodePair(Syntree *self, SyntreeNodeID id1, SyntreeNodeID id2) {
return syntreeNodeAppend(self, syntreeNodeTag(self,id1), id2);
}
SyntreeNodeID syntreeNodeAppend(Syntree *self, SyntreeNodeID list, SyntreeNodeID elem) {
if (self->node_list[list].amount_of_children < self->node_list[list].capacity_children) {
self->node_list[list].children[self->node_list[list].amount_of_children++] = elem;
} else {
/*TODO realloc()*/
}
return list;
}
SyntreeNodeID syntreeNodePrepend(Syntree *self, SyntreeNodeID elem, SyntreeNodeID list) {
if (self->node_list[list].amount_of_children < self->node_list[list].capacity_children) {
// move all IDs in the children list 1 up and add elem at index 0
for (int j = self->node_list[list].amount_of_children; j > 0; --j) {
self->node_list[list].children[j] = self->node_list[list].children[j-1];
}
self->node_list[list].children[0] = elem;
self->node_list[list].amount_of_children++;
} else {
/*TODO realloc()*/
}
return list;
}
void syntreePrint(const Syntree *self, SyntreeNodeID root) {
syntreePrint_rec(self, root);
if (self != NULL) {
printf("\n");
} else {
printf("<<Tree empty>>\n");
}
return;
}
void syntreePrint_rec(const Syntree *self, SyntreeNodeID root) {
if (self == NULL)
return;
if (self->node_list[root].amount_of_children == 0)
printf("(%d)", self->node_list[root].val);
else {
printf("{");
for (int i = 0; i < self->node_list[root].amount_of_children; ++i) {
syntreePrint_rec(self, self->node_list[root].children[i]);
};
printf("}");
}
return;
}