创建二叉树

全部代码

 

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct node
{
    int nValue;
    struct node *pLeft;
    struct node *pRight;
}BiTree;

BiTree *CreateBiTree(void)
{
    BiTree *pRoot = NULL;

    //
    pRoot = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot)
    {
        printf("pRoot空间分配失败!\n");
        exit(-1);
    }
    pRoot->nValue = 1;
    pRoot->pLeft = NULL;
    pRoot->pRight = NULL;

    //根的左
    pRoot->pLeft = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot->pLeft)
    {
        printf("pRoot->pLeft空间分配失败!\n");
        exit(-1);
    }
    pRoot->pLeft->nValue = 2;
    pRoot->pLeft->pLeft = NULL;
    pRoot->pLeft->pRight = NULL;

    //根的右
    pRoot->pRight = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot->pRight)
    {
        printf("pRoot->pRight空间分配失败!\n");
        exit(-1);
    }
    pRoot->pRight->nValue = 3;
    pRoot->pRight->pLeft = NULL;
    pRoot->pRight->pRight = NULL;

    //左的左
    pRoot->pLeft->pLeft = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot->pLeft->pLeft)
    {
        printf("pRoot->pLeft->pLeft空间分配失败!\n");
        exit(-1);
    }
    pRoot->pLeft->pLeft->nValue = 4;
    pRoot->pLeft->pLeft->pLeft = NULL;
    pRoot->pLeft->pLeft->pRight = NULL;

    //左的右
    pRoot->pLeft->pRight = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot->pLeft->pRight)
    {
        printf("pRoot->pLeft->pRight空间分配失败!\n");
        exit(-1);
    }
    pRoot->pLeft->pRight->nValue = 5;
    pRoot->pLeft->pRight->pLeft = NULL;
    pRoot->pLeft->pRight->pRight = NULL;

    //右的左
    pRoot->pRight->pLeft = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == pRoot->pRight->pLeft)
    {
        printf("pRoot->pRight->pLeft空间分配失败!\n");
        exit(-1);
    }
    pRoot->pRight->pLeft->nValue = 6;
    pRoot->pRight->pLeft->pLeft = NULL;
    pRoot->pRight->pLeft->pRight = NULL;

    return pRoot;
}

//递归创建二叉树
void RecCreateBiTree(BiTree **ppRoot)
{
    int nNum;

    assert(ppRoot!=NULL);

    //输入节点的值
    scanf("%d", &nNum);

    //检测是否是结束标志
    if(0 == nNum)
    {
        return;
    }

    *ppRoot = (BiTree *)malloc(sizeof(BiTree));
    if(NULL == *ppRoot)
    {
        printf("*ppRoot空间分配失败!");
        exit(-1);
    }
    (*ppRoot)->nValue = nNum;
    (*ppRoot)->pLeft = NULL;
    (*ppRoot)->pRight = NULL;

    //处理当前节点的左和右
    RecCreateBiTree(&(*ppRoot)->pLeft);
    RecCreateBiTree(&(*ppRoot)->pRight);
}

//前序遍历
void PreOrderTraversal(BiTree *pRoot)
{
    if(NULL == pRoot)
    {
        return;
    }

    printf("%d ", pRoot->nValue);
    PreOrderTraversal(pRoot->pLeft);
    PreOrderTraversal(pRoot->pRight);
}

//中序遍历
void MidOrderTraversal(BiTree *pRoot)
{
    if(NULL == pRoot)
    {
        return;
    }

    MidOrderTraversal(pRoot->pLeft);
    printf("%d ", pRoot->nValue);
    MidOrderTraversal(pRoot->pRight);
}

//后序遍历
void LastOrderTraversal(BiTree *pRoot)
{
    if(NULL == pRoot)
    {
        return;
    }

    LastOrderTraversal(pRoot->pLeft);
    LastOrderTraversal(pRoot->pRight);
    printf("%d ", pRoot->nValue);
}

int main(void)
{
    BiTree *pRoot = CreateBiTree();
    PreOrderTraversal(pRoot);
    printf("\n");
    MidOrderTraversal(pRoot);
    printf("\n");
    LastOrderTraversal(pRoot);

    return 0;
}

 

posted @ 2017-11-08 20:28  c&z  阅读(300)  评论(0编辑  收藏  举报