//求深度
int BTreeDepth(const BiTree T) {
    int depth = 0;
    if(T) {
        int left = BTreeDepth(T->lchild);
        int right = BTreeDepth(T->rchild);
        depth = (left>right?left:right)+1;
    }
    return depth;
}
Beispiel #2
0
/**
 *  Find the depth of BT
 *
 *  @param bt BT root pointer
 *
 *  @return Depth of BT
 */
int BTreeDepth(BTreeNode *bt){
    if (bt == NULL) {
        return FAILURE;
    }else{
        int dep1 = BTreeDepth(bt->lchild);
        int dep2 = BTreeDepth(bt->rchild);
        if (dep1>dep2) {
            return dep1+1;
        }else{
            return dep2+1;
        }

    }
}
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);

}
Beispiel #4
0
int main()
{
    BTreeNode *bt;
    char *b;
    ElemtType x,*px;
    InitBTree(&bt);
    b = "c(a(d,e),b)";
    CreateBTree(&bt, b);
    /*
    if (bt != NULL) {
        printf("%c", bt->data);
    }
     */
    printf("preorder\n");
    PreOrder(bt);printf("\n");
    printf("inorder\n");
    InOrder(bt);printf("\n");
    printf("postorder\n");
    PostOrder(bt);printf("\n");
    printf("levelorder\n");
    LevelOrder(bt);printf("\n");
    printf("find one root\n");
    scanf("%c",&x);
    px = FindBTree(bt, x);
    if (px) {
        printf("find it !: %c\n",*px);
    }
    else{
        printf("not find it\n");
    }
    printf("the depth of bt is \n");
    printf("%d\n",BTreeDepth(bt));
    ClearBTree(&bt);
    return 0;
}