二叉樹(shù)是一種非線(xiàn)性結(jié)構(gòu),遍歷二叉樹(shù)幾乎都是通過(guò)遞歸或者用棧輔助實(shí)現(xiàn)非遞歸的遍歷。用二叉樹(shù)作為存儲(chǔ)結(jié)構(gòu)時(shí),取到一個(gè)節(jié)點(diǎn),只能獲取節(jié)點(diǎn)的左孩子和右孩子,不能直接得到節(jié)點(diǎn)的任一遍歷序列的前驅(qū)或者后繼。為了保存這種在遍歷中需要的信息,我們利用二叉樹(shù)中指向左右子樹(shù)的空指針來(lái)存放節(jié)點(diǎn)的前驅(qū)和后繼信息。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、成都微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了向陽(yáng)免費(fèi)建站歡迎大家使用!#include <iostream> using namespace std; enum PointerTag {THREAD, LINK}; template<class T> struct BinaryTreeNodeThd { T _data; //數(shù)據(jù) BinaryTreeNodeThd<T>* _left; //左孩子 BinaryTreeNodeThd<T>* _right; //右孩子 PointerTag _leftTag; //左孩子線(xiàn)索標(biāo)志 PointerTag _rightTag; //右孩子線(xiàn)索標(biāo)志 BinaryTreeNodeThd(const T& data) :_data(data) ,_left(NULL) ,_right(NULL) ,_leftTag(LINK) ,_rightTag(LINK) {} }; template<class T> class BinaryTreeThd { public: BinaryTreeThd(const T* array, size_t size, const T& invalid) { size_t index = 0; _root = _CreateTree(array, size, index, invalid); } ~BinaryTreeThd() { _DestroyTree(_root); _root = NULL; } void InOrderThreading() { BinaryTreeNodeThd<T>* prev = NULL; _InOrderThreading(_root, prev); } void PreOrderThreading() { BinaryTreeNodeThd<T>* prev = NULL; _PreOrderThreading(_root, prev); } void PostOrderThreading() { BinaryTreeNodeThd<T>* prev = NULL; _PostOrderThreading(_root, prev); } void PreOrderThd() { BinaryTreeNodeThd<T>* cur = _root; while (cur) { while (cur && LINK == cur->_leftTag) { cout<<cur->_data<<" "; cur = cur->_left; } cout<<cur->_data<<" "; cur = cur->_right; } cout<<endl; } void InOrderThd() { BinaryTreeNodeThd<T>* cur = _root; while (cur) { while (cur && LINK == cur->_leftTag) { cur = cur->_left; } cout<<cur->_data<<" "; while (THREAD == cur->_rightTag) { cur = cur->_right; cout<<cur->_data<<" "; } cur = cur->_right; } cout<<endl; } protected: BinaryTreeNodeThd<T>* _CreateTree(const T* array, size_t size, size_t& index, const T& invalid) { BinaryTreeNodeThd<T>* root = NULL; if (index < size && array[index] != invalid) { root = new BinaryTreeNodeThd<T>(array[index]); root->_left = _CreateTree(array, size, ++index, invalid); root->_right = _CreateTree(array, size, ++index, invalid); } return root; } void _DestroyTree(BinaryTreeNodeThd<T>* root) { if (NULL == root) return; if (LINK == root->_leftTag) _DestroyTree(root->_left); if (LINK == root->_rightTag) _DestroyTree(root->_right); delete root; } void _PreOrderThreading(BinaryTreeNodeThd<T>* cur, BinaryTreeNodeThd<T>*& prev) { if (NULL == cur) return; if (NULL == cur->_left) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev && NULL == prev->_right) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; if (cur->_leftTag == LINK) _PreOrderThreading(cur->_left, prev); if (cur->_rightTag == LINK) _PreOrderThreading(cur->_right, prev); } void _InOrderThreading(BinaryTreeNodeThd<T>* cur, BinaryTreeNodeThd<T>*& prev) { if (NULL == cur) return; _InOrderThreading(cur->_left, prev); if (NULL == cur->_left) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev && NULL == prev->_right) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; _InOrderThreading(cur->_right, prev); } void _PostOrderThreading(BinaryTreeNodeThd<T>* cur, BinaryTreeNodeThd<T>*& prev) { if (NULL == cur) return; _PostOrderThreading(cur->_left, prev); _PostOrderThreading(cur->_right, prev); if (cur->_left == NULL) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev && NULL == prev->_right) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; } protected: BinaryTreeNodeThd<T>* _root; }; void Test() { int a[] = {1, 2, 3, '#', '#', 4, '#', '#', 5, 6}; BinaryTreeThd<int> t1(a, sizeof(a)/sizeof(a[0]), '#'); t1.PreOrderThreading(); t1.PreOrderThd(); BinaryTreeThd<int> t2(a, sizeof(a)/sizeof(a[0]), '#'); t2.InOrderThreading(); t2.InOrderThd(); int a1[] = {1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8}; BinaryTreeThd<int> t3(a1, sizeof(a1)/sizeof(a1[0]), '#'); t3.PreOrderThreading(); t3.PreOrderThd(); } int main() { Test(); return 0; }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專(zhuān)業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買(mǎi)多久送多久。
文章標(biāo)題:C++線(xiàn)索化二叉樹(shù)-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://jinyejixie.com/article32/diodpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站制作、定制網(wǎng)站、定制開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容