forked from ranjanvittal/LinearGraphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
213 lines (135 loc) · 4.45 KB
/
tree.h
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
#ifndef LINEAR_TREE
#define LINEAR_TREE
#include "dynarray.h"
#include <vector>
using namespace std;
#define DEFAULT_DEGREE 3
struct Node {
int id;
int extension;
int sat_id;
int natural;
bool is_main;
//Should be the last attribute
int edge[1];
};
class Tree{
public:
/*
Tree Constructor taking parameters as :
hcount = Number of hierarchies
sizes = Array with sizes of each hierarchy
dd = Default degree for optimum construction
undir = Is the graph undirected?
*/
Tree(int hcount, int sizes[], int dd = DEFAULT_DEGREE, bool undir = false);
/*
GetDataRow
Returns the data of a node corresponding to a class hierarchy
Parameters
node_id = ID of the node whose data you want
sat_row = Index of the class hierarchy whose data you want
*/
void * GetDataRow(int node_id, int sat_row);
/*
GetDataRow
Returns all data of a node in the form of an array of pointers.
Parameters
node_id = ID of the node whose data you want
ptrs = Array of pointers appropriately sized. If this is NULL,
then an array is allocated by GetData
*/
void ** GetData(int node_id, void** ptrs);
/*
AddNode
Adds a node to the graph
*/
int AddNode ();
/*
AddEdge
Adds an edge to the graph
Parameters
node_id1 = First node
node_id2 = Second node
*/
void AddEdge (int node_id1, int node_id2);
/*
AddEdge
Adds an edge to the graph
Parameters
node_id1 = First node
node_id2 = Second node
*/
void AddDirectedEdge(int id1, int id2);
/*
IsNeighbour
Checks if two nodes are neighbours
Parameters
node_id1 = First node
node_id2 = Second node
*/
bool IsNeighbour(int node_id1, int node_id2);
/*
Print
Used for development, testing and design purposes.
We use the output of this to contemplate what further
improvements can be inducted into the project.
*/
void Print();
/*
Sort based on comparion
*compare = Element comparator function that accepts arguments node1, node2, and a map
(Note that third parameter must be there)
map = A map of comparison that is passed as-is to the compare function
(pass NULL if this is unnecessary)
This is also the wrapper for SortPrivate function.
*/
void Sort(int (*compare) (int node1, int node2, void* map), void* map);
//The vector storing nodes
Vector graph;
/*
Function to go through the nodes in a DFS manner and do previsit and postvisit
on the nodes.
Parameters :
node_id: Root node
previsit : Function executed on visiting the node.
postvisit: Function executed after traversing the subtree rooted at the node and returning to the parent
*/
void DFS (int node_id, int (*previsit) (Tree* t, int node_id), void (*postvisit) (Tree* t, int node_id), bool is_natural = true );
/*
Small wrapper to the above DFS method.
*/
int DFSOptimizer(int rootnode);
private:
//The default number of edges stored in each node
int default_degree;
//Whether the graph is undirected
bool undirected;
vector<int> map_natural_to_node;
//Number of nodes in the graph so far
int node_count;
//Array of vectors to store satellite data
Vector* sat;
//Number of class hierarchies to be stored as satellite data
int sat_rows;
//Helper function
int AddNodePrivate (Node* nParent);
//Sort Helper
int* main_cells;
// Used for swapping
Node* tempnode;
// Private function used to sort based on comparison. More of a sort helper.
void SortPrivate(int (*compare) (int node1, int node2, void* map), void* map, int n, int start);
/* Partitioning for Sorting */
int Partition(int n, int start, int (*compare) (int node1, int node2, void* map), void* map);
/* Swapping two nodes by taking their node id's */
void Swap( int a, int b);
/* Swapping two satellite by taking their node id's */
void SatSwap( int a, int b);
/* Partitioning for Sorting */
int SatPartition(int n, int start);
/* Sorting satellite data */
void SatSort();
void SatSortPrivate(int n, int start);
};
#endif