二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。为了保存这种在遍历中需要的信息,我们利用二叉树中指向左右子树的空指针来存放节点的前驱和后继信息。
10年的玉山网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整玉山建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“玉山网站设计”,“玉山网站推广”以来,每个客户项目都认真落实执行。
#include <iostream> using namespace std; enum PointerTag {THREAD, LINK}; template<class T> struct BinaryTreeNodeThd { T _data; //数据 BinaryTreeNodeThd<T>* _left; //左孩子 BinaryTreeNodeThd<T>* _right; //右孩子 PointerTag _leftTag; //左孩子线索标志 PointerTag _rightTag; //右孩子线索标志 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; }
分享标题:C++线索化二叉树
本文链接:https://www.cdcxhl.com/article30/ghdiso.html
成都网站建设公司_创新互联,为您提供网站设计、云服务器、静态网站、网页设计公司、做网站、服务器托管
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联