/* Merge two binary trees */ int bitree_merge (BiTree * merge, BiTree * left, BiTree * rigth, const void * data) { BiTreeNode * new_node; if (merge == NULL) return -1; bitree_init(merge, left -> destroy); if ((new_node = (BiTreeNode *) malloc (sizeof(BiTreeNode))) != NULL) { printf("Can't create a new node.\n"); return -1; } new_node -> data = (void *) data; new_node -> left = left -> root; new_node -> right= rigth -> root; merge -> size = bitree_size(left) + bitree_size(right) + 1; merge -> root = new_node; left -> root = NULL; left -> data = 0; right -> root = NULL; right -> data = 0; }
int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data) { /* initialize the merged tree */ bitree_init(merge, left->destroy); /* insert the data for the root node of the merged tree */ if (bitree_insert_left(merge, NULL, data) != 0) { bitree_destroy(merge); return -1; } /* merge the two binary trees into a single binary tree */ bitree_root(merge)->left = bitree_root(left); bitree_root(merge)->right = bitree_root(right); /* adjust the size of the new binary tree */ merge->size = merge->size + bitree_size(left) + bitree_size(right); /* do not let the original trees access the merged nodes */ left->root = NULL; left->size = 0; right->root = NULL; right->size = 0; return 0; }
int bitree_ins_left(BiTree * tree, BiTreeNode * node, const void *data) { BiTreeNode *new_node, **position; /***************************************************************************** * Determine where to insert the node. * *****************************************************************************/ if (node == NULL) { /************************************************************************** * Allow insertion at the root only in an empty tree. * **************************************************************************/ if (bitree_size(tree) > 0) return -1; position = &tree->root; } else { /************************************************************************** * Normally allow insertion only at the end of a branch. * **************************************************************************/ if (bitree_left(node) != NULL) return -1; position = &node->left; } /***************************************************************************** * Allocate storage for the node. * *****************************************************************************/ if ((new_node = (BiTreeNode *) malloc(sizeof(BiTreeNode))) == NULL) return -1; /***************************************************************************** * Insert the node into the tree. * *****************************************************************************/ new_node->data = (void *)data; new_node->left = NULL; new_node->right = NULL; *position = new_node; /***************************************************************************** * Adjust the size of the tree to account for the inserted node. * *****************************************************************************/ tree->size++; return 0; }
void free_node(BI_S_TREE *b, struct bitree_node *node) { memset(node, 0, sizeof(struct bitree_node)); free(node); bitree_size(b)--; return; }
int bi_search_tree_insert(BI_S_TREE *b, void *data) { if (bitree_size(b) == 0) return insert_left(b, NULL, data); return search_insert(b, b->root, data); }
/* Remove the left tree under the node from the tree */ void bitree_rem_left(BiTree * tree, BiTreeNode * node) { BiTreeNode ** position; // For the situation of tree is empty if (bitree_size(tree) == 0) return; // Determine where to remove nodes if (node == NULL) position = & tree -> root; else position = & node -> left; if (position != NULL) { bitree_rem_left(tree, *position); bitree_rem_right(tree, * position); if (tree -> destroy != NULL) { tree -> destroy ((* position) -> data); } free( *position); *position == NULL; tree -> size--; } return; }
/* Insert a node to the right child of the tree */ void bitree_ins_right (BiTree * tree, BiTreeNode * node, const void * data) { BiTreeNode * new_node, ** position; // Allowed to add a empty tree if (node == NULL) { if (bitree_size(tree) > 1) { printf ("The tree node is empty.\n"); return -1; } positino = & tree -> root; } else { if (bit_tree_right(node) != NULL) { printf ("The node already have a right child.\n"); return -1; } position = & tree -> right; } // Create a new node if ((new_node = (BiTreeNode *) malloc (sizeof(BiTreeNode))) == NULL) { printf ("Can't create the new node.\n"); return -1; } new_node -> left = NULL; new_node -> rigth= NULL; new_node -> data = (void *) data; *position = new_node; // Adjust the size of the tree for the inserted node tree -> size++; }
/* Insert a node to be the left child of the tree */ void bitree_ins_left(BiTree * tree, BiTreeNode * node, const void *data) { BitTreeNode * new_node, ** position; // Allowed to add a empty tree if (node == NULL) { if (bitree_size(tree) > 1) { printf ("The tree node is empty.\n"); return -1; } positon = & tree -> root; } else { if (bitree_left(node) != NULL) { printf ("This node already have a left child.\n"); return -1; } position = & node -> left; } if ((new_node = (BiTreeNode *) malloc (sizeof(BiTreeNode))) == NULL) { printf ("Can't create a new node.\n"); return -1; } new_node -> left = NULL; new_node -> right= NULL; new_node -> data = (void *) data; *postion == new_node; tree -> size ++; }
void bitree_remove_right(BiTree *tree, BiTreeNode *node) { BiTreeNode **position; /* do not allow removal from an empty tree */ if (bitree_size(tree) == 0) return; /* determine where to remove nodes */ if (node == NULL) position = &tree->root; else position = &node->right; /* remove the nodes - recursive */ if (*position != NULL) { bitree_remove_left(tree, *position); bitree_remove_right(tree, *position); if (tree->destroy != NULL) { /* call a user defined function to free dynamically allocated data */ tree->destroy((*position)->data); } free(*position); *position = NULL; /* adjust the size of the tree to account for the remoced node */ --tree->size; } }
int main(){ setup(); puts("Add 0-item as BT1's root, BT1: "); bitree_add_left(bt1, NULL, u[0]); bitree_dump(bt1, print); puts(""); puts("Add 1-item as BT2's root, BT2: "); bitree_add_left(bt2, NULL, u[1]); bitree_dump(bt2, print); puts(""); puts("Add 2-item as BT1's root's left child "); bitree_add_left(bt1, bitree_root(bt1), u[2]); puts("Add 3-item as BT1's root's right child "); bitree_add_right(bt1, bitree_root(bt1), u[3]); puts("BT1: "); bitree_dump(bt1, print); puts(""); puts("Add 4-item as BT2's root's left child "); bitree_add_left(bt2, bitree_root(bt2), u[4]); puts("Add 5-item as BT2's root's right child "); bitree_add_right(bt2, bitree_root(bt2), u[5]); puts("BT2: "); bitree_dump(bt2, print); puts(""); printf("BT1 len: %d\n", bitree_size(bt1)); printf("BT2 len: %d\n", bitree_size(bt2)); printf("BT len: %d\n", bitree_size(bt)); puts("Merge BT1 and BT2 to BT: "); bitree_merge(bt, bt1, bt2, u[6]); bitree_dump(bt, print); puts(""); printf("BT len: %d\n", bitree_size(bt)); puts("BT remove left child, BT: "); bitree_remove_left(bt, bitree_root(bt)); bitree_dump(bt, print); puts(""); enddown(); return 0; }
void bitree_rem_right(BiTree * tree, BiTreeNode * node) { BiTreeNode **position; /***************************************************************************** * Do not allow removal from an empty tree. * *****************************************************************************/ if (bitree_size(tree) == 0) return; /***************************************************************************** * Determine where to remove nodes. * *****************************************************************************/ if (node == NULL) position = &tree->root; else position = &node->right; /***************************************************************************** * Remove the nodes. * *****************************************************************************/ if (*position != NULL) { bitree_rem_left(tree, *position); bitree_rem_right(tree, *position); if (tree->destroy != NULL) { /*********************************************************************** * Call a user-defined function to free dynamically allocated data. * ***********************************************************************/ tree->destroy((*position)->data); } free(*position); *position = NULL; /************************************************************************** * Adjust the size of the tree to account for the removed node. * **************************************************************************/ tree->size--; } return; }
int avltree_insert(AVLTREE *b, void *data) { struct bitree_node *node; if (bitree_size(b) == 0) { node = bitree_insert_left(b, NULL, data); return (node == NULL) ? -1 : 0; } node = search_insert(b, b->root, data); update_height_balance(b, node); return 0; }
void bitree_remove_right(Bitree *tree, BiTreeNode *node) { BiTreeNode **position; if (bitree_size(tree) == 0) return; if (node == NULL) position = &tree->root; else position = &node->right; if (*position != NULL) { bitree_remove_right(tree, *position); bitree_remove_left(tree, *position); if (tree->destory != NULL) tree->destory((*position)->data); free(*position); *position = NULL; tree->size--; } return; }
static void destroy_right (BisTree *tree, BiTreeNode *node) { BiTreeNode **position; /* Do not allow destruction of an empty tree. */ if (bitree_size (tree) == 0) return; /* Determine where to destroy nodes. */ if (node == NULL) position = &tree->root; else position = &node->right; /* Destroy the nodes. */ if (*position != NULL) { destroy_left (tree, *position); destroy_right (tree, *position); if (tree->destroy != NULL) { /* Call a user-defined function to free dynamically allocated data. */ tree->destroy (((AvlNode *) (*position)->data)->data); } /* Free the AVL data in the node, then free the node itself. */ free ((*position)->data); free (*position); *position = NULL; /* Adjust the size of the tree to account for the destroyed node. */ tree->size--; } return; }
int main(int argc, char **argv) { BiTree tree; BiTreeNode *node; int i; /***************************************************************************** * * * Initialize the binary tree. * * * *****************************************************************************/ bitree_init(&tree, free); /***************************************************************************** * * * Perform some binary tree operations. * * * *****************************************************************************/ fprintf(stdout, "Inserting some nodes\n"); if (insert_int(&tree, 20) != 0) return 1; if (insert_int(&tree, 10) != 0) return 1; if (insert_int(&tree, 30) != 0) return 1; if (insert_int(&tree, 15) != 0) return 1; if (insert_int(&tree, 25) != 0) return 1; if (insert_int(&tree, 70) != 0) return 1; if (insert_int(&tree, 80) != 0) return 1; if (insert_int(&tree, 23) != 0) return 1; if (insert_int(&tree, 26) != 0) return 1; if (insert_int(&tree, 5) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); i = 30; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the left tree below it\n", i); bitree_rem_left(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = 99; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the right tree below it\n", i); bitree_rem_right(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = 20; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the right tree below it\n", i); bitree_rem_right(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = bitree_is_leaf(bitree_root(&tree)); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i); i = bitree_is_leaf(bitree_left((bitree_root(&tree)))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i); i = bitree_is_leaf(bitree_left(bitree_left((bitree_root(&tree))))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i); i = bitree_is_leaf(bitree_right(bitree_left((bitree_root(&tree))))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i); fprintf(stdout, "Inserting some nodes\n"); if (insert_int(&tree, 55) != 0) return 1; if (insert_int(&tree, 44) != 0) return 1; if (insert_int(&tree, 77) != 0) return 1; if (insert_int(&tree, 11) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); fprintf(stdout, "(Inorder traversal)\n"); print_inorder(bitree_root(&tree)); fprintf(stdout, "(Postorder traversal)\n"); print_postorder(bitree_root(&tree)); /***************************************************************************** * * * Destroy the binary tree. * * * *****************************************************************************/ fprintf(stdout, "Destroying the tree\n"); bitree_destroy(&tree); return 0; }