线索化二叉树
将二叉树变为线索二叉树的过程称为线索化。按某种次序将二叉树线索化的实质是:按该次序遍历二叉树,在遍历过程中用线索取代空指针。
代码:
#pragma once enum PointTag{ LINK, THREAD, }; template<class T> struct BinaryTreeNode{ T _data; BinaryTreeNode<T> *_left; BinaryTreeNode<T> *_right; PointTag _lefttag; PointTag _righttag; BinaryTreeNode(const T&data) :_data(data) , _left(NULL) , _right(NULL) , _lefttag(LINK) , _righttag(LINK) {} }; template<class T> class BinaryTreeThread{ typedef BinaryTreeNode<T> Node; public: BinaryTreeThread() :_root(NULL) {} BinaryTreeThread(const T*a, size_t size,const T&invalid) :_root(NULL) { size_t index = 0; _root = _Create(a,size,invalid,index); } void PrevOrder() { _PrevOrder(_root); } ~BinaryTreeThread() { _Clear(_root); } void PrevOrderThread() { Node *prev = NULL; _PrevOrderThread(_root,prev); _Prev_Print(_root); } void InOrderThread() { Node *prev = NULL; _InOrderThread(_root, prev); _In_Print(_root); } void PostOrderThread() { Node *prev = NULL; _PostOrderThread(_root, prev); } private: Node* _Create(const T*a, size_t size, const T&invalid, size_t &index) { if (a == NULL) { return NULL; } if ((index < size) && (a[index] != invalid)) { Node *cur = new Node(a[index]); cur->_left = _Create(a, size, invalid, ++index); cur->_right = _Create(a, size, invalid, ++index); return cur; } return NULL; } void _PrevOrderThread(Node* root,Node* prev) { if (root == NULL) { return; } if (root->_left == NULL) { root->_lefttag = THREAD; root->_left = prev; } if (prev&&prev->_right == NULL) { root->_righttag = THREAD; root->_right = root; } prev = root; if (root->_lefttag == LINK) { _PrevOrderThread(root->_left, prev); } if (root->_righttag == LINK) { _PrevOrderThread(root->_right, prev); } } void _Prev_Print(Node *root) { if (root == NULL) { return; } cout << root->_data<<" "; if (root->_lefttag == LINK) { _Prev_Print(root->_left); } if (root->_righttag == LINK) { _Prev_Print(root->_right); } } void _InOrderThread(Node* root, Node *prev) { if (root == NULL) { return; } if (root->_lefttag == LINK) { _InOrderThread(root->_left, prev); } if (root->_left == NULL) { root->_lefttag = THREAD; root->_left = prev; } if (prev&&prev->_right == NULL) { prev->_righttag = THREAD; prev->_right = root; } prev = root; if (root->_righttag == LINK) { _InOrderThread(root->_right, prev); } /*if (root != NULL) { _InOrderThread(_root->_left, prev); if (root->_left == NULL) { root->_lefttag = THREAD; root->_left = prev; } if (prev != NULL&&prev->_right == NULL) { prev->_righttag = THREAD; prev->_right = root; } prev = root; _InOrderThread(_root->_right) }*/ } void _In_Print(Node *root) { if (root == NULL) { return; } if (root->_lefttag == LINK) { _Prev_Print(root->_left); } cout << root->_data<<" "; if (root->_righttag == LINK) { _Prev_Print(root->_right); } } void _PrevOrder(Node *root) { if (root == NULL) { cout << ‘#‘ << " "; return; } cout << root->_data<<" "; _PrevOrder(root->_left); _PrevOrder(root->_right); } void _Clear(Node*_root) { if (_root != NULL) { Node *left = _root->_left; Node *right = _root->_right; delete _root; _root = NULL; _Clear(left); _Clear(right); } } private: Node *_root; }; void reviewtest() { int a[] = { 1, 2, 3, ‘#‘, ‘#‘, 4, ‘#‘, ‘#‘, 5, 6 }; //int a[] = { 1, 2, ‘#‘, 3, ‘#‘, ‘#‘, 4, 5, ‘#‘, 6, ‘#‘, 7, ‘#‘, ‘#‘, 8 }; BinaryTreeThread<int> btt(a,sizeof(a)/sizeof(int),‘#‘); btt.PrevOrder(); btt.PrevOrderThread(); btt.InOrderThread(); cout << "123" << endl; }
本文出自 “学习记录” 博客,转载请与作者联系!
原文地址:http://10794428.blog.51cto.com/10784428/1832267