线索二叉树

线索二叉树是一种通过额外的线索标志位来标识节点前驱和后继的二叉树结构。当节点的左或右指针为空时,它们会被用来指向遍历时的前驱或后继节点,但需要额外的标志来区分这些指针是否表示子节点关系。
摘要由CSDN通过智能技术生成

当某节点的左指针为空时,令该指针指向按照某种方式遍历二叉树时得到该节点的前驱结点;当某节点的右指针为空时,令该指针指向按照某种方式遍历二叉树时得到该节点的后继结点。但问题是无法区分:左指针指向的结点是左孩子结点还是前驱结点,右指针指向的结点是右孩子结点还是后继结点。因此需要增加两个线索标志位来区分这两种情况:

这里写图片描述

#pragma once

#include <iostream>
using namespace std;

template<class T>
struct Node 
{
    Node(const T& data)
        :_data(data)
        , _lChild(NULL)
        , _rChild(NULL)
        , _leftThread(false)
        , _rightThread(false)
    {}

    T _data;
    Node<T>* _lChild;
    Node<T>* _rChild;
    Node<T>* _Parent;
    bool _leftThread;
    bool _rightThread;
};

template<class T>
class ThreadBinary
{
    typedef Node<T> Node;

public:

    ThreadBinary()
        :_root(NULL)
        , _IsThread(false)
    {}

    ThreadBinary(const T array[], size_t size, const T invalue)
        :_IsThread(false)
    {
        size_t i = 0;
        CreateBinTreePreOrder(_root, array, size, i, invalue);
    }

    void PreThread()
    {
        Node* pPre = NULL;
        _IsThread = true;
        PreThread(_root, pPre);
    }

    void InThread()
    {
        Node* pPre = NULL;
        _IsThread = true;
        InThread(_root, pPre);
    }

    void PosThread()
    {
        Node* pPre = NULL;
        _IsThread = true;
        PosThread(_root, pPre);
    }

    void Preorder()
    {
        Node* pCur = _root;

        while (pCur)
        {
            while (!pCur->_leftThread)
            {
                cout << pCur->_data << ' ';
                pCur = pCur->_lChild;
            }
            cout << pCur->_data << ' ';
            pCur = pCur->_rChild;
        }
    }

    void InOrder()
    {
        Node* pCur = _root;
        Node* pPre = NULL;

        while (pCur)
        {
            if (pPre != pCur->_lChild)
            {
                while (!pCur->_leftThread)
                    pCur = pCur->_lChild;
            }

            cout << pCur->_data << ' ';
            pPre = pCur;

            while (pCur->_rChild && !pCur->_rightThread && pCur->_rChild->_leftThread)
            {
                pCur = pCur->_rChild;
                cout << pCur->_data << ' ';
            }

            pCur = pCur->_rChild;
        }
    }

    void PosOrder()
    {
        Node* pCur = _root;
        Node* pPre = NULL;

        while (pCur)
        {
            while (!pCur->_leftThread)
            `   pCur = pCur->_lChild;

        }
    }

    ~ThreadBinary()
    {
        if (_IsThread)
        {
            Destory1();
        }
        else
        {
            Destory2();
        }

    }

private:
    void Destory1()
    {
        if (_root)
        {

        }
    }

    void Destory2()
    {

    }

    void CreateBinTreePreOrder(Node*& root, const T array[], const  size_t size, size_t& i, const T& invalue)
    {
        if (i < size && array[i] != invalue)
        {
            root = new Node(array[i]);
            CreateBinTreePreOrder(root->_lChild, array, size, ++i, invalue);
            if (root->_lChild)
                root->_lChild->_Parent = root;

            CreateBinTreePreOrder(root->_rChild, array, size, ++i, invalue);
            if (root->_rChild)
                root->_rChild->_Parent = root;
        }
    }

    void PreThread(Node* root, Node*& pPre)
    {
        if (root)
        {
            if (NULL == root->_lChild)
            {
                root->_leftThread = true;
                root->_lChild = pPre;
            }

            if (pPre && NULL == pPre->_rChild)
            {
                pPre->_rightThread = true;
                pPre->_rChild = root;
            }

            pPre = root;

            if (!root->_leftThread)
            {
                PreThread(root->_lChild, pPre);
            }
            if (!root->_rightThread)
            {
                PreThread(root->_rChild, pPre);
            }
        }
    }

    void InThread(Node* root, Node*& pPre)
    {
        if (root)
        {
            InThread(root->_lChild, pPre);

            if (NULL == root->_lChild)
            {
                root->_leftThread = true;
                root->_lChild = pPre;
            }

            if (pPre && NULL == pPre->_rChild)
            {
                pPre->_rightThread = true;
                pPre->_rChild = root;
            }

            pPre = root;

            if (!root->_rightThread)
            {
                InThread(root->_rChild, pPre);
            }
        }
    }

    void PosThread(Node* root, Node*& pPre)
    {
        if (root)
        {
            PosThread(root->_lChild, pPre);
            PosThread(root->_rChild, pPre);

            if (NULL == root->_lChild)
            {
                root->_leftThread = true;
                root->_lChild = pPre;
            }

            if (pPre && NULL == pPre->_rChild)
            {
                pPre->_rightThread = true;
                pPre->_rChild = root;
            }

            pPre = root;
        }
    }

private:
    Node* _root;
    bool _IsThread;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值