示例#1
0
int setup(){
	if((bt = (BiTree *)malloc(sizeof(BiTree))) == NULL){
		return -1;
	}
	if((bt1 = (BiTree *)malloc(sizeof(BiTree))) == NULL){
		free(bt);
		return -1;
	}
	if((bt2 = (BiTree *)malloc(sizeof(BiTree))) == NULL){
		free(bt);
		free(bt1);
		return -1;
	}
	bitree_init(bt, destroy, compare);
	bitree_init(bt1, destroy, compare);
	bitree_init(bt2, destroy, compare);

	for(int i=0; i<10; i++){
		if((u[i] = (UserInfo *)malloc(sizeof(UserInfo))) == NULL){
			for(int j=i-1; j>=0; j--){
				free(u[j]);
				return -1;
			}
		}
		u[i]->seq = i;
		sprintf(u[i]->username, "%d-item", i);
	}
	return 0;
}
示例#2
0
int main(int argc, char const *argv[])
{
    //create a unbalanced tree
    BiTree * unbalanced_tree = (BiTree*)malloc(sizeof(BiTree));

    bitree_init(unbalanced_tree, NULL);

    int tree_node_index[5];
    for (int i = 0; i < 5; ++i)
    {
        tree_node_index[i] = i;
    }
    BiTreeNode * position = NULL;

    bitree_ins_left(unbalanced_tree, NULL, (void *)&tree_node_index[0]);
    bitree_ins_left(unbalanced_tree, bitree_root(unbalanced_tree), (void *)&tree_node_index[1]);
    position = bitree_root(unbalanced_tree)->left;
    bitree_ins_left(unbalanced_tree, position, (void *)&tree_node_index[2]);
    position = position->left;
    bitree_ins_left(unbalanced_tree, position, (void *)&tree_node_index[3]);
    bitree_ins_right(unbalanced_tree, bitree_root(unbalanced_tree), (void *)&tree_node_index[4]);

    int balance_check_result = check_balance(bitree_root(unbalanced_tree));
    if(balance_check_result < 0)
        printf("unbalanced tree\n");
    else
        printf("balanced tree\n");

    bitree_destroy(unbalanced_tree);
    free(unbalanced_tree);

    //create a balanced tree
    BiTree * balanced_tree = (BiTree *)malloc(sizeof(BiTree));
    bitree_init(balanced_tree, NULL);
    bitree_ins_left(balanced_tree, NULL, (void *)&tree_node_index[0]);
    bitree_ins_left(balanced_tree, bitree_root(balanced_tree), (void *)&tree_node_index[1]);

    position = bitree_root(balanced_tree)->left;

    bitree_ins_left(balanced_tree, position, (void *)&tree_node_index[2]);
    bitree_ins_right(balanced_tree, position, (void *)&tree_node_index[3]);
    bitree_ins_right(balanced_tree, bitree_root(balanced_tree), (void *)&tree_node_index[4]);

    balance_check_result = check_balance(bitree_root(balanced_tree));
    if(balance_check_result < 1)
        printf("unbalanced!\n");
    else
        printf("balanced\n");
    bitree_destroy(balanced_tree);
    free(balanced_tree);    
    return 0;
}
示例#3
0
/* 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;
}
示例#4
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;
}
示例#5
0
void
bistree_init (BisTree *tree,
              int (*compare) (const void *key1, const void *key2),
              void (*destroy) (void *data))
{
  /* Initialize the tree.  */
  bitree_init (tree, destroy);
  tree->compare = compare;
  return;
}
示例#6
0
struct bitree *build(int n)
{
	int i, *data;
	struct bitree *tree = NULL;
	struct bitree_node *position = NULL;

	tree = (struct bitree *) malloc(sizeof(struct bitree));
	if (!tree)
		return NULL;

	bitree_init(tree, NULL);

	data = (int *) malloc(n * sizeof(int));
	if (!data) {
		goto fail;
	}

	for (i = 0; i <= n/2; i++) {
		data[i] = i + 1;
		if (bitree_ins_left(tree, position, (const void *) &data[i]) != 0)
			goto fail;

		if (!position) {
			if (bitree_root(tree) == NULL)
				goto fail;
			position = bitree_root(tree);
		} else {
			position = position->left;
		}
	}

	position = bitree_root(tree);
	for (i = n/2 + 1; i < n; i++) {
		data[i] = i + 1;
		if (bitree_ins_right(tree, position, (const void *) &data[i]) != 0)
			goto fail;

		position = position->right;
	}

