二叉樹是一種非線性結(jié)構(gòu),遍歷二叉樹幾乎都是通過遞歸或者用棧輔助實(shí)現(xiàn)非遞歸的遍歷。用二叉樹作為存儲(chǔ)結(jié)構(gòu)時(shí),取到一個(gè)節(jié)點(diǎn),只能獲取節(jié)點(diǎn)的左孩子和右孩子,不能直接得到節(jié)點(diǎn)的任一遍歷序列的前驅(qū)或者后繼。為了保存這種在遍歷中需要的信息,我們利用二叉樹中指向左右子樹的空指針來存放節(jié)點(diǎn)的前驅(qū)和后繼信息。
10年的玉山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整玉山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“玉山網(wǎng)站設(shè)計(jì)”,“玉山網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
#includeusing namespace std; enum PointerTag {THREAD, LINK}; template struct BinaryTreeNodeThd { T _data; //數(shù)據(jù) BinaryTreeNodeThd * _left; //左孩子 BinaryTreeNodeThd * _right; //右孩子 PointerTag _leftTag; //左孩子線索標(biāo)志 PointerTag _rightTag; //右孩子線索標(biāo)志 BinaryTreeNodeThd(const T& data) :_data(data) ,_left(NULL) ,_right(NULL) ,_leftTag(LINK) ,_rightTag(LINK) {} }; template 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 * prev = NULL; _InOrderThreading(_root, prev); } void PreOrderThreading() { BinaryTreeNodeThd * prev = NULL; _PreOrderThreading(_root, prev); } void PostOrderThreading() { BinaryTreeNodeThd * prev = NULL; _PostOrderThreading(_root, prev); } void PreOrderThd() { BinaryTreeNodeThd * cur = _root; while (cur) { while (cur && LINK == cur->_leftTag) { cout< _data<<" "; cur = cur->_left; } cout< _data<<" "; cur = cur->_right; } cout< * cur = _root; while (cur) { while (cur && LINK == cur->_leftTag) { cur = cur->_left; } cout< _data<<" "; while (THREAD == cur->_rightTag) { cur = cur->_right; cout< _data<<" "; } cur = cur->_right; } cout< * _CreateTree(const T* array, size_t size, size_t& index, const T& invalid) { BinaryTreeNodeThd * root = NULL; if (index < size && array[index] != invalid) { root = new BinaryTreeNodeThd (array[index]); root->_left = _CreateTree(array, size, ++index, invalid); root->_right = _CreateTree(array, size, ++index, invalid); } return root; } void _DestroyTree(BinaryTreeNodeThd * root) { if (NULL == root) return; if (LINK == root->_leftTag) _DestroyTree(root->_left); if (LINK == root->_rightTag) _DestroyTree(root->_right); delete root; } void _PreOrderThreading(BinaryTreeNodeThd * cur, BinaryTreeNodeThd *& 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 * cur, BinaryTreeNodeThd *& 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 * cur, BinaryTreeNodeThd *& 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 * _root; }; void Test() { int a[] = {1, 2, 3, '#', '#', 4, '#', '#', 5, 6}; BinaryTreeThd t1(a, sizeof(a)/sizeof(a[0]), '#'); t1.PreOrderThreading(); t1.PreOrderThd(); BinaryTreeThd t2(a, sizeof(a)/sizeof(a[0]), '#'); t2.InOrderThreading(); t2.InOrderThd(); int a1[] = {1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8}; BinaryTreeThd t3(a1, sizeof(a1)/sizeof(a1[0]), '#'); t3.PreOrderThreading(); t3.PreOrderThd(); } int main() { Test(); return 0; }