数据结构学习笔记(三) 树形结构之遍历对称序线索二叉树

以下是前序、对称序以及后序遍历对称序线索二叉树的算法。
用于遍历的二叉树如下图:
这里写图片描述

一、前序遍历对称序线索二叉树。

#include<iostream>
#define MaxSize 10
using namespace std;

//变量定义与声明
typedef char datatype;
typedef struct node
{
    datatype data;
    struct node *lchild,*rchild;
    int ltag,rtag;
}ThrBTnode,*ThrBinTree;
typedef struct
{
    ThrBTnode* S[MaxSize];
    int top;
}SeqStack;
SeqStack ST;
ThrBinTree root;
ThrBTnode* nodes[10];

//置空栈
void ClearStack(SeqStack &ST)
{
    ST.top=-1;
}

//栈的推入
void push(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top>=MaxSize-1)
        cout<<"overflow!"<<endl;
    else
        ST.S[++ST.top]=p;
}

//栈的弹出
ThrBTnode* pop(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top==-1)
        cout<<"underflow!"<<endl;
    else
        p=ST.S[ST.top--];
    return p;
}

//判栈空否
int StackEmpty(SeqStack ST)
{
    if(ST.top==-1)
        return 1;
    else
        return 0;
}

//构建一棵二叉树
ThrBinTree Create(ThrBinTree root)
{
    ThrBTnode *A=new ThrBTnode;
    ThrBTnode *B=new ThrBTnode;
    ThrBTnode *C=new ThrBTnode;
    ThrBTnode *D=new ThrBTnode;
    ThrBTnode *E=new ThrBTnode;
    ThrBTnode *F=new ThrBTnode;
    ThrBTnode *G=new ThrBTnode;
    ThrBTnode *H=new ThrBTnode;
    ThrBTnode *I=new ThrBTnode;
    ThrBTnode *J=new ThrBTnode;

    A->data='A';
    B->data='B';
    C->data='C';
    D->data='D';
    E->data='E';
    F->data='F';
    G->data='G';
    H->data='H';
    I->data='I';
    J->data='J';

    A->lchild=B;
    A->rchild=C;
    A->ltag=A->rtag=0;

    B->lchild=D;
    B->rchild=E;
    B->ltag=B->rtag=0;

    E->lchild=H;
    E->rchild=I;
    E->ltag=E->rtag=0;

    C->lchild=F;
    C->rchild=G;
    C->ltag=C->rtag=0;

    F->lchild=NULL;
    F->rchild=J;
    F->ltag=F->rtag=0;

    D->lchild=D->rchild=NULL;
    D->ltag=D->rtag=0;

    H->lchild=H->rchild=NULL;
    H->ltag=H->rtag=0;

    I->lchild=I->rchild=NULL;
    I->ltag=I->rtag=0;

    J->lchild=J->rchild=NULL;
    J->ltag=J->rtag=0;

    G->lchild=G->rchild=NULL;
    G->ltag=G->rtag=0;

    root=A;

    nodes[0]=A;
    nodes[1]=B;
    nodes[2]=C;
    nodes[3]=D;
    nodes[4]=E;
    nodes[5]=F;
    nodes[6]=G;
    nodes[7]=H;
    nodes[8]=I;
    nodes[9]=J;

    return root;
}

//线索化二叉树
void inOrderThreadTree(ThrBinTree root)
{
    ThrBTnode *pre,*p;
    ClearStack(ST);
    p=root;
    pre=NULL;
    while(p!=NULL||!StackEmpty(ST))
    {
        if(p!=NULL)
        {
            push(ST,p);
            p=p->lchild;
        }
        else
        {
            p=pop(ST,p);
            if(p->lchild==NULL)
            {
                p->lchild=pre;
                p->ltag=1;
            }
            if(pre!=NULL&&pre->rchild==NULL)
            {
                pre->rchild=p;
                pre->rtag=1;
            }
            pre=p;
            p=p->rchild;
        }
    }
    if(pre!=NULL)
            pre->rtag=1;
}

