예제 #1
0
/*按先序次序输入二叉树中结点的值,#表示空树*/
void BTreeInit(BiTree *T) {
    char ch;
    scanf("%c", &ch);
    if (ch == '#') {  //#表示空结点
        *T = NULL;
    } else {
        *T = (BiTree)malloc(sizeof(BiNode));
        if (!T) {
            printf("分配失败!");
            exit(-1);
        }
        (*T)->data = ch;            //生成根结点
        BTreeInit(&(*T)->lchild);   //构造左子树
        BTreeInit(&(*T)->rchild);   //构造右子树
    }
}
예제 #2
0
void BTreeTest() {
    printf("请输入二叉树:");
    BiTree T = NULL;
    BTreeInit(&T);
    printf("----创建成功!-----");
    
    printf("\n先序遍历:");
    BTreePreOrderTraverse(T,BTreePrint);
    
    printf("\n中序遍历:");
    BTreeInOrderTraverse(T,BTreePrint);
    
    printf("\n后序遍历:");
    BTreeBackOrderTraverse(T,BTreePrint);
    
    printf("\n深度=%d\n",BTreeDepth(T));
    
    int count = 0;
    BTreeLeafCount(T,&count);
    printf("\n叶子结点个数=%d\n",count);

    count = 0;
    BTreeAllCount(T,&count);
    printf("\n所有结点个数=%d\n",count);

}
예제 #3
0
int
main( int ac, char* pArgs[] )
{
	BTree_t		BTree;
	BTCursor_t	Cursor;
	BTCursor_t	Cursor1;
	BTCursor_t	Cursor2;

	NHeapPoolInit( &Pool, 1024*10, 3, malloc, free, 0/*HEAP_DYNAMIC*/ );

	BTreeInit( &BTreeMem, 2, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL );
	BTreeInit( &BTree, 2, NULL, NULL, BTMalloc, BTFree, NULL );

	BTCursorOpen( &Cursor, &BTree );
	BTCursorOpen( &Cursor1, &BTree );
	BTCursorOpen( &Cursor2, &BTree );


printf("\nINSERT\n");
BTreePrint(&BTree);printf("Insert 20\n");
	BTInsert( &BTree, (void*)20 );

//	BTCFind( &Cursor1, (void*)20 );

BTreePrint(&BTree);printf("Insert 40\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)40 );
BTreePrint(&BTree);printf("Insert 10\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)10 );
BTreePrint(&BTree);printf("Insert 30\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)30 );
BTreePrint(&BTree);printf("Insert 15\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)15 );

BTreePrint(&BTree);printf("Insert 35\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)35 );
BTreePrint(&BTree);printf("Insert 7\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)7 );
BTreePrint(&BTree);printf("Insert 26\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)26 );
BTreePrint(&BTree);printf("Insert 18\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)18 );
BTreePrint(&BTree);printf("Insert 22\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)22 );

BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)5 );
printf("\nINSERT 5\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

	BTInsert( &BTree, (void*)42 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)13 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)46 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)27 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)8 );
BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)32 );
printf("\nINSERT 32\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

	BTreePrint(&BTree);printf("Insert 38\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)38 );
	BTreePrint(&BTree);printf("Insert 24\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)24 );
	BTreePrint(&BTree);printf("Insert 45\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)45 );
	BTreePrint(&BTree);printf("Insert 25\n");BTreePrint(&BTreeMem);
	BTInsert( &BTree, (void*)25 );


//	Insert( &BTree, (void*)15 );
printf("\nINSERT 25\n");
BTreePrint( &BTree );BTreePrint(&BTreeMem);

//	BTCFind( &Cursor2, (void*)25 );

	BTCHead( &Cursor );
	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

BTreePrint( &BTree );BTreePrint(&BTreeMem);
printf("\nDELETE 25\n");
	if( !BTCFind( &Cursor, (void*)25 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 45\n");
	if( !BTCFind( &Cursor, (void*)45 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 24\n");
	if( !BTCFind( &Cursor, (void*)24 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);
printf("\nDELETE 38\n");
	if( !BTCFind( &Cursor, (void*)38 ) )	BTCDelete( &Cursor );
	BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem);

	BTDelete( &BTree, (void*)25 );
	BTDelete( &BTree, (void*)45 );
	BTDelete( &BTree, (void*)24 );

	BTreePrint( &BTree );BTreePrint(&BTreeMem);
	BTCHead( &Cursor );
	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

printf("\nFIND 8\n");
	if( !BTCFind( &Cursor, (void*)8 ) )	PrintCursor( &Cursor);

printf("\nFIND 20<=\n");
	if( !BTCFindLower( &Cursor, (void*)20 ) )	PrintCursor( &Cursor );
	while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor );

	BTDelete( &BTree, (void*)38 );
	BTDelete( &BTree, (void*)32 );

	BTDelete( &BTree, (void*)8 );
	BTDelete( &BTree, (void*)27 );
	BTDelete( &BTree, (void*)46 );
	BTDelete( &BTree, (void*)13 );
	BTDelete( &BTree, (void*)42 );

	BTDelete( &BTree, (void*)5 );
	BTDelete( &BTree, (void*)22 );
	BTDelete( &BTree, (void*)18 );
	BTDelete( &BTree, (void*)26 );

	BTDelete( &BTree, (void*)7 );
	BTDelete( &BTree, (void*)35 );
	BTDelete( &BTree, (void*)15 );

	BTreePrint( &BTree );BTreePrint(&BTreeMem);

//	BTDelete( &BTree, (void*)20 );
//	BTDelete( &BTree, (void*)40 );
//	BTDelete( &BTree, (void*)10 );
//	BTDelete( &BTree, (void*)30 );

/***
printf("END\n");
	BTreePrint( &BTree );
printf("HEAD\n");
	BTCHead( &Cursor );PrintCursor( &Cursor );
printf("NEXT\n");
	printf("%d\n",BTCDeleteAndNext( &Cursor ));PrintCursor( &Cursor );
printf("Key=%d\n", BTCGetKey( int, &Cursor ) );
***/

	BTreePrint( &BTree );
printf("=== MEMORY LEAK ===\n");
	BTreePrint( &BTreeMem );

	NHeapPoolDump( &Pool );


	BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, NULL );
	int	i;
	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	for( i = 0; i < 100; i++ ) {
		BTDelete( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );

	int	Ret;

	for( i = 0; i < 10; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	BTCursorOpen( &Cursor, &BTree );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%p)\n", BTCGetKey( void*, &Cursor ) );
		Ret = BTCDelete( &Cursor );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTree );

	BTreeInit( &BTreeMem, 3, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL );
	BTreeInit( &BTree, 5, NULL, NULL, BTMalloc, BTFree, NULL );
	for( i = 206; i < 285; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	BTCursorOpen( &Cursor, &BTree );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) );
		Ret = BTCDelete( &Cursor );
		BTreePrint( &BTree );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTree );
printf("=== MEMORY LEAK ===\n");
	BTreePrint( &BTreeMem );

	NHeapPoolDestroy( &Pool );
	NHeapPoolInit( &Pool, 1024*10, 10, malloc, free, 0/*HEAP_DYNAMIC*/ );
	BTreeInit( &BTreeMem, 5, NULL, NULL, BTMemAlloc, BTMemFree, NULL );
	for( i = 206; i < 285; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(i) );
	}
	BTreePrint( &BTreeMem );
	BTCursorOpen( &Cursor, &BTreeMem );
	while( BTCHead( &Cursor ) == 0 ) {
		printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) );
		Ret = BTCDelete( &Cursor );
		BTreePrint( &BTreeMem );
	}
	BTCursorClose( &Cursor );
	BTreePrint( &BTreeMem );
//	BTreeDestroy( &BTreeMem );
	NHeapPoolDump( &Pool );

//#define	RAND_ARRAY	151
#define	RAND_ARRAY	10000
	int	aRand[RAND_ARRAY];

	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	BTreePrint( &BTreeMem );

//	BTInsert( &BTreeMem, INT32_PNT(rand()) );
//	BTreePrint( &BTreeMem );

	for( i = 0; i < RAND_ARRAY; i++ ) {
		Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) );
		ASSERT( !Ret );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );

	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) );
		ASSERT( !Ret );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );

	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(i) );
	}
	BTCursorOpen( &Cursor, &BTreeMem );
	for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) {
		i = PNT_INT32(BTCGetKey( void*, &Cursor ));
printf("%d\n", i );
		BTCDelete( &Cursor );
	}
	// Destroy
	BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, BDestroy );
	for( i = 0; i < 20; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );

	BTreeDestroy( &BTree );

	BTreePrint( &BTree );

	return( 0 );
}
예제 #4
0
int
main( int ac, char* pArgs[] )
{
	BTree_t		BTree;
	BTCursor_t	Cursor;
	int	Ret;

	NHeapPoolInit( &Pool, 1024*20, 5, malloc, free, 0/*HEAP_DYNAMIC*/ );

	BTreeInit( &BTreeMem, 5, NULL, NULL, BTMemAlloc, BTMemFree, NULL );
	BTreeInit( &BTree, 2, NULL, NULL, BTMalloc, BTFree, NULL );

	BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, NULL );
	int	i;
	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	for( i = 0; i < 100; i++ ) {
		BTDelete( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );

	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTree, INT32_PNT(i) );
	}
	BTreePrint( &BTree );
	BTCursorOpen( &Cursor, &BTree );
	for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) {
		printf("DELETE(%d)\n", i = PNT_INT32(BTCGetKey( void*, &Cursor )) );
		if( i != 10 ) {
			Ret = BTCDelete( &Cursor );
		}
	}
	BTreePrint( &BTree );

