王道c语言督学营课时14.2作业

#include <stdio.h>
#include <stdlib.h>
typedef char BiElemType;
typedef struct BiTNode{
    BiElemType c;//c就是data存储数据
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*Bitree;

//tag队列是辅助二叉树
typedef struct tag{
    Bitree p;//树的某一个结点地址
    struct tag *pnext;
}tag_t,*ptag_t;

//辅助队列来实现深度优先遍历
typedef Bitree ElemType;
typedef struct LinkNode{
    ElemType data;
    struct LinkNode *next;
}LinkNode;
typedef struct {
    LinkNode *front,*rear;//链表头;链表尾
}LinkQueue;
void InitQueue(LinkQueue &Q);
void EnQueue(LinkQueue &Q, ElemType x);
bool DeQueue(LinkQueue &Q, ElemType &x);
bool IsQueue(LinkQueue Q);

bool IsQueue(LinkQueue Q)
{
    return Q.front==Q.rear;
}
//初始化队列
void InitQueue(LinkQueue &Q)
{
    Q.front=Q.rear= (LinkNode*)malloc(sizeof (LinkNode));
}
//入队
void EnQueue(LinkQueue &Q,ElemType x)
{
    //由于队列空间可以无限申请故无需判队满
    LinkNode* pnew=(LinkNode*)malloc(sizeof (LinkNode));//定义队列新增值
    pnew->data=x;
    pnew->next=NULL;//队列结束的标志
    Q.rear->next=pnew;//rear指针始终指向新元素
    Q.rear=pnew;
}
//出队
bool DeQueue(LinkQueue &Q,ElemType &x)
{
    //判断队列是否为空
    if(Q.front==Q.rear)
    {
        return false;
    }



    LinkNode*p =Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x=p->data;
    Q.front->next=p->next;//断链
    if(p==Q.rear)//倘若就2个元素则需要让尾指针重新等于头指针
    {
        Q.rear=Q.front;
    }
    free(p);//释放掉空间
    return true;
}

//前序遍历 先序遍历 优先深度遍历
void preOrder(Bitree p)
{
    if(p!=NULL){
        printf("%c",p->c);
        preOrder(p->lchild);
        preOrder(p->rchild);
    }

}
//中序遍历
void InOrder(Bitree p)
{
    if(p!=NULL){
        InOrder(p->lchild);
        printf("%c",p->c);
        InOrder(p->rchild);
    }
}
//后序遍历
void PostOrder(Bitree p)
{
    if(p!=NULL){
        PostOrder(p->lchild);
        PostOrder(p->rchild);
        printf("%c",p->c);
    }
}
//广度遍历 层序遍历 层次遍历
void LevelOrder(Bitree p)
{
    LinkQueue Q;
    InitQueue(Q);//初始化队列
    Bitree tree;//接受返回值
    EnQueue(Q,p);//入队
    while (!IsQueue(Q))
    {
        DeQueue(Q,tree);
        printf("%c",tree->c);
        if(tree->lchild)
        {
            EnQueue(Q,tree->lchild);
        }
        if(tree->rchild)
        {
            EnQueue(Q,tree->rchild);
        }
    }
}
int main() {
    Bitree tree=NULL;//树根为零
    Bitree pnext;//定义新结点
    BiElemType c;//定义接受传入值
    ptag_t phead=NULL,ptail=NULL,pcur=NULL,listpnext;
    while (scanf("%c",&c))//abcdefghij
    {
        if(c=='\n')
        {
            break;//构造输入结束
        }
        pnext= (Bitree)calloc(1,sizeof (BiTNode));
        pnext->c=c;
        listpnext= (ptag_t)calloc(1,sizeof (tag_t));
        listpnext->p=pnext;
        if(tree==NULL)
        {
            tree=pnext;
            phead=listpnext;
            ptail=listpnext;
            pcur=listpnext;
        } else{
            ptail->pnext=listpnext;//尾指针向后移动
            ptail=listpnext;
            if(NULL==pcur->p->lchild)
            {
                pcur->p->lchild=pnext;//若左子树为空则向左子树赋值
            }
            else if(NULL==pcur->p->rchild)
            {
                pcur->p->rchild=pnext;//若右子树为空则向右子树赋值
                pcur=pcur->pnext;
            }
        }
    }

    InOrder(tree);
    printf("\n");
    PostOrder(tree);
    printf("\n");
    LevelOrder(tree);
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值