HPCToolkit
binarytree.c
Go to the documentation of this file.
1 // * BeginRiceCopyright *****************************************************
2 //
3 // $HeadURL$
4 // $Id$
5 //
6 // --------------------------------------------------------------------------
7 // Part of HPCToolkit (hpctoolkit.org)
8 //
9 // Information about sources of support for research and development of
10 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
11 // --------------------------------------------------------------------------
12 //
13 // Copyright ((c)) 2002-2019, Rice University
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions are
18 // met:
19 //
20 // * Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 //
23 // * Redistributions in binary form must reproduce the above copyright
24 // notice, this list of conditions and the following disclaimer in the
25 // documentation and/or other materials provided with the distribution.
26 //
27 // * Neither the name of Rice University (RICE) nor the names of its
28 // contributors may be used to endorse or promote products derived from
29 // this software without specific prior written permission.
30 //
31 // This software is provided by RICE and contributors "as is" and any
32 // express or implied warranties, including, but not limited to, the
33 // implied warranties of merchantability and fitness for a particular
34 // purpose are disclaimed. In no event shall RICE or contributors be
35 // liable for any direct, indirect, incidental, special, exemplary, or
36 // consequential damages (including, but not limited to, procurement of
37 // substitute goods or services; loss of use, data, or profits; or
38 // business interruption) however caused and on any theory of liability,
39 // whether in contract, strict liability, or tort (including negligence
40 // or otherwise) arising in any way out of the use of this software, even
41 // if advised of the possibility of such damage.
42 //
43 // ******************************************************* EndRiceCopyright *
44 
45 //******************************************************************************
46 //
47 // File:
48 // $HeadURL$
49 //
50 // Purpose:
51 // Implement an API for binary trees that supports
52 // (1) building a balanced binary tree from an arbitrary imbalanced tree
53 // (e.g., a list), and
54 // (2) searching a binary tree for a matching node
55 //
56 // The binarytree_node_t data type is meant to be used as a prefix to
57 // other structures so that this code can be used to manipulate arbitrary
58 // tree structures. Macros support the (unsafe) up and down casting needed
59 // to use the API on derived structures.
60 //
61 //******************************************************************************
62 
63 //******************************************************************************
64 // global include files
65 //******************************************************************************
66 
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <assert.h>
71 
72 //******************************************************************************
73 // local include files
74 //******************************************************************************
75 
76 #include "binarytree.h"
77 
78 
79 //******************************************************************************
80 // macros
81 //******************************************************************************
82 
83 #define MAX_SUBTREE_STR 32768
84 #define MAX_LEFT_LEAD_STR 256
85 
86 //******************************************************************************
87 // implementation type
88 //******************************************************************************
89 
90 #if OPAQUE_TYPE
91 
92 // opaque type not supported by gcc 4.4.*
93 typedef struct binarytree_s {
94  struct binarytree_s *left;
95  struct binarytree_s *right;
96  void* val;
97 } binarytree_t;
98 
99 #endif
100 
101 
102 //******************************************************************************
103 // private operations
104 //******************************************************************************
105 
106 static void
107 subtree_tostr2(binarytree_t *subtree, val_tostr tostr, char valstr[],
108  char* left_lead, char result[])
109 {
110  if (subtree) {
111  char Left_subtree_buff[MAX_SUBTREE_STR + 1];
112  char Right_subtree_buff[MAX_SUBTREE_STR + 1];
113  char Left_lead_buff[MAX_LEFT_LEAD_STR + 1];
114 
115  size_t new_left_lead_size = strlen(left_lead) + strlen("| ") + 1;
116  snprintf(Left_lead_buff, new_left_lead_size, "%s%s", left_lead, "| ");
117  binarytree_t * left = {subtree->left};
118  subtree_tostr2(left, tostr, valstr, Left_lead_buff, Left_subtree_buff);
119  snprintf(Left_lead_buff, new_left_lead_size, "%s%s", left_lead, " ");
120  binarytree_t * right = {subtree->right};
121  subtree_tostr2(right, tostr, valstr, Left_lead_buff, Right_subtree_buff);
122  tostr(subtree->val, valstr);
123  snprintf(result, MAX_TREE_STR, "%s%s%s%s%s%s%s%s%s%s%s%s",
124  "|_ ", valstr, "\n",
125  left_lead, "|\n",
126  left_lead, Left_subtree_buff, "\n",
127  left_lead, "|\n",
128  left_lead, Right_subtree_buff);
129  }
130  else {
131  strcpy(result, "|_ {}");
132  }
133 }
134 
135 //******************************************************************************
136 // interface operations
137 //******************************************************************************
138 
139 
140 binarytree_t *
141 binarytree_new(size_t size, mem_alloc m_alloc)
142 {
143  binarytree_t *node = (binarytree_t *)m_alloc(sizeof(binarytree_t) + size);
144  node->right = NULL;
145  node->left = NULL;
146  return node;
147 }
148 
149 // destructor
151 {
152  if (*root) {
153  binarytree_del(&((*root)->left), m_free);
154  binarytree_del(&((*root)->right), m_free);
155  m_free(*root);
156  *root = NULL;
157  }
158 }
159 
160 // return the value at the root
161 // pre-condition: tree != NULL
162 void*
164 {
165  assert(tree != NULL);
166  return tree->val;
167 }
168 
169 // pre-condition: tree != NULL
172 {
173  assert(tree != NULL);
174  return tree->left;
175 }
176 
177 // pre-condition: tree != NULL
180 {
181  assert(tree != NULL);
182  return tree->right;
183 }
184 
185 void
188  binarytree_t* subtree)
189 {
190  assert(tree != NULL);
191  tree->left = subtree;
192 }
193 
194 void
197  binarytree_t* subtree)
198 {
199  assert(tree != NULL);
200  tree->right = subtree;
201 }
202 
203 int
205 {
206  return tree?
207  binarytree_count(tree->left) + binarytree_count(tree->right) + 1: 0;
208 }
209 
210 binarytree_t *
212 {
213  if (count == 0)
214  return NULL;
215  int mid = count >> 1;
217  binarytree_t *root = *head;
218  root->left = left;
219  *head = (*head)->right;
220  root->right = binarytree_list_to_tree(head, count - mid - 1);
221  return root;
222 }
223 
224 void
226 {
227  if (root != NULL) {
228  binarytree_listify_helper(root->left, tail);
229  root->left = NULL;
230  *tail = root;
231  tail = &root->right;
232  binarytree_listify_helper(root->right, tail);
233  }
234 }
235 
236 binarytree_t *
238 {
239  binarytree_t *head;
240  binarytree_listify_helper(root, &head);
241  return head;
242 }
243 
244 binarytree_t *
245 binarytree_listalloc(size_t elt_size, int num_elts, mem_alloc m_alloc)
246 {
247  binarytree_t *head;
248  binarytree_t **tail = &head;
249  while (num_elts--) {
250  *tail = binarytree_new(elt_size, m_alloc);
251  tail = &(*tail)->right;
252  }
253  return head;
254 }
255 
256 binarytree_t *
258 {
259  while (root) {
260  int cmp_status = matches(root->val, val);
261 
262  if (cmp_status == 0) return root; // subtree_root matches val
263 
264  // determine which subtree to search
265  root = (cmp_status > 0) ? root->left : root->right;
266  }
267  return NULL;
268 }
269 
270 void
272  char result[])
273 {
274  binarytree_tostring_indent(tree, tostr, valstr, "", result);
275 }
276 
277 void
279  char valstr[], char* indents, char result[])
280 {
281  if (root) {
282  char Left_subtree_buff[MAX_SUBTREE_STR + 1];
283  char Right_subtree_buff[MAX_SUBTREE_STR + 1];
284  char newindents[MAX_INDENTS+5];
285  snprintf(newindents, MAX_INDENTS+4, "%s%s", indents, "| ");
286  subtree_tostr2(root->left, tostr, valstr, newindents, result);
287  snprintf(Left_subtree_buff, MAX_SUBTREE_STR, "%s", result);
288  snprintf(newindents, MAX_INDENTS+4, "%s%s", indents, " ");
289  subtree_tostr2(root->right, tostr, valstr, newindents, result);
290  snprintf(Right_subtree_buff, MAX_SUBTREE_STR, "%s", result);
291  tostr(root->val, valstr);
292  snprintf(result, MAX_TREE_STR, "%s%s%s%s%s%s%s%s%s%s%s",
293  valstr, "\n",
294  indents,"|\n",
295  indents, Left_subtree_buff, "\n",
296  indents, "|\n",
297  indents, Right_subtree_buff);
298  }
299  else {
300  strcpy(result, "{}");
301  }
302 }
303 
304 int
306 {
307  if (root) {
308  int left_height = binarytree_height(root->left);
309  int right_height = binarytree_height(root->right);
310  return (left_height > right_height)? left_height + 1: right_height + 1;
311  }
312  return 0;
313 }
314 
315 binarytree_t *
317 {
318  if(!root) {
319  return key; // empty tree case
320  }
321  else if (compare(root->val, key->val) > 0) {
322  root->left = binarytree_insert(root->left, compare, key);
323  }
324  else if (compare(root->val, key->val) < 0) {
325  root->right = binarytree_insert(root->right, compare, key);
326  }
327  return root;
328 }
int binarytree_height(binarytree_t *root)
Definition: binarytree.c:305
void *(* mem_alloc)(size_t size)
Definition: mem_manager.h:17
#define MAX_TREE_STR
Definition: binarytree.h:83
cct_node_t * node
Definition: cct.c:128
char val[]
Definition: binarytree.h:101
void * binarytree_rootval(binarytree_t *tree)
Definition: binarytree.c:163
struct binarytree_s * right
Definition: binarytree.h:100
int compare(SrcFile::ln x, SrcFile::ln y)
Definition: SrcFile.hpp:80
static int matches(char *ins, int len, const char *sig)
#define MAX_LEFT_LEAD_STR
Definition: binarytree.c:84
binarytree_t * binarytree_list_to_tree(binarytree_t **head, int count)
Definition: binarytree.c:211
binarytree_t * binarytree_find(binarytree_t *root, val_cmp matches, void *val)
Definition: binarytree.c:257
static void subtree_tostr2(binarytree_t *subtree, val_tostr tostr, char valstr[], char *left_lead, char result[])
Definition: binarytree.c:107
void(* val_tostr)(void *val, char str[])
Definition: generic_val.h:22
struct binarytree_s binarytree_t
binarytree_t * binarytree_listify(binarytree_t *root)
Definition: binarytree.c:237
void binarytree_listify_helper(binarytree_t *root, binarytree_t **tail)
Definition: binarytree.c:225
int(* val_cmp)(void *lhs, void *rhs)
Definition: generic_val.h:32
struct binarytree_s * left
Definition: binarytree.h:99
#define NULL
Definition: ElfHelper.cpp:85
void binarytree_set_rightsubtree(binarytree_t *tree, binarytree_t *subtree)
Definition: binarytree.c:195
void binarytree_tostring_indent(binarytree_t *root, val_tostr tostr, char valstr[], char *indents, char result[])
Definition: binarytree.c:278
binarytree_t * binarytree_leftsubtree(binarytree_t *tree)
Definition: binarytree.c:171
void binarytree_set_leftsubtree(binarytree_t *tree, binarytree_t *subtree)
Definition: binarytree.c:186
#define MAX_INDENTS
Definition: binarytree.h:84
binarytree_t * binarytree_new(size_t size, mem_alloc m_alloc)
Definition: binarytree.c:141
binarytree_t * binarytree_rightsubtree(binarytree_t *tree)
Definition: binarytree.c:179
binarytree_t * binarytree_insert(binarytree_t *root, val_cmp compare, binarytree_t *key)
Definition: binarytree.c:316
#define MAX_SUBTREE_STR
Definition: binarytree.c:83
void(* mem_free)(void *ptr)
Definition: mem_manager.h:18
binarytree_t * binarytree_listalloc(size_t elt_size, int num_elts, mem_alloc m_alloc)
Definition: binarytree.c:245
int binarytree_count(binarytree_t *tree)
Definition: binarytree.c:204
void binarytree_tostring(binarytree_t *tree, val_tostr tostr, char valstr[], char result[])
Definition: binarytree.c:271
bitree_uwi_t * tree
void binarytree_del(binarytree_t **root, mem_free m_free)
Definition: binarytree.c:150