83 #define MAX_SUBTREE_STR 32768 84 #define MAX_LEFT_LEAD_STR 256 108 char* left_lead,
char result[])
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,
"| ");
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,
" ");
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",
126 left_lead, Left_subtree_buff,
"\n",
128 left_lead, Right_subtree_buff);
131 strcpy(result,
"|_ {}");
165 assert(tree !=
NULL);
173 assert(tree !=
NULL);
181 assert(tree !=
NULL);
190 assert(tree !=
NULL);
191 tree->
left = subtree;
199 assert(tree !=
NULL);
200 tree->
right = subtree;
215 int mid = count >> 1;
219 *head = (*head)->
right;
251 tail = &(*tail)->
right;
262 if (cmp_status == 0)
return root;
265 root = (cmp_status > 0) ? root->
left : root->
right;
279 char valstr[],
char* indents,
char result[])
285 snprintf(newindents,
MAX_INDENTS+4,
"%s%s", indents,
"| ");
288 snprintf(newindents,
MAX_INDENTS+4,
"%s%s", indents,
" ");
291 tostr(root->
val, valstr);
292 snprintf(result,
MAX_TREE_STR,
"%s%s%s%s%s%s%s%s%s%s%s",
295 indents, Left_subtree_buff,
"\n",
297 indents, Right_subtree_buff);
300 strcpy(result,
"{}");
310 return (left_height > right_height)? left_height + 1: right_height + 1;
int binarytree_height(binarytree_t *root)
void *(* mem_alloc)(size_t size)
void * binarytree_rootval(binarytree_t *tree)
struct binarytree_s * right
int compare(SrcFile::ln x, SrcFile::ln y)
static int matches(char *ins, int len, const char *sig)
#define MAX_LEFT_LEAD_STR
binarytree_t * binarytree_list_to_tree(binarytree_t **head, int count)
binarytree_t * binarytree_find(binarytree_t *root, val_cmp matches, void *val)
static void subtree_tostr2(binarytree_t *subtree, val_tostr tostr, char valstr[], char *left_lead, char result[])
void(* val_tostr)(void *val, char str[])
struct binarytree_s binarytree_t
binarytree_t * binarytree_listify(binarytree_t *root)
void binarytree_listify_helper(binarytree_t *root, binarytree_t **tail)
int(* val_cmp)(void *lhs, void *rhs)
struct binarytree_s * left
void binarytree_set_rightsubtree(binarytree_t *tree, binarytree_t *subtree)
void binarytree_tostring_indent(binarytree_t *root, val_tostr tostr, char valstr[], char *indents, char result[])
binarytree_t * binarytree_leftsubtree(binarytree_t *tree)
void binarytree_set_leftsubtree(binarytree_t *tree, binarytree_t *subtree)
binarytree_t * binarytree_new(size_t size, mem_alloc m_alloc)
binarytree_t * binarytree_rightsubtree(binarytree_t *tree)
binarytree_t * binarytree_insert(binarytree_t *root, val_cmp compare, binarytree_t *key)
void(* mem_free)(void *ptr)
binarytree_t * binarytree_listalloc(size_t elt_size, int num_elts, mem_alloc m_alloc)
int binarytree_count(binarytree_t *tree)
void binarytree_tostring(binarytree_t *tree, val_tostr tostr, char valstr[], char result[])
void binarytree_del(binarytree_t **root, mem_free m_free)