网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

数据结构之二叉树(前序中序后续线索话非递归方式)

节点:
enum LinkType
{
                 THREAD,
                 LINK
};

template
struct ThredBinaryNode
{
                 ThredBinaryNode *_left;
                 ThredBinaryNode *_right;
                 LinkType _left_tag;
                 LinkType _right_tag;

                 T _data;
                ThredBinaryNode( T data ) :_data(data), _left( NULL), _right(NULL ), _left_tag(LINK), _right_tag(LINK)
                {};
};
线索化二叉树为:
template
class BinaryTreeThred
{
                 typedef ThredBinaryNode  Node;
public:
                BinaryTreeThred( const T * a, size_t size, const T & valiue)
                {
                                 size_t index = 0;
                                _root = _CreatTree( a, size , index, valiue);
                }
private:

              Node *_root;

};

构造函数:
                 Node*_CreatTree(const T* a, size_t size, size_t &index, const T &valiue)
                {
                                 Node *root = NULL ;
                                 if (index < size&& a[index ] != valiue)
                                {
                                                root = new Node (a[index]);
                                                root->_left = _CreatTree(a, size , ++index, valiue);
                                                root->_right = _CreatTree(a, size , ++index, valiue);
                                }
                                 return root;
                }

中序线索化递归:

                 void _InThred(Node *cur, Node* & prev)//递归线索化
                {
                                 if (cur != NULL)
                                {

                                                _InThred( cur->_left, prev );
                                                 if (cur ->_left == NULL)
                                                {
                                                                 cur->_left_tag = THREAD ;
                                                                 cur->_left = prev ;
                                                }
                                                 if (prev != NULL&& prev->_right==NULL )
                                                {
                                                                 prev->_right_tag = THREAD ;
                                                                 prev->_right = cur ;
                                                }
                                                 prev = cur ;
                                                _InThred( cur->_right, prev );
                                }


                };

中序线索非递归:
                 void InThred_Nor()//非递归
                {
                                 stack s;
                                 Node *prev = NULL ;
                                 Node *cur = _root;
                                 while (cur||!s.empty())
                                {
                                                 while (cur)
                                                {
                                                                s.push(cur);
                                                                cur = cur->_left;
                                                };
                                                 Node *tmp = s.top();
                                                s.pop();
                                                 if (tmp->_left == NULL )
                                                {
                                                                tmp->_left = prev;
                                                                tmp->_left_tag = THREAD;
                                                }
                                                prev = tmp;
                                                 if (prev->_right == NULL &&!s.empty())
                                                {
                                                                tmp->_right = s.top();
                                                                tmp->_right_tag = THREAD;
                                                }
                                                 else
                                                {
                                                                cur = tmp->_right;
                                                }
                                }
                }



前序线化非递归:
void BinaryTreeThred ::PreThread() //前序线索化非递归
{
                 Node *pre = NULL ;
                 Node*cur = _root;
                 stack s;
                s.push(cur);
                 while (cur||!s.empty())
                {
                                 Node *tmp = NULL ;
                                 if (!s.empty())
                                {
                                                tmp = s.top();
                                }
                                 else
                                {
                                                 return;
                                }


                                s.pop();
                                 if (tmp->_right)
                                {
                                                s.push(tmp->_right);
                                }

                                 if (tmp->_left)
                                {
                                                s.push(tmp->_left);
                                }
                                 else//tmp->left==null
                                {
                                                tmp->_left_tag = THREAD;
                                                tmp->_left=pre;
                                }              
                                 if (pre != NULL &&pre->_right == NULL)
                                {
                                                pre->_right_tag = THREAD;
                                                pre->_right = tmp;
                                }
                                pre = tmp;
                }
}

后序列线索话非递归:

void BinaryTreeThred ::PostThread() //后续
{
                 Node *cur = _root;
                 stack s;
                 Node *prev = NULL ;
                 while (cur || !s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;//3
                                }
                                 Node *tmp = s.top();
                                 if (tmp->_right == NULL || tmp->_right == prev)
                                {
                                                 if (tmp->_left == NULL )
                                                {
                                                                tmp->_left_tag = THREAD;
                                                                tmp->_left = prev;
                                                }
                                                 if (prev != NULL &&prev->_right == NULL)
                                                {
                                                                prev->_right_tag = THREAD;
                                                                prev->_right = tmp;
                                                }
                                                s.pop();
                                                prev = tmp;
                                }
                                 else
                                {
                                                cur = tmp->_right;
                                }
                }

}

网页题目:数据结构之二叉树(前序中序后续线索话非递归方式)
文章起源:http://bjjierui.cn/article/jhjcie.html

其他资讯