二叉树的创建 、前序、中序、后序、层序(层序二层从左遍历下一层从右遍历)遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cell_KEY/article/details/52638352
#include<iostream>
using namespace std;
#include<stack>
#include<queue>
template<typename T>
struct BinaryTreeNode
{

	BinaryTreeNode* pLeft;
	BinaryTreeNode* pRight;
	T data;
	BinaryTreeNode(const T& _data)
		:pLeft(NULL)
		, pRight(NULL)
		, data(_data)
	{}
};
template <typename T>
class BinaryTree
{
public:
	/*BinaryTree()
	: _pRoot(NULL)
	{
	}*/
	BinaryTree(T *arr, int size)
	{
		  size_t ildx = 0;
		  CreatTree(_pRoot, arr, size, ildx);
	}
	BinaryTree(const BinaryTree<T>&s)
	{
		_pRoot = CopyTree(s._pRoot);
	}
	BinaryTree<T>& operator=(const BinaryTree<T>&s)
	{
		if (this != &s)
		{
			DistoryTree(_pRoot);
			_pRoot = CopyTree(s._pRoot);
		}
		return *this;
	}
	~BinaryTree()
	{
		DistoryTree(_pRoot);
		cout << "~BinaryTree()" << endl;
	}
	void PreOderTree()
	{
		cout << "preoder:" << endl;
		PreOrder(_pRoot);
		cout << endl;
	}
	void MidOderTree()
	{
		cout << "midoder:" << endl;
		MidOrder(_pRoot);
		cout << endl;
	}
	void PreOrderTreeNor()
	{
		cout << "preorder:" << endl;
		PreOder1(_pRoot);
		cout <<endl;
	}
	void LevelOrder()
	{
		cout << "levelorder" << endl;
		_LevelOrder(_pRoot);
		cout << endl;

	}
	//层序遍历要求 一行是从左打印下一行是从右打印
	void LevelOrderLR()
	{
		cout << "leveloder一行从左打印下一行从右打印" << endl;
		_LevelOrderLR(_pRoot);
		cout << endl;
	}

private:
	//先序遍历 递归
  void PreOrder(BinaryTreeNode<T>* pRoot)
	{

		if (pRoot)
		{
			cout << pRoot->data << " ";
			PreOrder(pRoot->pLeft);
			PreOrder(pRoot->pRight);
		}
	}
  //先序遍历  循环
  void PreOder1(BinaryTreeNode<T>* pRoot)
  {
	  stack<BinaryTreeNode<T>*> s;
	  if (pRoot)
	  {
		  s.push(pRoot);
	  }
	  while (!s.empty())
	  {
		  BinaryTreeNode<T>* cur = s.top();
		  cout << cur->data<<" ";
		  s.pop();
		  if (cur->pRight)
		  {
			  s.push(cur->pRight);
		  }
		  if (cur->pLeft)
		  {                                                      
			  s.push(cur->pLeft);
		  }
	  }
  }
	//中序遍历 递归
	void MidOrder(BinaryTreeNode<T>* pRoot)
	{
		
		if (pRoot)
		{
			MidOrder(pRoot->pLeft);
			cout << pRoot->data << " ";
			MidOrder(pRoot->pRight);
		}
		
	}
	
	//层序遍历
	void _LevelOrder(BinaryTreeNode<T>* pRoot)
	{
		queue<BinaryTreeNode<T>*> q;
		if (pRoot)
		{
			q.push(pRoot);

		}
		while (!q.empty())
		{
			BinaryTreeNode<T>* cur = q.front();
			cout << cur->data<<" ";
			q.pop();
			if (cur->pLeft)
			{
				q.push(cur->pLeft);
			}
			if (cur->pRight)
			{
				q.push(cur->pRight);
			}
			
		}
	}
	//层序遍历要求 一行是从左打印下一行是从右打印
	void _LevelOrderLR(BinaryTreeNode<T>* pRoot)
	{
		stack<BinaryTreeNode<T>*> s1;
		stack<BinaryTreeNode<T>*> s2;
		s1.push(pRoot);
		while (!(s1.empty() && s2.empty()))
		{
			while (!s1.empty())
			{
				BinaryTreeNode<T>* cur1=s1.top();
				s1.pop();
				cout << cur1->data << " ";
				if (cur1->pRight)
					s2.push(cur1->pRight);
				if (cur1->pLeft)
					s2.push(cur1->pLeft);
			}
			while (!s2.empty())
			{
				BinaryTreeNode<T>*cur2 = s2.top();
				s2.pop();
				cout << cur2->data << " ";
				if (cur2->pLeft)
					s1.push(cur2->pLeft);
				if (cur2->pRight)
					s1.push(cur2->pRight);
			}
		}
	}

	void CreatTree(BinaryTreeNode<T>*& _pRoot, T *arr, size_t size, size_t &ildx)
	{
		if (ildx < size&&arr[ildx] != '#')
		{
			_pRoot = new BinaryTreeNode<T>(arr[ildx]);
			CreatTree(_pRoot->pLeft, arr, size, ++ildx);
			CreatTree(_pRoot->pRight, arr, size, ++ildx);
		}
	}
	BinaryTreeNode<T>* CopyTree(BinaryTreeNode<T>* pRoot)
	{
		BinaryTreeNode<T>* ret(0);
		if (pRoot)
		{
			ret = new BinaryTreeNode<T>(pRoot->data);
			ret->pLeft = CopyTree(pRoot->pLeft);
			ret->pRight = CopyTree(pRoot->pRight);
		}
		return ret;
	}
	void DistoryTree(BinaryTreeNode<T>* pRoot)
	{
		if (pRoot)
		{
			DistoryTree(pRoot->pLeft);
			DistoryTree(pRoot->pRight);
			delete pRoot;
			pRoot = NULL;
		}
	}
	BinaryTreeNode<T>* RebuildBinaryTree(const T pre[], size_t presize, const size_t& prestart, const T in[], const size_t insize,const size_t instart)
	{
		size_t indx=0
		while (indx < insize)
		{
			if (pre[indx] == in[indx])
			{
				break;
			}
			indx++;
		}
		if (indx >= insize)
		{
			return NULL;
		}
		BinaryTreeNode<T> * pRoot = new BinaryTreeNode<T>(pre[ildx]);
		if (ildx > instar)
		{
			pRoot->pLeft = RebuildBinaryTree();
		}
		if (ildx + 1 < insize)
		{
			pRoot->pRright = RebuildBinaryTree();
		}
	}
private:
	BinaryTreeNode<T>* _pRoot;
};

void FunTest()
{
	char arr[10] = { '0', '1', '3', '#', '#', '4', '#', '#', '2', '5' };
	char arr1[10] = { '0', '2', '4', '#', '#', '5', '#', '#', '3', '6' };
	BinaryTree<char>s1(arr, 10);
	BinaryTree<char>s3(arr1, 10);
	BinaryTree<char>s2(s1);
	s2 = s3;
	s1.PreOderTree();
	s1.PreOrderTreeNor();
	s1.MidOderTree();
	s1.LevelOrder();
	s1.LevelOrderLR();
}
int main()
{
	FunTest();
	system("pause");
	return 0;
}





猜你喜欢

转载自blog.csdn.net/Cell_KEY/article/details/52638352
今日推荐