//找指定结点的前序后继
ThrBTnode* FindPreSuccessor(ThrBTnode *p)
{
    ThrBTnode *q;
    if(p->ltag==0)
    {
        q=p->lchild;
        return q;
    }else{
        q=p;
    }
    while(q->rtag==1&&q->rchild!=NULL)
        q=q->rchild;
    q=q->rchild;
    return q;
}

//前序遍历对称序线索二叉树
void PreOrderTraThrTree(ThrBinTree root)
{
    ThrBTnode *p=root;
    cout<<p->data<<" ";
    while(p->rchild!=NULL)
    {
        p=FindPreSuccessor(p);
        cout<<p->data<<" ";
    }
    cout<<endl;
}

//测试函数
int main()
{
    root=Create(root);
    inOrderThreadTree(root);
    cout<<"前序遍历对称序线索树结果为:"<<endl;
    PreOrderTraThrTree(root);
    return 0;
}

二、对称序遍历对称序线索二叉树。

#include<iostream>
#define MaxSize 10
using namespace std;

//变量定义与声明
typedef char datatype;
typedef struct node
{
    datatype data;
    struct node *lchild,*rchild;
    int ltag,rtag;
}ThrBTnode,*ThrBinTree;
typedef struct
{
    ThrBTnode* S[MaxSize];
    int top;
}SeqStack;
SeqStack ST;
ThrBinTree root;
ThrBTnode* nodes[10];

//置空栈
void ClearStack(SeqStack &ST)
{
    ST.top=-1;
}

//栈的推入
void push(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top>=MaxSize-1)
        cout<<"overflow!"<<endl;
    else
        ST.S[++ST.top]=p;
}

//栈的弹出
ThrBTnode* pop(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top==-1)
        cout<<"underflow!"<<endl;
    else
        p=ST.S[ST.top--];
    return p;
}

//判栈空否
int StackEmpty(SeqStack ST)
{
    if(ST.top==-1)
        return 1;
    else
        return 0;
}

//构建一棵二叉树
ThrBinTree Create(ThrBinTree root)
{
    ThrBTnode *A=new ThrBTnode;
    ThrBTnode *B=new ThrBTnode;
    ThrBTnode *C=new ThrBTnode;
    ThrBTnode *D=new ThrBTnode;
    ThrBTnode *E=new ThrBTnode;
    ThrBTnode *F=new ThrBTnode;
    ThrBTnode *G=new ThrBTnode;
    ThrBTnode *H=new ThrBTnode;
    ThrBTnode *I=new ThrBTnode;
    ThrBTnode *J=new ThrBTnode;

    A->data='A';
    B->data='B';
    C->data='C';
    D->data='D';
    E->data='E';
    F->data='F';
    G->data='G';
    H->data='H';
    I->data='I';
    J->data='J';

    A->lchild=B;
    A->rchild=C;
    A->ltag=A->rtag=0;

    B->lchild=D;
    B->rchild=E;
    B->ltag=B->rtag=0;

    E->lchild=H;
    E->rchild=I;
    E->ltag=E->rtag=0;

    C->lchild=F;
    C->rchild=G;
    C->ltag=C->rtag=0;

    F->lchild=NULL;
    F->rchild=J;
    F->ltag=F->rtag=0;

    D->lchild=D->rchild=NULL;
    D->ltag=D->rtag=0;

    H->lchild=H->rchild=NULL;
    H->ltag=H->rtag=0;

    I->lchild=I->rchild=NULL;
    I->ltag=I->rtag=0;

    J->lchild=J->rchild=NULL;
    J->ltag=J->rtag=0;

    G->lchild=G->rchild=NULL;
    G->ltag=G->rtag=0;

    root=A;

    nodes[0]=A;
    nodes[1]=B;
    nodes[2]=C;
    nodes[3]=D;
    nodes[4]=E;
    nodes[5]=F;
    nodes[6]=G;
    nodes[7]=H;
    nodes[8]=I;
    nodes[9]=J;

    return root;
}