	return tree;

fail:
	free(tree);
	return NULL;
}
示例#7
0
int main(void) {
	BiTree * tree;
	char * a[] = {"kobe", "bryant", "james", "jordan", "johnson"};
	List *list;
	ListNode *node;

	tree = (BiTree *) malloc(sizeof(BiTree));
	if (tree == NULL) return -1;

	list = (List *) malloc(sizeof(List));
	if (list == NULL) return -1;

	bitree_init(tree, &data_destroy);
	bitree_ins_left(tree, NULL, a[0]);


	bitree_ins_left(tree, tree->root, a[1]);
	bitree_ins_right(tree, tree->root, a[2]);

	bitree_ins_left(tree, tree->root->left, a[3]);
	bitree_ins_right(tree, tree->root->left, a[4]);

	//list init

	list_init(list, &data_destroy);
	
	postorder(tree->root, list);
	node = list->head;
	
	while(node != NULL) {
		printf("%s\n", (char *) node->data );
		node = node->next;
	}
	

	bitree_destroy(tree);
	list_destroy(list);
	return 0;	
}
示例#8
0
static int build_tree(int *freqs, BiTree **tree) {

BiTree             *init,
                   *merge,
                   *left,
                   *right;

PQueue             pqueue;

HuffNode           *data;

int                size,
                   c;

/*****************************************************************************
*                                                                            *
*  Initialize the priority queue of binary trees.                            *
*                                                                            *
*****************************************************************************/

*tree = NULL;

pqueue_init(&pqueue, compare_freq, destroy_tree);

for (c = 0; c <= UCHAR_MAX; c++) {

   if (freqs[c] != 0) {

      /***********************************************************************
      *                                                                      *
      *  Set up a binary tree for the current symbol and its frequency.      *
      *                                                                      *
      ***********************************************************************/

      if ((init = (BiTree *)malloc(sizeof(BiTree))) == NULL) {

         pqueue_destroy(&pqueue);
         return -1;

      }

      bitree_init(init, free);

      if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {

         pqueue_destroy(&pqueue);
         return -1;

      }

      data->symbol = c;
      data->freq = freqs[c];

      if (bitree_ins_left(init, NULL, data) != 0) {

         free(data);
         bitree_destroy(init);
         free(init);
         pqueue_destroy(&pqueue);
         return -1;

      }

      /***********************************************************************
      *                                                                      *
      *  Insert the binary tree into the priority queue.                     *
      *                                                                      *
      ***********************************************************************/

      if (pqueue_insert(&pqueue, init) != 0) {

         bitree_destroy(init);
         free(init);
         pqueue_destroy(&pqueue);
         return -1;

      }

   }

}

/*****************************************************************************
*                                                                            *
*  Build a Huffman tree by merging trees in the priority queue.              *
*                                                                            *
*****************************************************************************/

size = pqueue_size(&pqueue);

for (c = 1; c <= size - 1; c++) {

   /**************************************************************************
   *                                                                         *
   *  Allocate storage for the next merged tree.                             *
   *                                                                         *
   **************************************************************************/

   if ((merge = (BiTree *)malloc(sizeof(BiTree))) == NULL) {

      pqueue_destroy(&pqueue);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Extract the two trees whose root nodes have the smallest frequencies.  *
   *                                                                         *
   **************************************************************************/

   if (pqueue_extract(&pqueue, (void **)&left) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   if (pqueue_extract(&pqueue, (void **)&right) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Allocate storage for the data in the root node of the merged tree.     *
   *                                                                         *
   **************************************************************************/

   if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   memset(data, 0, sizeof(HuffNode));

   /**************************************************************************
   *                                                                         *
   *  Sum the frequencies in the root nodes of the trees being merged.       *
   *                                                                         *
   **************************************************************************/

   data->freq = ((HuffNode *)bitree_data(bitree_root(left)))->freq +
      ((HuffNode *)bitree_data(bitree_root(right)))->freq;

   /**************************************************************************
   *                                                                         *
   *  Merge the two trees.                                                   *
   *                                                                         *
   **************************************************************************/

   if (bitree_merge(merge, left, right, data) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Insert the merged tree into the priority queue and free the others.    *
   *                                                                         *
   **************************************************************************/

   if (pqueue_insert(&pqueue, merge) != 0) {

      pqueue_destroy(&pqueue);
      bitree_destroy(merge);
      free(merge);
      return -1;

   }

   free(left);
   free(right);

}

/*****************************************************************************
*                                                                            *
*  The last tree in the priority queue is the Huffman tree.                  *
*                                                                            *
*****************************************************************************/

if (pqueue_extract(&pqueue, (void **)tree) != 0) {

   pqueue_destroy(&pqueue);
   return -1;

   }

else {

   pqueue_destroy(&pqueue);

}

return 0;

}
示例#9
0
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;

}
示例#10
0
void bi_search_tree_init(BI_S_TREE *b, int (*compare)(void *d1, void *d2))
{
	bitree_init(b, compare);
}
示例#11
0
void avltree_init(AVLTREE *b, int (*compare)(void *d1, void *d2))
{
	bitree_init(b, compare);
	return;
}