# 【c++】c++模板类，实现二叉树非递归遍历

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

//结点类
template <class T>
class BinaryNode
{
public:
T data;
BinaryNode<T> *lchild;
BinaryNode<T> *rchild;

BinaryNode();
BinaryNode(T val);
private:

};

template <class T>
BinaryNode<T>::BinaryNode()
{
data = 0;
lchild = NULL;
rchild = NULL;
}

template <class T>
BinaryNode<T>::BinaryNode(T val)
{
data = val;
lchild = NULL;
rchild = NULL;
}

//二叉树类
template <class  T>
class BinaryTree
{
private:
BinaryNode<T> *_root;  //根结点

public:
BinaryTree();	//构造空结点
BinaryTree(const T preList[], const int size, int index, const T end);	//先序构造
~BinaryTree();

BinaryNode<T>* CreateBiTree(const T preList[], const int size, int &index, const T end);	//先序创建二叉树
void ClearBiTree(BinaryNode<T> *root);  //销毁二叉树
void PreVisitBiTree();	//先序遍历，非递归
void MidVisitBiTree();	//中序遍历，非递归
void LastVisitBiTree();	//后序遍历，非递归
};

//构造空树
template <class T>
BinaryTree<T>::BinaryTree()
{
_root = NULL;
}

//先序构造二叉树
template <class T>
BinaryTree<T>::BinaryTree(const T preList[], const int size, int index, const T end)
{
_root = CreateBiTree(preList, size, index, end);
}

//析构
template <class T>
BinaryTree<T>::~BinaryTree()
{
ClearBiTree(_root);
}

//先序创建二叉树
template <class T>
BinaryNode<T>* BinaryTree<T>::CreateBiTree(const T preList[],const int size, int &index,const T end)  //特别注意：index必须用引用，否则函数的两个++index将不会正常改变
{
BinaryNode<T>* root = NULL;

if((index < size) && (preList[index] != end))
{

root = new BinaryNode<T>();

root->data = preList[index];
root->lchild = CreateBiTree(preList, size, ++index, end);
root->rchild = CreateBiTree(preList, size, ++index, end);
}
return root;
}

//销毁二叉树
template <class T>
void BinaryTree<T>::ClearBiTree(BinaryNode<T>* root)
{
BinaryNode<T> *tmp = root;

if(tmp == NULL)
{
return;
}
ClearBiTree(tmp->lchild);
ClearBiTree(tmp->rchild);

delete tmp;
tmp = NULL;
}

//非递归遍历

//1 前序遍历
template <class T>
void BinaryTree<T>::PreVisitBiTree()
{
BinaryNode<T>* cur = _root;
stack<BinaryNode<T> *> biTreeStack;

if(cur == NULL)
{
cout << "NULL" << endl;
return;
}

while ((cur != NULL) || (!biTreeStack.empty()))
{
while (cur != NULL)
{
cout << cur->data << " ";
biTreeStack.push(cur);
cur = cur->lchild;
}
BinaryNode<T> *top = biTreeStack.top();
biTreeStack.pop();
cur = top->rchild;
cur->data;
}
cout << endl;
}

//2 中序遍历
template<class T>
void BinaryTree<T>::MidVisitBiTree()
{
BinaryNode<T> *cur = _root;
stack<BinaryNode<T> *> biTreeStack;

while ((cur != NULL) || (!biTreeStack.empty()))
{
while (cur != NULL)
{
biTreeStack.push(cur);
cur = cur->lchild;
}
BinaryNode<T> *top = biTreeStack.top();
cout << top->data << " ";
biTreeStack.pop();
cur = top->rchild;
}
cout << endl;
}

//后序遍历
template<class T>
void BinaryTree<T>::LastVisitBiTree()
{
BinaryNode<T> *cur = _root;
BinaryNode<T> *priview = NULL;

stack<BinaryNode<T> *> biTreeStack;

while ((cur != NULL) || (!biTreeStack.empty()))
{
while (cur != NULL)
{
biTreeStack.push(cur);
cur = cur->lchild;
}

BinaryNode<T> *top = biTreeStack.top();

if((top->rchild == NULL) || (priview == top->rchild))
{
cout << top->data << " ";
priview = top;
biTreeStack.pop();
}
else
{
cur = top->rchild;
}

}
cout << endl;
}

int main()
{
int preList[] = {1, 2 , 0, 22, 0, 0, 3, 0, 33, 0, 0};
//int preList[] = {1, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0};
BinaryTree<int> *tree = new BinaryTree<int>(preList, 12, 0, 0);
tree->PreVisitBiTree();
tree->MidVisitBiTree();
tree->LastVisitBiTree();
delete tree;

return 0;
}