//线索化二叉树
void inOrderThreadTree(ThrBinTree root)
{
    ThrBTnode *pre,*p;
    ClearStack(ST);
    p=root;
    pre=NULL;
    while(p!=NULL||!StackEmpty(ST))
    {
        if(p!=NULL)
        {
            push(ST,p);
            p=p->lchild;
        }
        else
        {
            p=pop(ST,p);
            if(p->lchild==NULL)
            {
                p->lchild=pre;
                p->ltag=1;
            }
            if(pre!=NULL&&pre->rchild==NULL)
            {
                pre->rchild=p;
                pre->rtag=1;
            }
            pre=p;
            p=p->rchild;
        }
    }
    if(pre!=NULL)
            pre->rtag=1;
}

//对称序遍历对称序线索树
void inOrderTraThrTree(ThrBinTree root)
{
    ThrBTnode *p=root;
    while(p!=NULL)
    {
        while(p->ltag==0)
            p=p->lchild;
        cout<<p->data<<" ";
        while(p->rtag==1&&p->rchild!=NULL)
        {
            p=p->rchild;
            cout<<p->data<<" ";
        }
        p=p->rchild;
    }
}

//逐个结点打印线索关系
void Print(ThrBinTree root)
{
    for(int i=0;i<10;i++)
    {
        cout<<"对于结点"<<nodes[i]->data<<"有:"<<endl;
        if((nodes[i]->lchild==NULL)||(nodes[i]->rchild==NULL))
        {
            if(nodes[i]->lchild==NULL&&nodes[i]->rchild==NULL)
            {
                cout<<"lchild为:  NULL"<<endl;
                cout<<"rchild为:  NULL"<<endl;
            }
            else if(nodes[i]->rchild==NULL&&nodes[i]->lchild!=NULL)
            {
                cout<<"lchild为:  "<<nodes[i]->lchild->data<<endl;
                cout<<"rchild为:  NULL"<<endl;
            }
            else if(nodes[i]->lchild==NULL&&nodes[i]->rchild!=NULL)
            {
                cout<<"lchild为:  NULL"<<endl;
                cout<<"rchild为:  "<<nodes[i]->rchild->data<<endl;
            }
            cout<<"ltag为:  "<<nodes[i]->ltag<<endl;
            cout<<"rtag为:  "<<nodes[i]->rtag<<endl;
            continue;
        }
        cout<<"lchild为:  "<<nodes[i]->lchild->data<<endl;
        cout<<"rchild为:  "<<nodes[i]->rchild->data<<endl;
        cout<<"ltag为:  "<<nodes[i]->ltag<<endl;
        cout<<"rtag为:  "<<nodes[i]->rtag<<endl;
    }
}

//测试函数
int main()
{
    root=Create(root);
    cout<<"对称序线索化二叉树结果为:"<<endl;
    inOrderThreadTree(root);
    Print(root);
    cout<<"对称序遍历对称序线索树结果为:"<<endl;
    inOrderTraThrTree(root);
    return 0;
}

三、后序遍历对称序线索二叉树。

#include<iostream>
#define MaxSize 10
using namespace std;

//变量定义与声明
typedef char datatype;
typedef struct node
{
    datatype data;
    struct node *lchild,*rchild;
    int ltag,rtag;
}ThrBTnode,*ThrBinTree;
typedef struct
{
    ThrBTnode* S[MaxSize];
    int top;
}SeqStack;
SeqStack ST,STPost;
ThrBinTree root;
ThrBTnode* nodes[10];

//置空栈
void ClearStack(SeqStack &ST)
{
    ST.top=-1;
}

//栈的推入
void push(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top>=MaxSize-1)
        cout<<"overflow!"<<endl;
    else
        ST.S[++ST.top]=p;
}

//栈的弹出
ThrBTnode* pop(SeqStack &ST,ThrBTnode *p)
{
    if(ST.top==-1)
        cout<<"underflow!"<<endl;
    else
        p=ST.S[ST.top--];
    return p;
}

