HPCToolkit
binarytree.h
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 // Define 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 #ifndef __binarytree_h__
64 #define __binarytree_h__
65 
66 //******************************************************************************
67 // global include files
68 //******************************************************************************
69 
70 #include <stdbool.h>
71 
72 //******************************************************************************
73 // local include files
74 //******************************************************************************
75 
76 #include "generic_val.h"
77 #include "mem_manager.h"
78 
79 //******************************************************************************
80 // macros
81 //******************************************************************************
82 
83 #define MAX_TREE_STR 65536
84 #define MAX_INDENTS 512
85 #define OPAQUE_TYPE 0
86 
87 //******************************************************************************
88 // abstract data type
89 //******************************************************************************
90 
91 #if OPAQUE_TYPE
92 
93 // opaque type not supported by gcc 4.4.*
94 typedef struct binarytree_s binarytree_t;
95 
96 #else
97 
98 typedef struct binarytree_s {
99  struct binarytree_s *left;
101  char val[];
102 } binarytree_t;
103 
104 #endif
105 
106 // constructors
107 binarytree_t *
108 binarytree_new(size_t size, mem_alloc m_alloc);
109 
110 // destructor
111 void binarytree_del(binarytree_t **root, mem_free m_free);
112 
113 /*
114  * Gettors
115  */
116 // return the value at the root
117 // pre-condition: tree != NULL
118 void*
120 
121 // pre-condition: tree != NULL
124 
125 // pre-condition: tree != NULL
128 
129 /*
130  * Settors
131  */
132 void
135  binarytree_t* subtree);
136 
137 void
140  binarytree_t* subtree);
141 
142 // count the number of nodes in the binary tree.
143 int
145 
146 // given a tree that is a list, with all left children empty,
147 // restructure to make a balanced tree
148 binarytree_t *
149 binarytree_list_to_tree(binarytree_t ** head, int count);
150 
151 // restructure a binary tree so that all its left children are null
152 binarytree_t *
154 
155 // allocate a binary tree so that all its left children are null
156 binarytree_t *
157 binarytree_listalloc(size_t elt_size, int num_elts, mem_alloc m_alloc);
158 
159 // use binarytree_node_cmp to find a matching node in a binary search tree.
160 // NULL is returned
161 // if no match is found.
162 binarytree_t *
164 
165 // compute a string representing the binary tree printed vertically and
166 // return result in the treestr parameter.
167 // caller should provide the appropriate length for treestr.
168 void
170  val_tostr valtostr_fun, char valstr[], char treestr[]);
171 
172 void
174  char valstr[], char* indents, char treestr[]);
175 
176 // compute the height of the binary tree.
177 // the height of an empty tree is 0.
178 // the height of an non-empty tree is 1+ the larger of the height of the left subtree
179 // and the right subtree.
180 int
182 
183 binarytree_t *
185 
186 #endif
void *(* mem_alloc)(size_t size)
Definition: mem_manager.h:17
binarytree_t * binarytree_listalloc(size_t elt_size, int num_elts, mem_alloc m_alloc)
Definition: binarytree.c:245
void binarytree_del(binarytree_t **root, mem_free m_free)
Definition: binarytree.c:150
binarytree_t * binarytree_listify(binarytree_t *root)
Definition: binarytree.c:237
binarytree_t * binarytree_rightsubtree(binarytree_t *tree)
Definition: binarytree.c:179
cct_node_t * node
Definition: cct.c:128
char val[]
Definition: binarytree.h:101
struct binarytree_s * right
Definition: binarytree.h:100
int compare(SrcFile::ln x, SrcFile::ln y)
Definition: SrcFile.hpp:80
void binarytree_set_rightsubtree(binarytree_t *tree, binarytree_t *subtree)
Definition: binarytree.c:195
void * binarytree_rootval(binarytree_t *tree)
Definition: binarytree.c:163
void binarytree_tostring_indent(binarytree_t *tree, val_tostr tostr, char valstr[], char *indents, char treestr[])
Definition: binarytree.c:278
binarytree_t * binarytree_leftsubtree(binarytree_t *tree)
Definition: binarytree.c:171
binarytree_t * binarytree_insert(binarytree_t *tree, val_cmp compare, binarytree_t *key)
Definition: binarytree.c:316
void binarytree_tostring(binarytree_t *root, val_tostr valtostr_fun, char valstr[], char treestr[])
Definition: binarytree.c:271
void(* val_tostr)(void *val, char str[])
Definition: generic_val.h:22
struct binarytree_s binarytree_t
int binarytree_count(binarytree_t *node)
Definition: binarytree.c:204
binarytree_t * binarytree_find(binarytree_t *tree, val_cmp fn, void *val)
Definition: binarytree.c:257
int(* val_cmp)(void *lhs, void *rhs)
Definition: generic_val.h:32
struct binarytree_s * left
Definition: binarytree.h:99
void binarytree_set_leftsubtree(binarytree_t *tree, binarytree_t *subtree)
Definition: binarytree.c:186
int binarytree_height(binarytree_t *tree)
Definition: binarytree.c:305
binarytree_t * binarytree_list_to_tree(binarytree_t **head, int count)
Definition: binarytree.c:211
binarytree_t * binarytree_new(size_t size, mem_alloc m_alloc)
Definition: binarytree.c:141
void(* mem_free)(void *ptr)
Definition: mem_manager.h:18
bitree_uwi_t * tree