Esempio n. 1
0
void BTree_create(BTree* tree, const int* data, int length)
{
	assert(tree);

	int i;

#ifdef DEBUG_TREE
	debug_print("\n 开始创建 B- 树,关键字为:\n");
	for (i = 0; i < length; i++) {
		printf(" %c ", data[i]);
	}
	debug_print("\n");
#endif


	for (i = 0; i < length; i++) {
#ifdef DEBUG_TREE
		debug_print("\n插入关键字 %c:\n", data[i]);
#endif
		BTree_insert(tree, data[i]);

#ifdef DEBUG_TREE
		BTree_print(*tree);
#endif
	}

	debug_print("\n");
}
Esempio n. 2
0
void test_btree()
{
	const int length = 10;
	int array[length] = {
		'G', 'M', 'P', 'X', 'A', 'C', 'D', 'E', 'J', 'K',
		//'N', 'O', 'R', 'S', 'T', 'U', 'V', 'Y', 'Z', 'F'
	};

	BTree tree = NULL;
	BTNode* node = NULL;
	int pos = -1;
	int key1 = 'R';		// in the tree.
	int key2 = 'B';		// not in the tree.

	// 创建
	BTree_create(&tree, array, length);

	printf("\n=== 创建 B 树 ===\n");
	BTree_print(tree);
	printf("\n");

	// 查找
	test_BTree_search(tree, key1);
	printf("\n");
	test_BTree_search(tree, key2);

	// 插入关键字
	printf("\n插入关键字 %c \n", key2);
	BTree_insert(&tree, key2);
	BTree_print(tree);
	printf("\n");

	test_BTree_search(tree, key2);

	// 移除关键字
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'M';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'E';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'G';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'A';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'D';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'K';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'P';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'J';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'C';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	key2 = 'X';
	test_BTree_remove(&tree, key2);
	test_BTree_search(tree, key2);

	// 销毁
	BTree_destory(&tree);
}