数据结构(五)层次遍历

数据结构(五)层次遍历

// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType BiTree
using namespace std;
typedef struct BiTNode
{
    char data;
    struct BiTNode* lchild, * rchild;
}BitTNode, * BiTree;
typedef struct  linknode //链式节点
{
    ElemType data;
    struct linknode* next;

}LinkNode;

//链式队列
typedef struct
{
    LinkNode* front, *rear;

}LinkQueue;

void InitQueue(LinkQueue &Q)
{
    //带头节点的队列初始化
        Q.rear=Q.front=(LinkNode*)malloc(sizeof(LinkNode));
        Q.front->next = NULL;
}

bool IsEmpty(LinkQueue& Q)
{
    if (Q.rear == Q.front)
    {
        return true;
    }
    return false;

}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s= (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x;
    s->next = Q.rear->next;
    Q.rear->next = s;
    Q.rear = s;

}

bool DeQueue(LinkQueue& Q, ElemType &x)
{
    if (IsEmpty(Q))
    {
        //队列为空
        return false;
    }
    LinkNode* p = Q.front->next;
    x = p->data;
    Q.front->next = p->next;
    
    if (p == Q.rear)  //要删除的为尾队列
    {
        Q.rear = Q.front;
    }
    free(p);
    return true;
}

//层次遍历
void LevelOrder(BiTree T)
{
    LinkQueue q;
    BiTNode* p;
    //初始化队列
    InitQueue(q);
    EnQueue(q,T);  //将根节点入队
    while (!IsEmpty(q))
    {
        DeQueue(q,p);
        printf("%c\t",p->data);
        if (p->lchild != NULL)
        {
            EnQueue(q,p->lchild);
        }
        if (p->rchild != NULL)
        {
            EnQueue(q, p->rchild);
        }

    }



}
bool createBiTree(BiTree& T)
{
    char ch;
    cin >> ch;

    if (ch == '.')
    {
        T = NULL; //如果输入 '.' , 该树空结点
    }
    else
    {
        T = (BitTNode*)malloc(sizeof(BitTNode));
        if (T == NULL)
        {
            printf("tree error!\n");
            exit(1);
        }
        T->data = ch;
        createBiTree(T->lchild);
        createBiTree(T->rchild);

    }
    return true;
}

int main()
{
    //int x;
    //LinkQueue Q;
    //InitQueue(Q);
    //EnQueue(Q, 5);
    //EnQueue(Q, 7);
    //EnQueue(Q, 9);
    //DeQueue(Q, x);
    //LinkNode* p = Q.front->next;
    //while (p != NULL)
    //{
    //    printf("%d\n",p->data);
    //    p = p->next;
    //}
    
    BiTree T;
    createBiTree(T);
    LevelOrder(T);

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

测试要完成如图
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshida_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值