//#define	RAND_ARRAY	151
#define	RAND_ARRAY	10000
	int	aRand[RAND_ARRAY];
int j;
for( j = 0; j < 100; j++ ) {
	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	BTreePrint( &BTreeMem );

//	BTInsert( &BTreeMem, INT32_PNT(rand()) );
//	BTreePrint( &BTreeMem );

	BTCursorOpen( &Cursor, &BTreeMem );
	for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) {
		Ret = BTCDelete( &Cursor );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );
}
for( j = 0; j < 100; j++ ) {
	for( i = 0; i < RAND_ARRAY; i++ ) {
		aRand[i]	= rand();
	}
	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	BTreePrint( &BTreeMem );

//	BTInsert( &BTreeMem, INT32_PNT(rand()) );
//	BTreePrint( &BTreeMem );

	for( i = 0; i < RAND_ARRAY; i++ ) {
		BTDelete( &BTreeMem, INT32_PNT(aRand[i]) );
	}
	BTreePrint( &BTreeMem );
	NHeapPoolDump( &Pool );
}

	for( i = 0; i < 100; i++ ) {
		BTInsert( &BTreeMem, INT32_PNT(i) );
	}
	BTDelete( &BTreeMem, INT32_PNT(99) );
	BTreePrint( &BTreeMem );

	return( 0 );
}
예제 #5
0
void BTreeDelete(TBTree *T) {
   if(T == NULL)
      return;
   deleteNode(T->root);
   BTreeInit(T, T->type);
}