91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

數據結構之二叉樹(前序 中序 后續線索話非遞歸方式)

發布時間:2020-07-23 15:35:06 來源:網絡 閱讀:703 作者:性感的玉米 欄目:編程語言
節點:
enum LinkType
{
                 THREAD,
                 LINK
};

template<class T>
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 T>
class BinaryTreeThred
{
                 typedef ThredBinaryNode <T> 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<Node *> 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 <T>::PreThread() //前序線索化非遞歸
{
                 Node *pre = NULL ;
                 Node*cur = _root;
                 stack<Node *> 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 <T>::PostThread() //后續
{
                 Node *cur = _root;
                 stack<Node *> 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;
                                }
                }

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宜川县| 五大连池市| 海南省| 陇南市| 衡水市| 静乐县| 东乡| 普宁市| 奉贤区| 宜君县| 巴东县| 社会| 华坪县| 池州市| 桃源县| 桓台县| 德昌县| 九江市| 五指山市| 龙海市| 万年县| 老河口市| 册亨县| 改则县| 米脂县| 民乐县| 尼木县| 北流市| 齐河县| 双辽市| 绍兴市| 邮箱| 忻城县| 巫溪县| 石首市| 巨鹿县| 昌吉市| 蕉岭县| 青冈县| 四川省| 静海县|