-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
164 lines (128 loc) · 5.2 KB
/
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
#include <stdlib.h>
#include <assert.h>
#include "list.h"
#define MAX_NODE_COUNT 500 * 1000 + 1
typedef struct Tree Tree;
struct Tree {
ListItem *nodes[MAX_NODE_COUNT];
unsigned nodeCount;
unsigned nextNodeId;
};
Tree * createTree() {
Tree *tree = calloc(1, sizeof(Tree));
assert(tree != NULL);
tree->nodeCount = 1;
tree->nextNodeId = 1;
tree->nodes[0] = createListItem(0);
return tree;
}
static ListItem * addNodeAndReturnAPointer(Tree * const tree,
const unsigned targetNodeId) {
assert(tree != NULL && targetNodeId < MAX_NODE_COUNT &&
tree->nextNodeId < MAX_NODE_COUNT);
ListItem * const targetNode = tree->nodes[targetNodeId];
assert(targetNode != NULL);
ListItem * const newNode = createListItem(tree->nextNodeId);
tree->nodes[tree->nextNodeId] = newNode;
tree->nextNodeId += 1;
tree->nodeCount += 1;
ListItem * const oldListEnd = getChildrenListEndFromListItem(targetNode);
if (oldListEnd == NULL) {
leftAnchorListItem(targetNode, newNode);
rightAnchorListItem(targetNode, newNode);
}
else {
connectListItems(oldListEnd, newNode);
rightAnchorListItem(targetNode, newNode);
}
return newNode;
}
static void freeNode(Tree * const tree, ListItem * const targetNode) {
assert(tree != NULL && targetNode != NULL && tree->nodeCount != 0);
const unsigned targetNodeId = getIdFromListItem(targetNode);
assert(tree->nodes[targetNodeId] != NULL);
tree->nodes[targetNodeId] = NULL;
tree->nodeCount -= 1;
free(targetNode);
}
int getRightmostChildId(Tree * const tree, const unsigned targetNodeId) {
assert(tree != NULL && targetNodeId < MAX_NODE_COUNT);
ListItem * const targetNode = tree->nodes[targetNodeId];
assert(targetNode != NULL);
if (getChildrenListEndFromListItem(targetNode) == NULL) {
assert(getChildrenListBeginningFromListItem(targetNode) == NULL);
return -1;
}
else {
assert(getChildrenListBeginningFromListItem(targetNode) != NULL);
}
return getIdFromListItem(getChildrenListEndFromListItem(targetNode));
}
unsigned getNodeCount(Tree * const tree) {
assert(tree != NULL);
return tree->nodeCount;
}
void addNode(Tree * const tree, const unsigned targetNodeId) {
addNodeAndReturnAPointer(tree, targetNodeId);
}
void deleteNode(Tree * const tree, const unsigned targetNodeId) {
assert(tree != NULL && targetNodeId != 0 && targetNodeId < MAX_NODE_COUNT);
ListItem * const targetNode = tree->nodes[targetNodeId];
assert(targetNode != NULL);
ListItem * const childrenBeginning = getChildrenListBeginningFromListItem(targetNode);
ListItem * const childrenEnd = getChildrenListEndFromListItem(targetNode);
if (childrenBeginning != NULL) {
assert(childrenEnd != NULL);
insertListFragmentInPlaceOfListItem(targetNode, childrenBeginning, childrenEnd);
}
else {
assert(childrenEnd == NULL);
disconnectListItem(targetNode);
}
freeNode(tree, targetNode);
}
void splitNode(Tree * const tree, const unsigned targetNodeId,
const unsigned nodeToSplitAfterId) {
assert(tree != NULL && targetNodeId < MAX_NODE_COUNT &&
nodeToSplitAfterId < MAX_NODE_COUNT);
ListItem * const targetNode = tree->nodes[targetNodeId];
ListItem * const nodeToSplitAfter = tree->nodes[nodeToSplitAfterId];
assert(targetNode != NULL && nodeToSplitAfter != NULL);
ListItem * const newNodeChildrenListBeginning = nextListItem(nodeToSplitAfter);
ListItem * const newNodeChildrenListEnd = getChildrenListEndFromListItem(targetNode);
rightAnchorListItem(targetNode, nodeToSplitAfter);
ListItem * const newNode = addNodeAndReturnAPointer(tree, targetNodeId);
if (newNodeChildrenListBeginning != NULL) {
leftAnchorListItem(newNode, newNodeChildrenListBeginning);
rightAnchorListItem(newNode, newNodeChildrenListEnd);
}
}
void deleteSubtree(Tree * const tree, const unsigned targetNodeId) {
assert(tree != NULL && targetNodeId != 0 && targetNodeId < MAX_NODE_COUNT);
ListItem * const targetNode = tree->nodes[targetNodeId];
assert(targetNode != NULL);
ListItem * currentNode = getChildrenListBeginningFromListItem(targetNode);
while (currentNode != targetNode && currentNode != NULL) {
if (getChildrenListBeginningFromListItem(currentNode) != NULL) {
currentNode = getChildrenListBeginningFromListItem(currentNode);
}
else {
ListItem * const nextNode = nextListItemOrAnchor(currentNode);
disconnectListItem(currentNode);
freeNode(tree, currentNode);
currentNode = nextNode;
}
assert(currentNode != NULL);
}
disconnectListItem(targetNode);
freeNode(tree, targetNode);
}
void freeTree(Tree * const tree) {
assert(tree != NULL);
for (unsigned i = 0; i < tree->nextNodeId; ++i) {
if (tree->nodes[i] != NULL) {
freeNode(tree, tree->nodes[i]);
}
}
free(tree);
}