首页 > 其他 > 详细

二叉树线索化

时间:2016-04-20 21:46:39      阅读:107      评论:0      收藏:0      [点我收藏+]
#include using namespace std; typedef enum { LINK, THREAD }PointerTag; template struct BinaryTreeNodeThd { T _data; BinaryTreeNodeThd* _left; BinaryTreeNodeThd* _right; PointerTag _leftTag; PointerTag _rightTag; BinaryTreeNodeThd() : _left(NULL), _right(NULL), _leftTag(LINK), _rightTag(LINK) {} BinaryTreeNodeThd(const T&data) :_data(data), _left(NULL), _right(NULL), _leftTag(LINK), _rightTag(LINK) {} }; template class BinaryTreeThd { public: BinaryTreeThd() :_root(NULL) {} BinaryTreeThd(const T*a, size_t size) { size_t index = 0; _root = _CreateTree(a, size, index); } BinaryTreeThd(const BinaryTreeThd& t) { _root = _Copy(t._root); } void InOrderThreading() { BinaryTreeNodeThd* prev = NULL; _InOrderThreading(_root, prev); } void PrevOrderThreading() { BinaryTreeNodeThd* prev = NULL; _PrevOrderThreading(_root, prev); } void PostOrderThreading() { BinaryTreeNodeThd* prev = NULL; _PostOrderThreading(_root, prev); } public: void InOrderThd() { BinaryTreeNodeThd* cur=_root; while (cur) { while (cur->_leftTag == LINK) { cur = cur->_left; } cout<_data<<" "; while(cur&&cur->_rightTag == THREAD) { cur = cur->_right; cout << cur->_data; } cur = cur->_right; } cout << endl; } void PrevOrderThd() { BinaryTreeNodeThd *cur =_root; while (cur) { while (cur&&cur->_leftTag == LINK) { cout << cur->_data << " "; cur = cur->_left; } cout << cur->_data << " "; while (cur&&cur->_rightTag == THREAD) { cur = cur->_right; cout << cur->_data << " "; } if (cur->_leftTag == LINK) cur = cur->_left; else cur = cur->_right; } } protected: BinaryTreeNodeThd* _CreateTree(const T*a, size_t size, size_t &index) { BinaryTreeNodeThd* root = NULL; if (index < size&&a[index] != ‘#‘) { root = new BinaryTreeNodeThd(a[index]); root->_left = _CreateTree(a, size, ++index); root->_right = _CreateTree(a, size, ++index); } return root; } void _InOrderThreading(BinaryTreeNodeThd* cur, BinaryTreeNodeThd*& prev) { if (cur == NULL) return; _InOrderThreading(cur->_left, prev); if (cur->_left == NULL) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev&&prev->_rightTag == NULL) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; _InOrderThreading(cur->_right, prev); } void _PrevOrderThreading(BinaryTreeNodeThd *cur, BinaryTreeNodeThd*& prev) { if (cur == NULL) { return; } if (cur->_left == NULL) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev&&prev->_right== NULL) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; if (cur->_leftTag == LINK) { _PrevOrderThreading(cur->_left, prev); } if (cur->_rightTag == LINK) { _PrevOrderThreading(cur->_right, prev); } } void _PostOrderThreading(BinaryTreeNodeThd *cur, BinaryTreeNodeThd*& prev) { if (cur == NULL) { return; } _PostOrderThreading(cur->_left, prev); _PostOrderThreading(cur->_right, prev); if (cur->_left == NULL) { cur->_leftTag = THREAD; cur->_left = prev; } if (prev&&prev->_right == NULL) { prev->_rightTag = THREAD; prev->_right = cur; } prev = cur; } BinaryTreeNodeThd* _Copy(BinaryTreeNodeThd* root) { if (root == NULL) return NULL; BinaryTreeNodeThd* newRoot = new BinaryTreeNodeThd(root->_data); newRoot->_left = _Copy(root->_left); newRoot->_right = _Copy(root->_right); return newRoot; } /*BinaryTreeThd& operator=(BinaryTreeThd*& t) { if (this != &t) { this->_Destory(_root); _root = _Copy(t._root); } return *this; }*/ //BinaryTreeNodeThd& operator=(BinaryTreeNodeThd t) //{ // swap(_root, t._root); // return *this; //} protected: BinaryTreeNodeThd *_root; }; int main() { int a[] = { 1, 2, 3, ‘#‘, ‘#‘, 4, ‘#‘, ‘#‘, 5, 6 }; BinaryTreeThd t(a, 10); BinaryTreeThd temp; t.InOrderThd(); t.InOrderThreading(); //t.PrevOrderThreading(); //t.PostOrderThreading(); cout << endl; system("pause"); return 0; }

二叉树线索化

原文:http://www.cnblogs.com/yuanshuang/p/5414477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!