二叉樹的基本操作(C語言實現)

二叉樹的建立、遍歷、統計、樹狀打印

#include <stdio.h>
#include <stdlib.h>
int c_Node = 0;     //節點數
int c_Leaf = 0;     //葉子數
int depth = 0;      //二叉樹高度
typedef char ElemType;
typedef struct Node         //節點
{
    ElemType data;          //節點數據
    struct Node *lchild;        //左孩子
    struct Node *rchild;        //右孩子
} biNode, *biTree;

//先序建立二叉樹
void createTree(biNode **root)
{

    ElemType data;
    scanf("%c",&data);
    if(data == '#')
    {
        (*root)=NULL;
    }

    else
    {
        *root = (biNode *)malloc(sizeof(biNode));
        if((*root) == NULL)
        {
            printf("分配空間失敗!\n");
            exit(0);
        }
        (*root)->data = data;
        createTree(&((*root)->lchild));
        createTree(&((*root)->rchild));
    }

}


//先序遍歷二叉樹
void preOrder(biNode *root)
{
    if(root)
    {
        printf("%c ",root->data);
        preOrder(root->lchild);
        preOrder(root->rchild);
    }

}


//中序遍歷二叉樹
void inOrder(biNode *root)
{
    if(root)
    {
        inOrder(root->lchild);
        printf("%c ",root->data);
        inOrder(root->rchild);
    }

}

//後序遍歷二叉樹
void postOrder(biNode *root)
{
    if(root)
    {
        postOrder(root->lchild);
        postOrder(root->rchild);
        printf("%c ",root->data);
    }
}


//統計節點數
void cal_Node(biNode *root)
{
    if(root)
    {
        c_Node++;
        cal_Node(root->lchild);
        cal_Node(root->rchild);
    }
}

//統計葉子數 方法一
void cal_Leaf(biNode *root)
{
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL) c_Leaf++;
        cal_Leaf(root->lchild);
        cal_Leaf(root->rchild);
    }
}

//統計葉子數 方法二
int calLeaf(biNode *root)
{
    int hl,hr;
    if(root==NULL) return 0;
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL) return 1;
        hl = calLeaf(root->lchild);
        hr = calLeaf(root->rchild);
    }
    return hl+hr;
}

//打印葉子數據
void printLeaf(biNode *root)
{
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL)
            printf("%c ",root->data);
        printLeaf(root->lchild);
        printLeaf(root->rchild);
    }
}

//計算二叉樹深度(方法一)
int cal_Depth(biNode *root)
{
    if(root == NULL) return 0;
    if(root)
    {
        int hl = cal_Depth(root->lchild);
        int hr = cal_Depth(root->rchild);
        return hl>hr?hl+1:hr+1;
    }
}

//計算二叉樹高度(方法二)
void calDepth(biNode *root, int h)
{

    if(root)
    {
        if(h > depth) depth = h;
        calDepth(root->lchild,h+1);
        calDepth(root->rchild,h+1);
    }
}

//樹狀打印二叉樹
void print_Tree(biNode *root, int h) {

    if(root) {
        print_Tree(root->rchild, h+1);
        for(int i=0; i<h; i++) printf("--");
        printf("%c\n",root->data);
        print_Tree(root->lchild, h+1);
    }

}


int main()
{
    printf("先序輸入二叉樹數據(# = NULL):");
    biNode *root;
    createTree(&root);
    printf("先序遍歷結果:");
    preOrder(root);
    printf("\n\n中序遍歷結果:  ");
    inOrder(root);
    printf("\n\n後序遍歷結果: ");
    postOrder(root);
    cal_Node(root);
    cal_Leaf(root);
    printf("\n\n節點數: %d\n",c_Node);
    printf("\n\n葉子點數:%d\n",c_Leaf);
    printf("葉子數(方法二):%d\n",calLeaf(root));
    printf("\n打印葉子節點: ");
    printLeaf(root);
    calDepth(root,1);
    printf("\n二叉樹高度(方法一):%d\n",cal_Depth(root));
    printf("二叉樹高度(方法二):%d\n",depth);
    printf("\n\n樹狀打印二叉樹\n\n");
    print_Tree(root,1);


}
/*
測試數據
ABC##DE#G##F###
*/

測試結果

原文出處https://blog.csdn.net/Vincent_Xupt/article/details/78797907

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章