线索二叉树的实现C++

一,简介

二,代码

中序线索二叉树的实现

#include <iostream>

using namespace std;

typedef char ElemType;
typedef struct threadNode
{
    /* data */
    ElemType data;
    threadNode *lchild, *rchild;
    /*
    
        tag = 0 指针指向孩子
        tag = 1 线索指针
    */
    int ltag, rtag;
} threadNode, *threadTree;

//中序遍历二叉树,一遍遍历一遍线索化
void MidVisTheradTree(threadTree node, threadTree &pre)
{
    if (node != NULL)
    {
        MidVisTheradTree(node->lchild, pre); //中序遍历左子树
        cout << "==============================" << endl;
        cout << "\t当前结点为: " << node->data << endl;
        if (node->lchild == NULL)
        { //左子树为空建立前驱线索
            node->lchild = pre;
            node->ltag = 1; //表示这是有左前驱线索节点
            if (node->lchild)
                cout << "\t当前结点: " << node->data << "左孩子为空 "
                     << "前驱是: " << node->lchild->data << endl;
        }
        //ABD#G##E##CF###
        //pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
        if (pre != NULL && pre->rchild == NULL)
        {

            pre->rchild = node;
            pre->rtag = 1;
            if (pre->rchild)
                cout << "\t" << pre->data << "结点右孩子为空 "
                     << "后继结点为:" << pre->rchild->data << endl;
        }
        cout << "==============================" << endl;
        //更新pre结点
        pre = node;
        MidVisTheradTree(node->rchild, pre); //中序遍历右子树
    }
}
//建立中序线索二叉树
void CreateMidThreadTree(threadTree node)
{
    //当前节点的前驱
    threadTree pre = NULL;
    if (node != NULL)
    {
        MidVisTheradTree(node, pre);
        if (pre->rchild == NULL)
        {
            pre->rtag = 1;
        }
    }
}

//利用前序创建二叉树
void built_Tree(threadTree &t)
{
    char ch;
    cin >> ch;
    if (ch == '#')
    {
        t = NULL;
        return;
    }
    else
    {
        t = new threadNode;
        t->data = ch;
        t->lchild = t->rchild = NULL;
        t->ltag = t->rtag = 0;
    }
    built_Tree(t->lchild);
    built_Tree(t->rchild);
}

int main()
{
    threadTree T;
    built_Tree(T);
    CreateMidThreadTree(T);
    system("pause");
    return 0;
}

三,样例

利用前序创建二叉树
输入样例 ABD#G##E##CF###
中序遍历:#D #G# B #E# A #F# C#(#表示没有建立线索二叉树时结点左or右指针为空)

在这里插入图片描述

四,先序和后序

后序,先序和中序差不多,都是利用遍历的方式,只不过先序有个坑

后序
void MidVisTheradTree(threadTree node, threadTree &pre)
{
    if (node != NULL)
    {
        MidVisTheradTree(node->lchild, pre); //中序遍历左子树
        MidVisTheradTree(node->rchild, pre); //中序遍历右子树
        cout << "==============================" << endl;
        cout << "\t当前结点为: " << node->data << endl;
        if (node->lchild == NULL)
        { //左子树为空建立前驱线索
            node->lchild = pre;
            node->ltag = 1; //表示这是有左前驱线索节点
            if (node->lchild)
                cout << "\t当前结点: " << node->data << "左孩子为空 "
                     << "前驱是: " << node->lchild->data << endl;
        }
        //ABD#G##E##CF###
        //pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
        if (pre != NULL && pre->rchild == NULL)
        {

            pre->rchild = node;
            pre->rtag = 1;
            if (pre->rchild)
                cout << "\t" << pre->data << "结点右孩子为空 "
                     << "后继结点为:" << pre->rchild->data << endl;
        }
        cout << "==============================" << endl;
        //更新pre结点
        pre = node;
    }
}
先序要注意
void MidVisTheradTree(threadTree node, threadTree &pre)
{
    if (node != NULL)
    {
        cout << "==============================" << endl;
        cout << "\t当前结点为: " << node->data << endl;
        if (node->lchild == NULL)
        { //左子树为空建立前驱线索
            node->lchild = pre;
            node->ltag = 1; //表示这是有左前驱线索节点
            if (node->lchild)
                cout << "\t当前结点: " << node->data << "左孩子为空 "
                     << "前驱是: " << node->lchild->data << endl;
        }
        //ABD#G##E##CF###
        //pre节点为node的前驱,如果pre的右孩子是空的,让pre节点指向pre的后继结点node
        if (pre != NULL && pre->rchild == NULL)
        {

            pre->rchild = node;
            pre->rtag = 1;
            if (pre->rchild)
                cout << "\t" << pre->data << "结点右孩子为空 "
                     << "后继结点为:" << pre->rchild->data << endl;
        }
        cout << "==============================" << endl;
        //更新pre结点
        pre = node;
        if(node->ltag == 0)//这是我的儿子我才处理
        	MidVisTheradTree(node->lchild, pre); //中序遍历左子树
        MidVisTheradTree(node->rchild, pre); //中序遍历右子树
    }
}

由于先序的特性是先拜访根结点,假设当前结点D,没有左孩子,前驱结点为pre,左孩子为D;
那么我们处理的D结点,由于D没有左孩子,在处理后会建立线索,左孩子的线索是指向它的前驱结点,也就是pre, 现在处理完了D结点,按照先序遍历就应该处理D的左孩子了,根据前面的描述,现在D的左孩子由原来的空节点变成了一个线索指针,指向它的前驱结点pre,D的左孩子就不为空了;
如果我们还是按照原来的做法,那么现在由于D的左孩子指向D的前驱pre,那么我们接下来就处理pre,pre的左孩子又是D这样我们就陷入了一个循环
所以我们根据tag标识来判断这是线索指针还是他孩子的指针

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值