| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 468 人关注过本帖
标题:帖一个《二叉树线索化》程序,前几天才写的,还没查错!
只看楼主 加入收藏
zglcx123
Rank: 2
等 级:论坛游民
帖 子:60
专家分:10
注 册:2007-7-2
收藏
 问题点数:0 回复次数:0 
帖一个《二叉树线索化》程序,前几天才写的,还没查错!
前几天才写完,没查错,写成模板了,帮我看看程序  在线索化的理解上有无错误。

对了今天到百度搜以前我的各种帖子,才发现我有很久没来这个论坛了(以前发的帖都是2007年的),sorry!!!
今天心血来潮,发下贴

程序代码:
/*******************************************************

 *

 *                    二叉树线索化

 *

 *                        by Bghost

 *                

 *                        2010-06-24

 *

 *

 *

 *******************************************************/

template<class T>
class node
{
public:
    T value;
    int lTag;                //0为节点,  1为线索
    node *left;
    int rTag;
    node *right;
};


template<class T>
class BinaryTreeThread
{
private:
    node<T> *buffer;
    node<T> *pre;

private:
    node<T>* createNode();                            //创建节点
    void visitNode(node<T> *one);                    //处理程序
    void preThread(node<T> *p);                        //前序线索辅助
    void inThread(node<T> *p);                        //中序线索辅助
    void postThread(node<T> *p);                    //后序线索辅助
public:
    BinaryTreeThread();
    ~BinaryTreeThread();
    
    void createTree();                                //创建树

    void preOrderThread();                            //前序线索化
    void preOrderTraverse();                        //前序线索化遍历
    
    void inOrderThread();                            //中序线索化
    void inOrderTraverse();                            //中序线索化遍历

    void postOrderThread();                            //后序线索化
    void postOrderTraverse();                        //后序线索化遍历

};

template<class T>
BinaryTreeThread<T>::BinaryTreeThread()
{
    buffer = new node<T>();
    buffer->lTag = 0;
    buffer->rTag = 1;
    buffer->left = NULL;
    buffer->right = buffer;
}

template<class T>
BinaryTreeThread<T>::~BinaryTreeThread()
{

}

template<class T>
node<T>* BinaryTreeThread<T>::createNode()
{
    T val;
    cin>>val;
    node<T> *now;
    if(val == '*')
    {
        now = NULL;
    }else
    {
        now = new node<T>();
        now->value = val;
        now->left = createNode();
        now->right = createNode();
    }
    return now;
}

template<class T>
void BinaryTreeThread<T>::createTree()
{
    buffer->left = createNode();
    buffer->right = buffer;
}

template<class T>
void BinaryTreeThread<T>::visitNode(node<T> *one)
{
    cout<<one->value;
}



template<class T>
void BinaryTreeThread<T>::preThread(node<T> *p)
{
    if(p != NULL)
    {
        if(p->left == NULL)
        {
            p->lTag = 1;
            p->left = pre;
        }
        if(pre->right == NULL)
        {
            pre->rTag = 1;
            pre->right = p;
        }
        pre = p;
        preThread(p->left);
        preThread(p->right);
    }

}

template<class T>
void BinaryTreeThread<T>::preOrderThread()
{

    node<T> *p = buffer;
    pre = NULL;

    p->lTag = 0;    //连接
    p->rTag = 1;    //线索
    p->right = p;
    if(buffer->left == NULL)
    {//无root节点
        p->left = p;
    }else
    {
        p->left = buffer->left;
        pre = p;
        p = p->left;
        preThread(p);
        buffer->right = pre;
        buffer->rTag = 1;
        pre->right = buffer;
        pre->rTag = 1;
    }
}


template<class T>
void BinaryTreeThread<T>::preOrderTraverse()
{
    node<T> *p = buffer->left;
    while(p != buffer)
    {
        visitNode(p);
        while(p->lTag == 0)
        {
            p = p->left;
            visitNode(p);
        }
        
        p = p->right;

    }
}

template<class T>
void BinaryTreeThread<T>::inThread(node<T> *p)
{
    if(p != NULL)
    {
        inThread(p->left);
        if(p->left == NULL)
        {
            p->lTag = 1;
            p->left = pre;
        }
        if(pre->right == NULL)
        {
            pre->rTag = 1;
            pre->right = p;
        }
        pre = p;
        inThread(p->right);
    }

}
template<class T>
void BinaryTreeThread<T>::inOrderThread()
{
    node<T> *p = buffer;
    pre = NULL;

    p->lTag = 0;    //连接
    p->rTag = 1;    //线索
    p->right = p;
    if(buffer->left == NULL)
    {//无root节点
        p->left = p;
    }else
    {
        p->left = buffer->left;
        pre = p;
        p = p->left;
        inThread(p);
        buffer->right = pre;
        buffer->rTag = 1;
        pre->right = buffer;
        pre->rTag = 1;
    }

}

template<class T>
void BinaryTreeThread<T>::inOrderTraverse()
{
    node<T> *p = buffer->left;
    while(p != buffer)
    {
        while(p->lTag == 0)
            p = p->left;
        visitNode(p);
        while(p->rTag == 1 && p->right != buffer)
        {
            p = p->right;
            visitNode(p);
        }
        p = p->right;
    }
}

template<class T>
void BinaryTreeThread<T>::postThread(node<T> *p)
{
    if(p != NULL)
    {
        postThread(p->left);
        if(p->left == NULL)
        {
            p->lTag = 1;
            p->left = pre;
        }
        if(pre->right == NULL)
        {
            pre->rTag = 1;
            pre->right = p;
        }
        postThread(p->right);
        pre = p;
        
    }
}

template<class T>
void BinaryTreeThread<T>::postOrderThread()
{
    node<T> *p = buffer;
    pre = NULL;

    p->lTag = 0;    //连接
    p->rTag = 1;    //线索
    p->right = p;
    if(buffer->left == NULL)
    {//无root节点
        p->left = p;
        p->right = p;
    }else
    {
        p->left = buffer->left;

        pre = p;
        p = p->left;
        postThread(p);
        buffer->right = pre;
        buffer->rTag = 1;
        pre->right = buffer;
        pre->rTag = 1;
    }
}


template<class T>
void BinaryTreeThread<T>::postOrderTraverse()
{
    node<T> *p = buffer->left;
    while(p != buffer)
    {
        while(p->lTag == 0)
            p = p->left;
        visitNode(p);

        p = p->right;

    }
}
搜索更多相关主题的帖子: 二叉树线索化 天才 
2010-06-27 20:37
快速回复:帖一个《二叉树线索化》程序,前几天才写的,还没查错!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025246 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved