int main(){ bstree tree; ini_bstree(&tree); //初始化 int i; int * pData; for(i = 0;i < 36;i++){ pData = (int *) malloc(sizeof(int)); *pData = i; if(insert_bstree(&tree,(void *)pData,compare_for_int) != TRUE){ free(pData); } } printf("Insert [0,36) tree is \n"); inOrder_bstree(&tree,myPrint); for(i = 40;i > 1;i--){ pData = (int *) malloc(sizeof(int)); *pData = i; if(insert_bstree(&tree,(void *)pData,compare_for_int) != TRUE){ free(pData); } } printf("\n"); printf("Insert (1,40] tree is \n"); inOrder_bstree(&tree,myPrint); printf("\n"); pData = (int *) malloc(sizeof(int)); for(i = 23;i < 30;i++){ *pData = i; if(delete_bstree(&tree,(void *)pData,compare_for_int) != TRUE){ printf("ERROR"); } } printf("After delete [23,30)\n"); inOrder_bstree(&tree,myPrint); printf("\n"); for(i = 53;i >= 0;i--){ *pData = i; if(delete_bstree(&tree,(void *)pData,compare_for_int) != TRUE){ printf("NO %d\t",*pData); } } printf("\n"); printf("After delete [53,0]\n"); inOrder_bstree(&tree,myPrint); printf("\n"); free(pData); if(empty_bstree(&tree) == TRUE){ printf("The tree is empty\n"); } free_bstree(&tree); return 0; }
void main() { int i, ilen; BSTree root=NULL; printf("== 依次添加: "); ilen = TBL_SIZE(arr); for(i=0; i<ilen; i++) { printf("%d ", arr[i]); root = insert_bstree(root, arr[i]); } printf("\n== 前序遍历: "); preorder_bstree(root); printf("\n== 中序遍历: "); inorder_bstree(root); printf("\n== 后序遍历: "); postorder_bstree(root); printf("\n"); printf("== 最小值: %d\n", bstree_minimum(root)->key); printf("== 最大值: %d\n", bstree_maximum(root)->key); printf("== 树的详细信息: \n"); print_bstree(root, root->key, 0); printf("\n== 删除根节点: %d", arr[3]); root = delete_bstree(root, arr[3]); printf("\n== 中序遍历: "); inorder_bstree(root); printf("\n"); // 销毁二叉树 destroy_bstree(root); }