35 #define BITREE_PRINT_LEVEL_PADDING 4
47 int (*compare)(
const void *key1,
const void *key2);
48 void (*destroy)(
void *data);
55 static int treeheight(
BiTreeNode node,
int depth);
56 static void print_tree(
BiTreeNode node,
int level,
void (*callback)(
const void *data));
57 static void preorder(
BiTreeNode node,
void (*callback)(
const void *data));
58 static void inorder(
BiTreeNode node,
void (*callback)(
const void *data));
59 static void postorder(
BiTreeNode node,
void (*callback)(
const void *data));
70 tree->destroy = destroy;
87 tree->compare = compare;
101 position = &tree->root;
109 position = &node->left;
117 new_node->data = (
void *)data;
118 new_node->left = NULL;
119 new_node->right = NULL;
120 *position = new_node;
139 position = &tree->root;
147 position = &node->right;
155 new_node->data = (
void *)data;
156 new_node->left = NULL;
157 new_node->right = NULL;
158 *position = new_node;
169 int direction, cmpval;
180 cmpval = tree->compare(data,
BITREEdata(node));
221 cmpval = tree->compare(*data,
BITREEdata(node));
245 tree->root = remnode(tree, tree->root, data);
260 position = &tree->root;
262 position = &node->left;
265 if (*position != NULL)
270 if (tree->destroy != NULL)
273 tree->destroy((*position)->data);
294 position = &tree->root;
296 position = &node->right;
299 if (*position != NULL)
304 if (tree->destroy != NULL)
307 tree->destroy((*position)->data);
324 if ((merge_tree =
BITREEinit(left_tree->destroy)) == NULL)
343 left_tree->root = NULL;
345 right_tree->root = NULL;
346 right_tree->size = 0;
368 return node->left == NULL && node->right == NULL;
400 preorder(tree->root, callback);
405 inorder(tree->root, callback);
410 postorder(tree->root, callback);
425 cmpval = tree->compare(*data, node->data);
428 node->left = remnode(tree, node->left, data);
430 node->right = remnode(tree, node->right, data);
431 else if(node->left && node->right)
438 datatmp = node->data;
439 node->data = ptmp->data;
440 ptmp->data = datatmp;
443 node->right = remnode(tree, node->right, data);
451 if (node->left == NULL)
453 else if (node->right == NULL)
467 static void print_tree(
BiTreeNode node,
int level,
void (*callback)(
const void *data))
490 print_tree(
BITREEleft(node), level+1, callback);
495 static void preorder(
BiTreeNode node,
void (*callback)(
const void *data))
500 callback(node->data);
502 preorder(node->left, callback);
504 preorder(node->right, callback);
509 static void inorder(
BiTreeNode node,
void (*callback)(
const void *data))
514 inorder(node->left, callback);
516 callback(node->data);
518 inorder(node->right, callback);
523 static void postorder(
BiTreeNode node,
void (*callback)(
const void *data))
528 postorder(node->left, callback);
530 postorder(node->right, callback);
532 callback(node->data);
537 static int treeheight(
BiTreeNode node,
int depth)
542 return maxval(treeheight(node->left, depth+1),
543 treeheight(node->right, depth+1));