1 /* init.c */
2
3 #include "../Tree.h"
4
5 /*--------------------------------------------------------------------*/
6 /*
7 -----------------------
8 simplest constructor
9
10 created -- 95nov15, cca
11 -----------------------
12 */
13 void
Tree_init1(Tree * tree,int size)14 Tree_init1 (
15 Tree *tree,
16 int size
17 ) {
18 /*
19 ---------------
20 check the input
21 ---------------
22 */
23 if ( tree == NULL || size < 0 ) {
24 fprintf(stderr, "\n fatal error in Tree_init1(%p,%d)"
25 "\n bad input\n", tree, size) ;
26 exit(-1) ;
27 }
28 /*
29 -----------------------
30 clear any previous data
31 -----------------------
32 */
33 Tree_clearData(tree) ;
34 /*
35 -----------------------------------------------
36 set size field and initialize the three vectors
37 -----------------------------------------------
38 */
39 tree->n = size ;
40 if ( size > 0 ) {
41 tree->par = IVinit(size, -1) ;
42 tree->fch = IVinit(size, -1) ;
43 tree->sib = IVinit(size, -1) ;
44 }
45
46 return ; }
47
48 /*--------------------------------------------------------------------*/
49 /*
50 --------------------------------
51 initialize given a parent vector
52
53 created -- 95nov15, cca
54 --------------------------------
55 */
56 void
Tree_init2(Tree * tree,int size,int par[])57 Tree_init2 (
58 Tree *tree,
59 int size,
60 int par[]
61 ) {
62 /*
63 ---------------
64 check the input
65 ---------------
66 */
67 if ( tree == NULL || size <= 0 || par == NULL ) {
68 fprintf(stderr, "\n fatal error in Tree_init2(%p,%d,%p)"
69 "\n bad input\n", tree, size, par) ;
70 exit(-1) ;
71 }
72 /*
73 ----------------------------
74 first use simple initializer
75 ----------------------------
76 */
77 Tree_init1(tree, size) ;
78 /*
79 ------------------
80 copy parent vector
81 ------------------
82 */
83 IVcopy(size, tree->par, par) ;
84 /*
85 -------------------------
86 set fch[], sib[] and root
87 -------------------------
88 */
89 Tree_setFchSibRoot(tree) ;
90
91 return ; }
92
93 /*--------------------------------------------------------------------*/
94 /*
95 ---------------------------------
96 initialize given the tree vectors
97
98 created -- 95nov15, cca
99 ---------------------------------
100 */
101 void
Tree_init3(Tree * tree,int size,int par[],int fch[],int sib[])102 Tree_init3 (
103 Tree *tree,
104 int size,
105 int par[],
106 int fch[],
107 int sib[]
108 ) {
109 /*
110 ---------------
111 check the input
112 ---------------
113 */
114 if ( tree == NULL || size <= 0
115 || par == NULL || fch == NULL || sib == NULL ) {
116 fprintf(stderr, "\n fatal error in Tree_init3(%p,%d,%p,%p,%p)"
117 "\n bad input\n", tree, size, par, fch, sib) ;
118 exit(-1) ;
119 }
120 /*
121 ----------------------------
122 first use simple initializer
123 ----------------------------
124 */
125 Tree_init1(tree, size) ;
126 /*
127 ----------------------
128 copy the three vectors
129 ----------------------
130 */
131 IVcopy(size, tree->par, par) ;
132 IVcopy(size, tree->fch, fch) ;
133 IVcopy(size, tree->sib, sib) ;
134 /*
135 --------
136 set root
137 --------
138 */
139 Tree_setRoot(tree) ;
140
141 return ; }
142
143 /*--------------------------------------------------------------------*/
144 /*
145 ------------------------------------
146 set the fch[], sib[] and root fields
147
148 created -- 95nov15, cca
149 ------------------------------------
150 */
151 void
Tree_setFchSibRoot(Tree * tree)152 Tree_setFchSibRoot (
153 Tree *tree
154 ) {
155 int n, root, u, v ;
156 int *fch, *par, *sib ;
157 /*
158 ---------------
159 check the input
160 ---------------
161 if ( tree == NULL || (n = tree->n) < 1 ) {
162 */
163 if ( tree == NULL ) {
164 fprintf(stderr, "\n fatal error in Tree_setFchSibRoot(%p)"
165 "\n bad input\n", tree) ;
166 exit(-1) ;
167 }
168 if ( (n = tree->n) < 1 ) {
169 return ;
170 }
171 par = tree->par ;
172 fch = tree->fch ;
173 sib = tree->sib ;
174 /*
175 ---------------------
176 initialize the fields
177 ---------------------
178 */
179 IVfill(n, tree->fch, -1) ;
180 IVfill(n, tree->sib, -1) ;
181 root = -1 ;
182 /*
183 --------------
184 set the fields
185 --------------
186 for ( u = 0 ; u < n ; u++ ) {
187 */
188 for ( u = n - 1 ; u >= 0 ; u-- ) {
189 if ( (v = par[u]) != -1 ) {
190 sib[u] = fch[v] ;
191 fch[v] = u ;
192 } else {
193 sib[u] = root ;
194 root = u ;
195 }
196 }
197 tree->root = root ;
198
199 return ; }
200
201 /*--------------------------------------------------------------------*/
202 /*
203 -----------------------
204 set the root field
205
206 created -- 95nov15, cca
207 -----------------------
208 */
209 void
Tree_setRoot(Tree * tree)210 Tree_setRoot (
211 Tree *tree
212 ) {
213 int n, root, u ;
214 int *par, *sib ;
215 /*
216 ---------------
217 check the input
218 ---------------
219 */
220 if ( tree == NULL || (n = tree->n) < 1 ) {
221 fprintf(stderr, "\n fatal error in Tree_setRoot(%p)"
222 "\n bad input\n", tree) ;
223 exit(-1) ;
224 }
225 n = tree->n ;
226 par = tree->par ;
227 sib = tree->sib ;
228 root = -1 ;
229 /*
230 --------------
231 set the fields
232 --------------
233 */
234 for ( u = 0 ; u < n ; u++ ) {
235 if ( par[u] == -1 ) {
236 sib[u] = root ;
237 root = u ;
238 }
239 }
240 tree->root = root ;
241
242 return ; }
243
244 /*--------------------------------------------------------------------*/
245