C++线索化二叉树-创新互联
二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。为了保存这种在遍历中需要的信息,我们利用二叉树中指向左右子树的空指针来存放节点的前驱和后继信息。
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了向阳免费建站欢迎大家使用!#includeusing namespace std; enum PointerTag {THREAD, LINK}; template struct BinaryTreeNodeThd { T _data; //数据 BinaryTreeNodeThd * _left; //左孩子 BinaryTreeNodeThd * _right; //右孩子 PointerTag _leftTag; //左孩子线索标志 PointerTag _rightTag; //右孩子线索标志 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; }
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文标题:C++线索化二叉树-创新互联
分享路径:http://azwzsj.com/article/diodpc.html