二叉树三种遍历(递归及非递归)的实现

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

template<class T>
struct TreeNode
{
	T _value;
	TreeNode<T>* _firstChild;
	TreeNode<T>* _nextBorther; 
};
template<class T>
struct BinaryTreeNode
{
	T _value;
	BinaryTreeNode<T>* _left;
	BinaryTreeNode<T>* _right;
	BinaryTreeNode(const T& value)
		:_value(value)
		,_left(NULL)
		,_right(NULL)
	{}
};
template<class T>
class Binarytree
{
public:
	Binarytree()
		:_root(NULL)
	{}
	Binarytree(char* str)
	{
		_CreatTree(_root, str);
	}
	Binarytree(BinaryTreeNode<T>& t);
	Binarytree& operator=(BinaryTreeNode<T>& t);
	~Binarytree()
	{}
	void _CreatTree(BinaryTreeNode<T>*& root, char*& str)
	{
		if (*str != '#'&& *str != '\0')
		{
			root = new BinaryTreeNode<T>(*str);
			_CreatTree(root->_left, ++str);
			if (*str == '\0')
			{
				return;
			}
			_CreatTree(root->_right, ++str);
		}
	}
	void PrevOrder()
	{
		_PrevOrder(_root);
		cout << endl;
	}
	void PrevOrder_NonR()
	{
		stack<BinaryTreeNode<T>*> Stack;
		if (_root)
		{
			Stack.push(_root);
		}
		while (!Stack.empty())
		{
			BinaryTreeNode<T>* ret = Stack.top();
				cout << ret->_value << " ";
				Stack.pop();
				if (ret->_right)
					Stack.push(ret->_right);
				if (ret->_left)
					Stack.push(ret->_left);
		}
		cout << endl;
	}
	void InOrder_NonR()
	{
		stack<BinaryTreeNode<T>*> Stack;
		BinaryTreeNode<T>* ret = _root;
		while (ret || !Stack.empty())
		{
			while (ret)
			{
				Stack.push(ret);
				ret = ret->_left;
			}
			if (!Stack.empty())
			{
				BinaryTreeNode<T>* Top = Stack.top();
				cout << Top->_value << " ";
				Stack.pop();
				if (Top->_right)
				{
					ret = Top->_right;
				}
			}
		}
		cout << endl;
	}
	void PostOrder_NonR()
	{
		stack<BinaryTreeNode<T>*> Stack;
		BinaryTreeNode<T>* ret = _root;
		BinaryTreeNode<T>* visNode = NULL;
		while (ret || !Stack.empty())
		{
			while (ret)
			{
				Stack.push(ret);
				ret = ret->_left;
			}
			BinaryTreeNode<T>* Top = Stack.top();
			if (Top->_right == NULL||Top->_right==visNode)
			{
				Stack.pop();
				cout << Top->_value << " ";
				visNode = Top;
			}
			else
			{
				ret = Top->_right;
			}
		}
		cout << endl;
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	void PostOrder()
	{
		_PostOrder(_root);
		cout << endl;
	}
	void LevelOrder()
	{
		_LevelOrder(_root);
		cout << endl;
	}
	int Size()
	{
		return _Size(_root);
	}
	int Depth()
	{
		return _Depth(_root);
	}
protected:
	int _Size(BinaryTreeNode<T>* root)
	{
		if (root == NULL)
		{
			return 0;
		}
		if (root->_left == NULL && root->_right == NULL)
		{
			return 1;
		}
		return 1 + (_Size(root->_left) + _Size(root->_right));
	}
	int _Depth(BinaryTreeNode<T>* root)
	{
		if (root == NULL)
		{
			return 0;
		}
		int leftDepth = _Depth(root->_left);
		int rightDepth = _Depth(root->_right);
		return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
	}
	void _PrevOrder(BinaryTreeNode<T>* root)
	{
		if (root != NULL)
		{
			cout << root->_value << " ";
			_PrevOrder(root->_left);
			_PrevOrder(root->_right);
		}
	}
	void _InOrder(BinaryTreeNode<T>* root)
	{
		if (root != NULL)
		{
			_InOrder(root->_left);
			cout << root->_value << " ";
			_InOrder(root->_right);
		}
	}
	void _PostOrder(BinaryTreeNode<T>* root)
	{
		if (root)
		{
			_PostOrder(root->_left);
			_PostOrder(root->_right);
			cout << root->_value << " ";
		}
	}
	void _LevelOrder(BinaryTreeNode<T>* root)
	{
		queue<BinaryTreeNode<T>*> q;
		if (root)
		{
			q.push(root);
		}
		while (!q.empty())
		{
			BinaryTreeNode<T>* front = q.front();
			cout << front->_value << " ";
			q.pop();
			if (front->_left)
				q.push(front->_left);
			if (front->_right)
				q.push(front->_right);
		}
	}
private:
	BinaryTreeNode<T>* _root;
};
void Test1()
{
	char* str = "123##4##56";
	Binarytree<char> bt1(str);
	bt1.PrevOrder();
	bt1.LevelOrder();
	bt1.PostOrder();
	bt1.InOrder();
	bt1.PrevOrder_NonR();
	bt1.InOrder_NonR();
	bt1.PostOrder_NonR();
	cout << "bt1.Size:" << bt1.Size() << endl;
	cout << "bt1.Depth:" << bt1.Depth() << endl;
}
int main()
{
	Test1();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值