コード例 #1
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;
}
コード例 #2
0
ファイル: report.c プロジェクト: vikrum/introspective
void
display_report(short num) {

	if(bitree_is_eob(bitree_root(&tree))) {
		printf("No variables declared.\n");
		return;
	}

	switch(num) {

		case 1:
			report1_header();
			report1_data();
			break;

		case 2:
			report2_header();
			report2_data();
			break;

		default:
			fprintf(stderr, "Invalid report type: %d\n", num);
			exit(-1);
			break;
	};
	return;
};
コード例 #3
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;
}
コード例 #4
0
ファイル: bitree.test.c プロジェクト: wikty/alogrithm
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;
}
コード例 #5
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;
}
コード例 #6
0
static int compare_freq(const void *tree1, const void *tree2)
{

	HuffNode *root1, *root2;

/*****************************************************************************
*  Compare the frequencies stored in the root nodes of two binary trees.     *
*****************************************************************************/

	root1 = (HuffNode *) bitree_data(bitree_root((const BiTree *)tree1));
	root2 = (HuffNode *) bitree_data(bitree_root((const BiTree *)tree2));

	if (root1->freq < root2->freq)
		return 1;
	else if (root1->freq > root2->freq)
		return -1;
	else
		return 0;

}
コード例 #7
0
int main(int argc, char *argv[])
{
	struct bitree *tree;
	int cnt;

	tree = build(DEFAULT_TREE_SIZE);
	if (!tree) {
		fprintf(stderr, "build tree error\n");
		exit(EXIT_FAILURE);
	}

	traverse_dump(tree, TRAVE_PREORDER);

	traverse_dump(tree, TRAVE_INORDER);

	traverse_dump(tree, TRAVE_POSTORDER);

	traverse_dump(tree, TRAVE_LEVELORDER);

	fprintf(stdout, "\nmirroring the tree\n");
	mirror(tree->root);
	traverse_dump(tree, TRAVE_LEVELORDER);
	mirror(tree->root); /* reverse */

	fprintf(stdout, "\ndoubling the tree\n");
	double_tree(tree);
	traverse_dump(tree, TRAVE_LEVELORDER);
	traverse_dump(tree, TRAVE_INORDER);
	traverse_dump(tree, TRAVE_PREORDER);

	cnt = print_paths(tree->root);
	fprintf(stdout, "total %d paths\n", cnt);


exit:
	if (visit_list)
		dlist_destroy(visit_list);
	free(bitree_root(tree)->data);
	bitree_destroy(tree);
	free(tree);

	exit(EXIT_SUCCESS);
}
コード例 #8
0
void bitree_avl_test() {
    BiTree_AVL bitree;

    bitree_avl_init(&bitree, (void *) studn_compare, (void *) studn_destroy);

    bitree_avl_insert(&bitree, studn_get_init(1, "zhangsan", 1, 32, 9));
    bitree_avl_insert(&bitree, studn_get_init(2, "lisi", 1, 32, 8));
    bitree_avl_insert(&bitree, studn_get_init(3, "wangdazhui", 1, 32, 9));
    bitree_avl_insert(&bitree, studn_get_init(4, "zhangquandan", 1, 32, 10));
    bitree_avl_insert(&bitree, studn_get_init(5, "zhangfei", 1, 32, 9));
    bitree_avl_insert(&bitree, studn_get_init(6, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(7, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(8, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(9, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(10, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(11, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(12, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(13, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(14, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(15, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(16, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(17, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(18, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(19, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(20, "liucuihua", 0, 22, 11));
    bitree_avl_insert(&bitree, studn_get_init(21, "liucuihua", 0, 22, 11));

    printf("%s%d\n", "bitree avl size :", bitree_avl_size(&bitree));


    BiTreeNode *node = bitree_root(&bitree);
    printBitree(node);
    printf("\n");

    printf("%s\n", "============== release data begin ===============");
    bitree_destroy(&bitree);
    printf("%s\n", "============== release data end ===============");
}
コード例 #9
0
ファイル: ex-1.c プロジェクト: ArineYao/algorithms
static BiTreeNode *search_int(BiTree *tree, int i) {

BiTreeNode         *node;

/*****************************************************************************
*                                                                            *
*  Look up i assuming a binary tree organized as a binary search tree.       *
*                                                                            *
*****************************************************************************/

node = bitree_root(tree);

while (!bitree_is_eob(node)) {

   if (i == *(int *)bitree_data(node)) {

      return node;

      }

   else if (i < *(int *)bitree_data(node)) {

      node = bitree_left(node);

      }

   else {

      node = bitree_right(node);

   }

}

return NULL;

}
コード例 #10
0
ファイル: ex-1.c プロジェクト: boosheng/masterca
int main(int argc, char **argv) {

BiTree             tree;

char               *target;

char               sarray[12][STRSIZ],
                   tarray[12][STRSIZ];

/*****************************************************************************
*                                                                            *
*  Load an array with the data to search.                                    *
*                                                                            *
*****************************************************************************/

strcpy(sarray[hop], "hop");
strcpy(sarray[hat], "hat");
strcpy(sarray[tap], "tap");
strcpy(sarray[bat], "bat");
strcpy(sarray[tip], "tip");
strcpy(sarray[mop], "mop");
strcpy(sarray[mom], "mom");
strcpy(sarray[cat], "cat");
strcpy(sarray[zoo], "zoo");
strcpy(sarray[wax], "wax");
strcpy(sarray[top], "top");
strcpy(sarray[dip], "dip");

/*****************************************************************************
*                                                                            *
*  Initialize the binary search tree.                                        *
*                                                                            *
*****************************************************************************/

bistree_init(&tree, compare_str, NULL);

/*****************************************************************************
*                                                                            *
*  Perform some binary search tree operations.                               *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Inserting some nodes\n");

if (bistree_insert(&tree, sarray[tap]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[tip]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[top]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[cat]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[bat]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Removing %s\n", sarray[tap]);

if (bistree_remove(&tree, &sarray[tap]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[tap]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Removing %s\n", sarray[top]);

if (bistree_remove(&tree, &sarray[top]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[top]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Removing %s\n", sarray[tip]);

if (bistree_remove(&tree, &sarray[tip]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[tip]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Removing %s\n", sarray[hop]);

if (bistree_remove(&tree, &sarray[hop]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[hop]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Inserting %s\n", sarray[hop]);

if (bistree_insert(&tree, sarray[hop]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Inserting %s\n", sarray[dip]);

if (bistree_insert(&tree, sarray[dip]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Inserting %s\n", sarray[tap]);

if (bistree_insert(&tree, sarray[tap]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Inserting %s\n", sarray[top]);

if (bistree_insert(&tree, sarray[top]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Inserting %s\n", sarray[tip]);

if (bistree_insert(&tree, sarray[tip]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Inserting more nodes\n");

if (bistree_insert(&tree, sarray[mom]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[hat]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[mop]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[wax]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

if (bistree_insert(&tree, sarray[zoo]) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
preorder_tree(bitree_root(&tree));

fprintf(stdout, "Removing %s\n", sarray[wax]);

if (bistree_remove(&tree, &sarray[wax]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[wax]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Removing %s\n", sarray[hop]);

if (bistree_remove(&tree, &sarray[hop]) != 0) {

   fprintf(stdout, "Could not find %s\n", sarray[hop]);

   }

else {

   fprintf(stdout, "Tree size is %d\n", bistree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   preorder_tree(bitree_root(&tree));

}

fprintf(stdout, "Looking up some nodes\n");

strcpy(tarray[0], "top");
strcpy(tarray[1], "hop");
strcpy(tarray[2], "wax");
strcpy(tarray[3], "hat");
strcpy(tarray[4], "xxx");

target = tarray[0];

if (bistree_lookup(&tree, (void **)&target) == -1)
   fprintf(stdout, "Could not find %s\n", target);
else
   fprintf(stdout, "Found %s\n", target);

target = tarray[1];

if (bistree_lookup(&tree, (void **)&target) == -1)
   fprintf(stdout, "Could not find %s\n", target);
else
   fprintf(stdout, "Found %s\n", target);

target = tarray[2];

if (bistree_lookup(&tree, (void **)&target) == -1)
   fprintf(stdout, "Could not find %s\n", target);
else
   fprintf(stdout, "Found %s\n", target);

target = tarray[3];

if (bistree_lookup(&tree, (void **)&target) == -1)
   fprintf(stdout, "Could not find %s\n", target);
else
   fprintf(stdout, "Found %s\n", target);

target = tarray[4];

if (bistree_lookup(&tree, (void **)&target) == -1)
   fprintf(stdout, "Could not find %s\n", target);
else
   fprintf(stdout, "Found %s\n", target);

/*****************************************************************************
*                                                                            *
*  Destroy the binary search tree.                                           *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Destroying the search tree\n");
bistree_destroy(&tree);

return 0;

}
コード例 #11
0
ファイル: bistree.c プロジェクト: nmcle/data-structures-c
int
bistree_lookup (BisTree *tree, void **data)
{
  return lookup (tree, bitree_root (tree), data);
}
コード例 #12
0
ファイル: huffman.c プロジェクト: boosheng/masterca
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;

}
コード例 #13
0
ファイル: ex-1.c プロジェクト: ArineYao/algorithms
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;

}
コード例 #14
0
ファイル: report.c プロジェクト: vikrum/introspective
void
report2_data(void) {
	inorder(bitree_root(&tree), report2_format);
	return;
};
コード例 #15
0
int avltree_insert(AvlTree *tree, const void *data) {
  int balanced = 0;
  return insert(tree, &bitree_root(tree), data, &balanced);
}
コード例 #16
0
ファイル: huffman.c プロジェクト: boosheng/masterca
int huffman_compress(const unsigned char *original, unsigned char
   **compressed, int size) {

BiTree             *tree;
HuffCode           table[UCHAR_MAX + 1];

int                freqs[UCHAR_MAX + 1],
                   max,
                   scale,
                   hsize,
                   ipos,
                   opos,
                   cpos,
                   c,
                   i;

unsigned char      *comp,
                   *temp;

/*****************************************************************************
*                                                                            *
*  Initially there is no buffer of compressed data.                          *
*                                                                            *
*****************************************************************************/

*compressed = NULL;

/*****************************************************************************
*                                                                            *
*  Get the frequency of each symbol in the original data.                    *
*                                                                            *
*****************************************************************************/

for (c = 0; c <= UCHAR_MAX; c++)
   freqs[c] = 0;

ipos = 0;

if (size > 0) {

   while (ipos < size) {

      freqs[original[ipos]]++;
      ipos++;

   }

}

/*****************************************************************************
*                                                                            *
*  Scale the frequencies to fit into one byte.                               *
*                                                                            *
*****************************************************************************/

max = UCHAR_MAX;

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

   if (freqs[c] > max)
      max = freqs[c];

}

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

   scale = (int)(freqs[c] / ((double)max / (double)UCHAR_MAX));

   if (scale == 0 && freqs[c] != 0)
      freqs[c] = 1;
   else
      freqs[c] = scale;

}

/*****************************************************************************
*                                                                            *
*  Build the Huffman tree and table of codes for the data.                   *
*                                                                            *
*****************************************************************************/

if (build_tree(freqs, &tree) != 0)
   return -1;

for (c = 0; c <= UCHAR_MAX; c++)
   memset(&table[c], 0, sizeof(HuffCode));

build_table(bitree_root(tree), 0x0000, 0, table);

bitree_destroy(tree);
free(tree);

/*****************************************************************************
*                                                                            *
*  Write the header information.                                             *
*                                                                            *
*****************************************************************************/

hsize = sizeof(int) + (UCHAR_MAX + 1);

if ((comp = (unsigned char *)malloc(hsize)) == NULL)
   return -1;

memcpy(comp, &size, sizeof(int));

for (c = 0; c <= UCHAR_MAX; c++)
   comp[sizeof(int) + c] = (unsigned char)freqs[c];

/*****************************************************************************
*                                                                            *
*  Compress the data.                                                        *
*                                                                            *
*****************************************************************************/

ipos = 0;
opos = hsize * 8;

while (ipos < size) {

   /**************************************************************************
   *                                                                         *
   *  Get the next symbol in the original data.                              *
   *                                                                         *
   **************************************************************************/

   c = original[ipos];

   /**************************************************************************
   *                                                                         *
   *  Write the code for the symbol to the buffer of compressed data.        *
   *                                                                         *
   **************************************************************************/

   for (i = 0; i < table[c].size; i++) {

      if (opos % 8 == 0) {

         /********************************************************************
         *                                                                   *
         *  Allocate another byte for the buffer of compressed data.         *
         *                                                                   *
         ********************************************************************/

         if ((temp = (unsigned char *)realloc(comp,(opos / 8) + 1)) == NULL) {

            free(comp);
            return -1;

         }

         comp = temp;

      }

      cpos = (sizeof(short) * 8) - table[c].size + i;
      bit_set(comp, opos, bit_get((unsigned char *)&table[c].code, cpos));
      opos++;

   }

   ipos++;

}

/*****************************************************************************
*                                                                            *
*  Point to the buffer of compressed data.                                   *
*                                                                            *
*****************************************************************************/

*compressed = comp;

/*****************************************************************************
*                                                                            *
*  Return the number of bytes in the compressed data.                        *
*                                                                            *
*****************************************************************************/

return ((opos - 1) / 8) + 1;

}
コード例 #17
0
ファイル: huffman.c プロジェクト: boosheng/masterca
int huffman_uncompress(const unsigned char *compressed, unsigned char
   **original) {

BiTree             *tree;
BiTreeNode         *node;

int                freqs[UCHAR_MAX + 1],
                   hsize,
                   size,
                   ipos,
                   opos,
                   state,
                   c;

unsigned char      *orig,
                   *temp;

/*****************************************************************************
*                                                                            *
*  Initially there is no buffer of original data.                            *
*                                                                            *
*****************************************************************************/

*original = orig = NULL;

/*****************************************************************************
*                                                                            *
*  Get the header information from the buffer of compressed data.            *
*                                                                            *
*****************************************************************************/

hsize = sizeof(int) + (UCHAR_MAX + 1);
memcpy(&size, compressed, sizeof(int));

for (c = 0; c <= UCHAR_MAX; c++)
   freqs[c] = compressed[sizeof(int) + c];

/*****************************************************************************
*                                                                            *
*  Rebuild the Huffman tree used previously to compress the data.            *
*                                                                            *
*****************************************************************************/

if (build_tree(freqs, &tree) != 0)
   return -1;

/*****************************************************************************
*                                                                            *
*  Uncompress the data.                                                      *
*                                                                            *
*****************************************************************************/

ipos = hsize * 8;
opos = 0;
node = bitree_root(tree);

while (opos < size) {

   /**************************************************************************
   *                                                                         *
   *  Get the next bit in the compressed data.                               *
   *                                                                         *
   **************************************************************************/

   state = bit_get(compressed, ipos);
   ipos++;

   if (state == 0) {

      /***********************************************************************
      *                                                                      *
      *  Move to the left.                                                   *
      *                                                                      *
      ***********************************************************************/

      if (bitree_is_eob(node) || bitree_is_eob(bitree_left(node))) {

         bitree_destroy(tree);
         free(tree);
         return -1;

         }

      else
         node = bitree_left(node);

      }

   else {

      /***********************************************************************
      *                                                                      *
      *  Move to the right.                                                  *
      *                                                                      *
      ***********************************************************************/

      if (bitree_is_eob(node) || bitree_is_eob(bitree_right(node))) {

         bitree_destroy(tree);
         free(tree);
         return -1;

         }

      else
         node = bitree_right(node);

   }

   if (bitree_is_eob(bitree_left(node))&&bitree_is_eob(bitree_right(node))) {

      /***********************************************************************
      *                                                                      *
      *  Write the symbol in the leaf node to the buffer of original data.   *
      *                                                                      *
      ***********************************************************************/

      if (opos > 0) {

         if ((temp = (unsigned char *)realloc(orig, opos + 1)) == NULL) {

            bitree_destroy(tree);
            free(tree);
            free(orig);
            return -1;

         }

         orig = temp;

         }

      else {

         if ((orig = (unsigned char *)malloc(1)) == NULL) {

            bitree_destroy(tree);
            free(tree);
            return -1;

         }

      }

      orig[opos] = ((HuffNode *)bitree_data(node))->symbol;
      opos++;

      /***********************************************************************
      *                                                                      *
      *  Move back to the top of the tree.                                   *
      *                                                                      *
      ***********************************************************************/

      node = bitree_root(tree);

   }

}

bitree_destroy(tree);
free(tree);

/*****************************************************************************
*                                                                            *
*  Point to the buffer of original data.                                     *
*                                                                            *
*****************************************************************************/

*original = orig;

/*****************************************************************************
*                                                                            *
*  Return the number of bytes in the original data.                          *
*                                                                            *
*****************************************************************************/

return opos;

}
コード例 #18
0
ファイル: bistree.c プロジェクト: nmcle/data-structures-c
int
bistree_insert (BisTree *tree, const void *data)
{
  int balanced = 0;
  return insert (tree, &bitree_root (tree), data, &balanced);
}
コード例 #19
0
int avltree_lookup(AvlTree *tree, void **data) {
  return lookup(tree, bitree_root(tree), data);
}
コード例 #20
0
ファイル: bistree.c プロジェクト: nmcle/data-structures-c
int
bistree_remove (BisTree *tree, const void *data)
{
  return hide (tree, bitree_root (tree), data);
}