summaryrefslogtreecommitdiff
path: root/src/utils/iltree.c
blob: 28e8d9098d3ed3afb9ce32d1fa3a38ed0bac7894 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
 *  This program is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  (c) Vincenzo Nicosia 2009-2017 -- <v.nicosia@qmul.ac.uk>
 * 
 *  This file is part of NetBunch, a package for complex network
 *  analysis and modelling. For more information please visit:
 *
 *             http://www.complex-networks.net/
 *
 *  If you use this software, please add a reference to 
 *
 *               V. Latora, V. Nicosia, G. Russo             
 *   "Complex Networks: Principles, Methods and Applications"
 *              Cambridge University Press (2017) 
 *                ISBN: 9781107103184
 *
 ***********************************************************************
 *
 *  This is an implementation of a simple insert-lookup Binary Search
 *  Tree.  It supports adding nodes, checking for the presence of
 *  keys, visiting the BST in pre-order, getting the maximum/minumum
 *  key, and applying a function to all the nodes. There is no support
 *  for deleting nodes.
 *
 */


/*
 *
 * A simple insert-lookup static binary tree datatype
 *
 */

#include <stdlib.h>
#include "iltree.h"
#include <stdio.h>


void __recursive_preorder(node_t *cur, ilfunc_t *funs){
  
  if(cur->left){
    __recursive_preorder(cur->left, funs);
  }
  funs->print(cur->info, funs->fileout);
  if(cur->right){
    __recursive_preorder(cur->right, funs);
  }
}

/*
 *
 * Recursive push of nodes in the nodecache :-)
 *
 */

void __recursive_destroy(node_t *cur, ilfunc_t *funs){
  if(cur->left){
    __recursive_destroy(cur->left, funs);
    free(cur->left);
    cur->left = NULL;
  }
  if(cur->right){
    __recursive_destroy(cur->right, funs);
    free(cur->right);
    cur->right = NULL;
  }
  funs->dealloc(cur->info);
}


int __recursive_insert(node_t *cur, node_t *elem, ilfunc_t *f){
  
  int res ;
  res = f->compare(cur->info, elem->info);
  /*  printf("res: %d\n", res); */
  if ( res > 0){
    if (cur->left){
      return __recursive_insert(cur->left, elem, f);
    }
    else{
      cur->left = elem;
      return 0;
    }
  }
  else if (res < 0){
    if (cur->right){
      return __recursive_insert(cur->right, elem, f);
    }
    else{
      cur->right = elem;
      return 0;
    }
  }
  printf("warning!!!!! duplicate entry!!!!!!\n\n");
  return -1;
}



void* __recursive_lookup(node_t *cur, void *v, ilfunc_t *f){
  
  int res;

  res = f->compare(cur->info, v);

  if (res > 0){
    if(cur->left)
      return __recursive_lookup(cur->left, v, f);
    else
      return NULL;
    
  }
  else if (res < 0){
    if(cur->right)
      return __recursive_lookup(cur->right, v, f);
    else
      return NULL;
  }
  else
    return cur->info;
}

void __recursive_map(node_t *cur, void (*func)(void*)){
  
  if (cur->left)
    __recursive_map(cur->left, func);
  func(cur->info);
  if (cur->right)
    __recursive_map(cur->right, func);
}

void __recursive_map_args(node_t *cur, void (*func)(void*, void*), void *args){
  
  if (cur->left)
    __recursive_map_args(cur->left, func, args);
  func(cur->info, args);
  if (cur->right)
    __recursive_map_args(cur->right, func, args);
}



iltree_t iltree_create(iltree_t t){
  if (!t) {
    t = (iltree_t)malloc(sizeof(iltree_struct_t));
  }
  t->root = NULL;
  return t;
}


void iltree_set_funs(iltree_t t, ilfunc_t *funs){
  
  t->funs = *funs;
}


void iltree_insert(iltree_t t, void *elem){
  
  node_t *n;
  
  n = (node_t*)malloc(sizeof(node_t));
  n->info = t->funs.alloc(); 
  t->funs.copy(elem, n->info);
  n->left = n->right = NULL;
  if (t->root == NULL){
    t->root = n;
  }
  else{
    __recursive_insert(t->root, n, & (t->funs));
  }
}


void iltree_destroy(iltree_t t){
  
  if(t->root)
    __recursive_destroy(t->root, & (t->funs));
  free(t->root);
  free(t);
}




void iltree_view_pre(iltree_t t){
  
  if (t->root){
    /*printf("----\n");*/
    __recursive_preorder(t->root, & (t->funs));
    /*printf("----\n");*/
  }
  else
    printf("----- Empty tree!!!! -----\n");
  
}



void* iltree_lookup(iltree_t t , void *elem){

  if(t->root)
    return __recursive_lookup(t->root, elem, & (t->funs) );
  else 
    return NULL;
}


void iltree_map(iltree_t t, void (*func)(void*)){
  
  __recursive_map(t->root, func);
  
}


void iltree_map_args(iltree_t t, void (*func)(void*, void*), void *args){
  
  __recursive_map_args(t->root, func, args);
  
}

void* iltree_get_fileout(iltree_t t){

  return t->funs.fileout;
}

void iltree_set_fileout(iltree_t t, void *f){
  
  t->funs.fileout = f;
}

void* __recursive_getmin(node_t *cur){
  
  if(cur->left){
    return __recursive_getmin(cur->left);
  }
  else{
    return cur->info;
  }

}


void* iltree_getmin(iltree_t t){
  
  if (!t){
    return NULL;
  }
  else{
    return __recursive_getmin(t->root);
  }

}


void* __recursive_getmax(node_t *cur){
  
  if(cur->right){
    return __recursive_getmax(cur->right);
  }
  else{
    return cur->info;
  }

}


void* iltree_getmax(iltree_t t){
  
  if (!t){
    return NULL;
  }
  else{
    return __recursive_getmax(t->root);
  }

}