//判栈空否
int StackEmpty(SeqStack ST)
{
    if(ST.top==-1)
        return 1;
    else
        return 0;
}

//构建一棵二叉树
ThrBinTree Create(ThrBinTree root)
{
    ThrBTnode *A=new ThrBTnode;
    ThrBTnode *B=new ThrBTnode;
    ThrBTnode *C=new ThrBTnode;
    ThrBTnode *D=new ThrBTnode;
    ThrBTnode *E=new ThrBTnode;
    ThrBTnode *F=new ThrBTnode;
    ThrBTnode *G=new ThrBTnode;
    ThrBTnode *H=new ThrBTnode;
    ThrBTnode *I=new ThrBTnode;
    ThrBTnode *J=new ThrBTnode;

    A->data='A';
    B->data='B';
    C->data='C';
    D->data='D';
    E->data='E';
    F->data='F';
    G->data='G';
    H->data='H';
    I->data='I';
    J->data='J';

    A->lchild=B;
    A->rchild=C;
    A->ltag=A->rtag=0;

    B->lchild=D;
    B->rchild=E;
    B->ltag=B->rtag=0;

    E->lchild=H;
    E->rchild=I;
    E->ltag=E->rtag=0;

    C->lchild=F;
    C->rchild=G;
    C->ltag=C->rtag=0;

    F->lchild=NULL;
    F->rchild=J;
    F->ltag=F->rtag=0;

    D->lchild=D->rchild=NULL;
    D->ltag=D->rtag=0;

    H->lchild=H->rchild=NULL;
    H->ltag=H->rtag=0;

    I->lchild=I->rchild=NULL;
    I->ltag=I->rtag=0;

    J->lchild=J->rchild=NULL;
    J->ltag=J->rtag=0;

    G->lchild=G->rchild=NULL;
    G->ltag=G->rtag=0;

    root=A;

    nodes[0]=A;
    nodes[1]=B;
    nodes[2]=C;
    nodes[3]=D;
    nodes[4]=E;
    nodes[5]=F;
    nodes[6]=G;
    nodes[7]=H;
    nodes[8]=I;
    nodes[9]=J;

    return root;
}

//线索化二叉树
void inOrderThreadTree(ThrBinTree root)
{
    ThrBTnode *pre,*p;
    ClearStack(ST);
    p=root;
    pre=NULL;
    while(p!=NULL||!StackEmpty(ST))
    {
        if(p!=NULL)
        {
            push(ST,p);
            p=p->lchild;
        }
        else
        {
            p=pop(ST,p);
            if(p->lchild==NULL)
            {
                p->lchild=pre;
                p->ltag=1;
            }
            if(pre!=NULL&&pre->rchild==NULL)
            {
                pre->rchild=p;
                pre->rtag=1;
            }
            pre=p;
            p=p->rchild;
        }
    }
    if(pre!=NULL)
            pre->rtag=1;
}

//找指定结点的后序前驱
ThrBTnode* FindPostPrecursor(ThrBTnode *p)
{
    ThrBTnode *q;
    if(p->rtag==0)
    {
        q=p->rchild;
        return q;
    }else{
        q=p;
    }
    while(q->ltag==1&&q->lchild!=NULL)
        q=q->lchild;
    q=q->lchild;
    return q;
}

//后序遍历对称序线索二叉树
void PostOrderTraThrTree(ThrBinTree root)
{
    ClearStack(STPost);
    ThrBTnode *p=root;
    push(STPost,p);
    while(p->lchild!=NULL)
    {
        p=FindPostPrecursor(p);
        push(STPost,p);
    }
    while(!StackEmpty(STPost))
        cout<<pop(STPost,p)->data<<" ";
    cout<<endl;
}

//测试函数
int main()
{
    root=Create(root);
    inOrderThreadTree(root);
    cout<<"后序遍历对称序线索树结果为:"<<endl;
    PostOrderTraThrTree(root);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值