二叉树的创建and遍历

二叉树的创建and遍历

作者: Aidan Lew

思路: 用好队列.

代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
typedef struct TNode* Position;
typedef int ElementType;
typedef Position BinTree; /* 二叉树类型 */
struct TNode { /* 树结点定义 */
    ElementType Data; /* 结点数据 */
    BinTree Left;     /* 指向左子树 */
    BinTree Right;    /* 指向右子树 */
};

void InorderTraversal(BinTree BT)           /*中序遍历*/
{
    if (BT) {
        InorderTraversal(BT->Left);
        /* 此处假设对BT结点的访问就是打印数据 */
        printf("%d ", BT->Data); /* 假设数据为整型 */
        InorderTraversal(BT->Right);
    }
}

void PreorderTraversal(BinTree BT)       /*先序遍历*/
{
    if (BT) {
        printf("%d ", BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
}

void PostorderTraversal(BinTree BT)      /*后序遍历*/
{
    if (BT) {
        PostorderTraversal(BT->Left);
        PostorderTraversal(BT->Right);
        printf("%d ", BT->Data);
    }
}

void LevelorderTraversal(BinTree BT)    /*队列实现层序*/
{
    queue<BinTree> Q;
    BinTree T;

    if (!BT) return; /* 若是空树则直接返回 */

    /* 创建空队列Q */
    Q.push(BT);
    while (!Q.empty()) {
        T = Q.front();
        Q.pop();
        printf("%d ", T->Data); /* 访问取出队列的结点 */
        if (T->Left)   Q.push(T->Left);
        if (T->Right)  Q.push( T->Right);
    }
}
BinTree init()     /*创建一个空树*/
{
    BinTree BT = (BinTree)malloc(sizeof(TNode));
    BT->Right = NULL ;
    BT->Left = NULL ;
    return BT;
}
void PreorderCreate(BinTree &BT)   /*先序创建树*/
{
    int data;
    cin >> data;
    if (data == -1)BT = NULL;
    else
    {
        BT = new TNode;
        BT->Data= data;
        PreorderCreate(BT->Left);
        PreorderCreate(BT->Right);
    }
}
void LevelCreate(BinTree &BT)     /*层序创建*/
{
    queue <BinTree> Que;
    int temp,temp1;
    cin>>temp;
    if(temp==-1){BT=NULL;return;}
    else
    {
            Que.push(BT);
            BT->Data=temp;
            BT->Left=NULL;
            BT->Right=NULL;
    }
    while(!Que.empty())
    {
        cin>>temp;
        BinTree bt=Que.front();
        if(temp==-1){bt->Left=NULL;}
        else
        {
              BinTree bn=(BinTree)malloc(sizeof(TNode));
              bn->Data=temp;
              bt->Left=bn;
              Que.push(bn);
             /*易错,别free(bn);*/
        }
        cin>>temp1;
        bt=Que.front();
         if(temp1==-1){bt->Right=NULL;Que.pop();}
        else
        {
              BinTree bn=(BinTree)malloc(sizeof(TNode));
              bn->Data=temp1;
              bt->Right=bn;
              Que.push(bn);
              Que.pop();
            /* 别 free(bn);*/
        }
    }
}

bool isempty(BinTree *BT)
{
    if (BT == NULL)return true;
    else return false;
}

int main()
{
    BinTree tree1 =init();
    LevelCreate(tree1);
    LevelorderTraversal(tree1);

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值