二叉树的线索化和遍历
2018-01-13 13:49
218 查看
包括二叉树的前序,中序,后序线索化和线索化遍历
#pragma once #include<iostream> using namespace std; enum pInfo{ LINK, THREAD }; template<class T> struct BinTreeThreadNode { BinTreeThreadNode(const T data) :_pLeft(NULL) , _pRight(NULL) , _pParent(NULL) , _data(data) , _pLeftThread(LINK) , _pRightThread(LINK) {} BinTreeThreadNode<T>* _pLeft; BinTreeThreadNode<T>* _pRight; BinTreeThreadNode<T>* _pParent; T _data; pInfo _pLeftThread; pInfo _pRightThread; }; template <class T> class BinTreeThread { typedef BinTreeThreadNode<T> Node; typedef Node* pNode; typedef void(*pFun)(pNode); public: BinTreeThread() :_pRoot(NULL) {} void CreateBinTree(T* arr, size_t size, T invalid) { size_t index = 0; _CreateBinTree(_pRoot, arr, size, index, invalid); } void PreVisit()//前序遍历 { _PreVisit(_pRoot, _PrintNode); } void InVisit()//中序遍历 { _InVisit(_pRoot, _PrintNode); } void PostVisit()//后序遍历 { _PostVisit(_pRoot, _PrintNode); } void LevelVisit()//层序遍历 { _LevelVisit(_pRoot, _PrintNode); } void PreThread() { pNode prev = NULL; _PreThread(_pRoot,prev); } void InThread() { pNode prev = NULL; _InThread(_pRoot,prev); } void PostThread() { pNode prev = NULL; _PostThread(_pRoot,prev); } void PreThreadVisit() { _PreThreadVisit(_pRoot,_PrintNode); } void InThreadVisit() { _InThreadVisit(_pRoot,_PrintNode); } void PostThreadVisit() { _PostThreadVisit(_pRoot,_PrintNode); } protected: void _CreateBinTree(pNode& root, T* arr, size_t size, size_t& index, T invalid) { if (index < size && invalid != arr[index]) { root = new Node(arr[index]); _CreateBinTree(root->_pLeft, arr, size, ++index, invalid); if (root->_pLeft) root->_pLeft->_pParent = root; _CreateBinTree(root->_pRight, arr, size, ++index, invalid); if (root->_pRight) root->_pRight->_pParent = root; } else root = NULL; } void _PreVisit(pNode& pd, pFun visit) { if (pd) { visit(pd); if (pd->_pLeft) _PreVisit(pd->_pLeft, visit); if (pd->_pRight) _PreVisit(pd->_pRight, visit); } 4000 } void _InVisit(pNode& pd, pFun visit) { if (pd) { if (pd->_pLeft) _InVisit(pd->_pLeft, visit); visit(pd); if (pd->_pRight) _InVisit(pd->_pRight, visit); } } void _PostVisit(pNode& pd, pFun visit) { if (pd) { if (pd->_pLeft) _PostVisit(pd->_pLeft, visit); if (pd->_pRight) _PostVisit(pd->_pRight, visit); visit(pd); } } void _LevelVisit(pNode& pd, pFun visit) { if (NULL == _pRoot) return; queue<pNode> q; pNode pCur = pd; q.push(pd); while (!q.empty()) { pCur = q.front(); visit(pCur); if (pCur->_pLeft) q.push(pCur->_pLeft); if (pCur->_pRight) q.push(pCur->_pRight); q.pop(); } } static void _PrintNode(pNode pd) { cout << pd->_data << "->"; } void _PreThread(pNode& pd, pNode& prev) { if (NULL == pd) return; //线索化当前节点 if (NULL == pd->_pLeft) { pd->_pLeft = prev; pd->_pLeftThread = THREAD; } if (prev && NULL == prev->_pRight) { prev->_pRight = pd; prev->_pRightThread = THREAD; } prev = pd; //线索化左子树 if (LINK == pd->_pLeftThread) _PreThread(pd->_pLeft,prev); //线索化右子树 if (LINK == pd->_pRightThread) _PreThread(pd->_pRight,prev); } void _InThread(pNode& pd, pNode& prev) { if (NULL == pd) return; _InThread(pd->_pLeft,prev); if (NULL == pd->_pLeft) { pd->_pLeft = prev; pd->_pLeftThread = THREAD; } if (prev && NULL == prev->_pRight) { prev->_pRight = pd; prev->_pRightThread = THREAD; } prev = pd; if (LINK == pd->_pRightThread) _InThread(pd->_pRight,prev); } void _PostThread(pNode& pd, pNode& prev) { if (NULL == pd) return; _PostThread(pd->_pLeft, prev); _PostThread(pd->_pRight, prev); if (NULL == pd->_pLeft) { pd->_pLeft = prev; pd->_pLeftThread = THREAD; } if (prev && NULL == prev->_pRight) { prev->_pRight = pd; prev->_pRightThread = THREAD; } prev = pd; } void _PreThreadVisit(pNode pd, pFun visit) { if (NULL == pd) return; pNode pCur = pd; while (pCur) { while (LINK == pCur->_pLeftThread) { visit(pCur); pCur = pCur->_pLeft; } visit(pCur); pCur = pCur->_pRight; } } void _InThreadVisit(pNode pd, pFun visit) { if (NULL == pd) return; pNode pCur = pd; while (pCur) { while (LINK == pCur->_pLeftThread) pCur = pCur->_pLeft; visit(pCur); while (pCur && THREAD == pCur->_pRightThread) { pCur = pCur->_pRight; visit(pCur); } pCur = pCur->_pRight; } } void _PostThreadVisit(pNode pd, pFun visit) { if (NULL == pd) return; pNode pCur = pd; pNode prev = NULL; while (pCur) { //找到最左边的节点 while (prev != pCur->_pLeft && LINK == pCur->_pLeftThread) pCur = pCur->_pLeft; while (pCur && THREAD == pCur->_pRightThread) { visit(pCur); prev = pCur; pCur = pCur->_pRight; } if (pCur == pd && (prev == pd->_pRight || NULL == pd->_pRight)) { visit(pCur); return; } while (pCur&& prev == pCur->_pRight) { visit(pCur); prev = pCur; pCur = pCur->_pParent; } if (pCur && LINK == pCur->_pRightThread) pCur = pCur->_pRight; } } private: pNode _pRoot; }; void TestBinTreeThread() { BinTreeThread<char> bt; char arr[] = "ABD###CE##F";/* A / \ B C / /\ D E F */ bt.CreateBinTree(arr, strlen(arr), '#');//创建树 //bt.PreVisit(); //bt.PreThread(); //cout << endl; /*bt.InVisit(); bt.InThread();*/ //bt.PostThread(); //bt.PreThreadVisit(); /*cout << endl; bt.InThreadVisit();*/ bt.PostVisit(); cout << endl; bt.PostThread(); bt.PostThreadVisit(); }
相关文章推荐
- 二叉树的构建,线索化,以及线索二叉树的遍历c++语言表示(《数据结构》算法6.5,6.6,6.7)
- C++二叉树线索化并遍历的示例代码
- 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)
- C++实现二叉树及其线索化和遍历
- 二叉树的线索化以及 线索化的先序,中序,后序遍历
- 二叉树的线索化以及遍历(前序、中序、后序)
- 二叉树线索化后 非递归遍历
- 不用栈和递归,实现线索化二叉树的遍历(还没人看的话就沉了吧)
- 二叉树线索化及其遍历方法
- 二叉树的线索化和遍历
- c++模板实现二叉树,线索化,线索化遍历,非递归遍历及一些基本操作
- 二叉树线索化以及线索化的先序、中序、后序遍历
- 创建一个二叉树,对该树线索化,对该树线索化遍历
- 二叉树线索化以及线索化前序、中序、后序遍历
- 二叉树的前序中序线索化及对对应线索二叉树的遍历
- 二叉树的前序、中序、后序线索化及遍历
- 二叉树的中序线索化以及遍历
- 二叉树线索化和遍历的算法
- 二叉树线索化、遍历的实现及其原理
- 线索化中序、先序、后序遍历二叉树的方式(待补充完整)