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

#include 
#include 
#include 

using namespace std;

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

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

};

template 
BinaryNode::BinaryNode()
{
	data = 0;
	lchild = NULL;
	rchild = NULL;
}

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


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

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

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

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

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

//析构
template 
BinaryTree::~BinaryTree()
{
	ClearBiTree(_root);
}

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

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

		root = new BinaryNode();

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

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

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

	delete tmp;
	tmp = NULL;
}

//非递归遍历

//1 前序遍历
template 
void BinaryTree::PreVisitBiTree()
{
	BinaryNode* cur = _root;
	stack *> 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 *top = biTreeStack.top();
		biTreeStack.pop();
		cur = top->rchild;
		cur->data;
	}
	cout << endl;
}

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

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


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

	stack *> biTreeStack;

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

		BinaryNode *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 *tree = new BinaryTree(preList, 12, 0, 0);
	tree->PreVisitBiTree();
	tree->MidVisitBiTree();
	tree->LastVisitBiTree();
	delete tree;

	return 0;
}

你可能感兴趣的:(c/c++)