线索二叉树三种遍历算法(前序中序后序)

本文介绍了线索二叉树的前序、中序和后序遍历算法,特别是后序线索二叉树的实现,涉及到parent指针域的处理。
摘要由CSDN通过智能技术生成

前序和中序线索二叉树的实现:

#include <iostream>
using namespace std;

struct node
{
    int data;//数据域
    node*left;//左子树
    node*right;//右指针
    bool ltag;//左标志 1: youxiansuo 0:muyou
    bool rtag;//右标志
};

class binaryTree
{
public:
    node *root;
    binaryTree()
    {
        root=NULL;
    }
    void creatTree(node *&r);
    void inThread(node *&r,node *&pre);
    void in();
    void showIn();

    void preThread(node *&r,node *&pre);
    void pre();
    void showPre();
};

void binaryTree::creatTree(node *&r)
{
    int temp;
    cin>>temp;
    if(temp==0)
        r=NULL;
    else
    {
        node *p=new node;
        p->data=temp;
        r=p;
        r->ltag=0;
        r->rtag=0;
        r->left=NULL;
        r->right=NULL;
        creatTree(r->left);
        creatTree(r->right);
    }
}

void binaryTree::inThread(node *&r,node *&pre)
{
    if(r==NULL)
        return;
    else
    {
        if(!r->ltag)
            inThread(r->left,pre);
        if(r->left==NULL)
        {
            r->left=pre;
            r->ltag=1;
        }
        if(pre&&!pre->right)
        {
            pre->rtag=1;
            pre->right=r;
        }
        pre=r;
        if(!r->rtag)
            inThread(r->right,pre);
    }
}

void binaryTree::in()
{
    node *pre=NULL;
    inThread(root,pre);
    if(pre->right==NULL)
        pre->rtag=1;
}

void binaryTree::showIn()
{
    node*r=root;
    while(r->left)
        r=r->left;
    while(r->right)
    {
        cout<<r->data<<" ";
        if(!r->rtag)
        {
            node*p=r->right;
            while(!p->ltag)
                p=p->left;
            r=p;
        }
        else
            r=r->right;
    }
    cout<<r->data;
}

void binaryTree::preThread(node *&r,node *&pre)
{
    if(r==NULL)return;
    else
    {
        if(!r->left)
        {
            r->left=pre;
            r->ltag=1;
        }
        if(pre&&!pre->right)
        {
            pre->right=r;
            pre->rtag=1;
        }
        pre=r;
        if(!r->ltag)
            preThread(r->left,pre);
        if(!r->rtag)
            preThread(r->right,pre);
    }
}

void binaryTree::pre()
{
    node *pre=NULL;
    preThread(root,pre);
    if(pre->right==NULL)
        pre->rtag=1;
}

void binaryTree::showPre()
{
    node *r=root;
    while(r!=NULL)
    {
        cout<<r->data<<" ";
        if(r->rtag==1)
        {
            r=r->right;
        }
        else
        {
            if(!r->ltag)
                r=r->left;
            else
                r=r->right;
        }
    }
}

int main()
{
    binaryTree hehe;
    //输入时候要注意先序输入并且最后要补全0 如 1 2 3 则输入为1 2 0 0 3 0 0
    hehe.creatTree(hehe.root);
   // hehe.in();
  //  hehe.showIn();
  hehe.pre();
  hehe.showPre();
    return 0;
}


后序线索二叉树比前两者多一个parent指针域,在遍历时分情况讨论即可。
后序线索二叉树的实现:

#include <iostream>
using namespace std;

struct thrNode
{
    int data;//数据域
    thrNode*left;//左子树
    thrNode*right;//右指针
    thrNode*parent;//父节点指针
    bool ltag;//左标志 1: youxiansuo 0:muyou
    bool rtag;//右标志
};

class thrThreadTree
{
public:
    thrNode *root;
    thrThreadTree()
    {
        root=NULL;
    }
    void creatTree(thrNode *&r);

    void postThread(thrNode *&r,thrNode *&pre);

    void post();
    void showPost();
};

void thrThreadTree::creatTree(thrNode *&r)
{
    int temp;
    cin>>temp;
    if(temp==0)
        r=NULL;
    else
    {
        thrNode *p=new thrNode;
        p->data=temp;
        r=p;
        r->ltag=0;
        r->rtag=0;
        r->left=NULL;
        r->right=NULL;
        r->parent=NULL;
        creatTree(r->left);
        creatTree(r->right);
        if (r->left!=NULL)
        {
            r->left->parent=r;
        }
        if (r->right!=NULL)
        {
            r->right->parent=r;
        }
    }
}

void thrThreadTree::postThread(thrNode *&r,thrNode *&pre)
{
    if(r==NULL)
        return;
    else
    {
        if(!r->ltag)
            postThread(r->left,pre);
        if(!r->rtag)
            postThread(r->right,pre);
        if(r->left==NULL)
        {
            r->left=pre;
            r->ltag=1;
        }
        if(pre&&!pre->right)
        {
            pre->rtag=1;
            pre->right=r;
        }
        pre=r;
    }
}

void thrThreadTree::post()
{
    thrNode *pre=NULL;
    postThread(root,pre);
    if(pre->right==NULL)
        pre->rtag=1;
}

void thrThreadTree::showPost()
{
    if(root!=NULL)
    {
        thrNode *cur=root;
        while(!cur->ltag || !cur->rtag)
        {
            if(!cur->ltag)
                cur=cur->left;
            else
                cur=cur->right;
        }
        while(cur!=NULL)
        {
            cout<<cur->data<<" ";
            thrNode *pt=cur->parent;
            if(cur->rtag)
            {
                cur=cur->right;
            }
            else if(cur==root)
            {
                cur=NULL;
            }
            else if(pt->right==cur || (pt->left==cur && pt->rtag))
            {
                cur=pt;
            }
            else
            {
                cur=pt->right;
                while(!cur->ltag || !cur->rtag)
                {
                    if(!cur->ltag)
                        cur=cur->left;
                    else
                        cur=cur->right;
                }
            }
        }
    }
}
//for test
int main()
{
    thrThreadTree hehe;
    //输入时候要注意先序输入并且最后要补全0 如 1 2 3 则输入为1 2 0 0 3 0 0
    //example 1 0 2 3 5 0 0 0 4 0 0
    hehe.creatTree(hehe.root);
    hehe.post();
    hehe.showPost();
    return 0;
}



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值