【c++】简单实现二叉树

#include<iostream>
#include<queue>
using namespace std;
template<class T>
struct BinTreeNode//二叉树的节点
{
	BinTreeNode(const T& data)
	:_pLeft(NULL)
	, _pRight(NULL)
	, _data(data)
	{}
	BinTreeNode<T>* _pLeft;//左孩子
	BinTreeNode<T>* _pRight;//右孩子
	T _data;//数据域
};
	template<class T>

	class BinTree
	{
		typedef BinTreeNode<T> *pNode;
		typedef BinTreeNode<T> Node;
	public:
		BinTree()
			:_pRoot(NULL)
		{}
		BinTree(const T* array, size_t size, T invalid)
		{
			size_t index = 0;
			_CreateBinTree(_pRoot, array, size, index, invalid);


		}
		void PreOrder()//前序遍历
		{
			cout << "PreOrder:" << endl;
			_PreOrder(_pRoot);
			cout << endl;
		}

	
		
		void InOrder()//中序遍历
		{
			cout << "InOrder:" << endl;
			_InOrder(_pRoot);
			cout << endl;
		}
		void PostOrder()//后序遍历
		{
			cout << "PostOrder:" << endl;
			_PostOrder(_pRoot);
			cout << endl;
		}
		
		void LevelOrder()//层序遍历
		{
			cout << "LevelOrder:" << endl;
			_LevelOrder(_pRoot);
			cout << endl;
		}
		
		size_t Size()//节点个数
		{
			return _Size(_pRoot);
		}
		
		size_t GetLeafCount()//叶节点个数
		{
			
			return _GetLeafCount(_pRoot);
			
		}
		size_t Height()//树的高度
		{
			return _Height(_pRoot);
		}
		
		size_t GetKLefCount(size_t K)//第k层节点个数
		{
			return _GetKLefCount(_pRoot, K);
		}
		
		pNode Find(const T &data)//查找函数
		{
			return _Find(_pRoot, data);
		}
		
		pNode Parent(pNode pnode)//双亲节点

		{
			return _Parent(_pRoot, pnode);
		}
		
		pNode LeftChild(pNode pnode)//左孩子节点
		{
			return (NULL == pnode) ? NULL : pnode->_pLeft;
		}
		pNode RightChild(pNode pnode)//右孩子节点
		{
			return (NULL == pnode) ? NULL : pnode->_pRight;
		}
	BinTree(const BinTree<T> & bt)//拷贝构造函数
	{
		_pRoot = _CopyBinTree(bt);
	}
	
	BinTree<T>& operator=(const BinTree<T>& bt)//赋值运算符重载

	{
		if (this != &bt)
		{
			_DestroyBinTree(_pRoot);
			_pRoot = _CopyBinTree(bt, _pRoot);
			return *this;
		}
	}
	
	
	~BinTree()
	{
		_DestroyBinTree(_pRoot);
	}

private:
	void _CreateBinTree(pNode &pRoot, const T *array, size_t size, size_t &index, const T&  invalid)//建立树
	{
		if (index<size&&'#'!= array[index])
		{
			pRoot = new Node(array[index]);
			
			_CreateBinTree(pRoot->_pLeft, array, size, ++index, invalid);
			_CreateBinTree(pRoot->_pRight, array, size, ++index, invalid);
		
		}
	}

	void _DestroyBinTree(pNode& pRoot)//销毁树
	{
		if (pRoot)
		{
			_DestroyBinTree(pRoot->_pLeft);
			_DestroyBinTree(pRoot->_pRight);
			delete pRoot;
			pRoot = NULL;
		}
	}
	void _PreOrder(pNode pRoot)
	{
		if (pRoot)
		{
			cout << pRoot->_data << " ";
			_PreOrder(pRoot->_pLeft);
			_PreOrder(pRoot->_pRight);

		}
	}
	void  _InOrder(pNode pRoot)
	{
		if (pRoot)
		{
			_InOrder(pRoot->_pLeft);
			cout << pRoot->_data << " ";
			_InOrder(pRoot->_pRight);

		}
	}
	void _PostOrder(pNode pRoot)
	{
		if (pRoot)
		{
			_PostOrder(pRoot->_pLeft);
			_PostOrder(pRoot->_pRight);
			cout << pRoot->_data << " ";
		}

	}
	void _LevelOrder(pNode Root)
	{
		if (NULL == _pRoot)
		{
			return;

		}
		queue<pNode> q;
		q.push(_pRoot);
		while (!q.empty())
		{
			pNode pCur = q.front();
			cout << pCur->_data << " ";
			if (pCur->_pLeft)
			{
				q.push(pCur->_pLeft);
			}
			if (pCur->_pRight)
			{
				q.push(pCur->_pRight);
			}
			q.pop();
		}
	}
	size_t _GetKLefCount(pNode pRoot, size_t K)
	{
		if (NULL == _pRoot || K < 1)
			return 0;
		if (1 == K)
			return 1;
		return _GetKLefCount(_pRoot->_pLeft, K - 1) + _GetKLefCount(_pRoot->_pRight, K - 1);
	}
	size_t _Size(pNode pRoot)
	{
		if (NULL == pRoot)
			return 0;
		return (_Size(pRoot->_pLeft) + _Size(pRoot->_pRight) + 1);
	}
	size_t _GetLeafCount(pNode pRoot)
	{
		if (NULL == pRoot)
		{
			return 0;
		}
		if (NULL == pRoot->_pLeft&& NULL == pRoot->_pRight)
		{
			return 1;
		}
		return _GetLeafCount(pRoot->_pLeft) + _GetLeafCount(pRoot->_pRight);
	}
	size_t _Height(pNode pRoot)
 	{
 		if (NULL == pRoot)
		{
			return 0;
		}
		if (NULL == pRoot->_pLeft&&NULL == pRoot->_pRight)
		{
 			return 1;
		}
		else
		{
			size_t LeftHeight = _Height(pRoot->_pLeft);
			size_t RightHeight = _Height(pRoot->_pRight);
			return LeftHeight >= RightHeight ? LeftHeight+1 : RightHeight+1;
		}
		
		

	}
	pNode _Find(pNode pRoot, const T& data)
	{
		if (NULL == pRoot)
			return NULL;
		if (pRoot->_data == data)
			return pRoot;
		pNode pRet;
		if ( pRet = _Find(pRoot->_pLeft,data))

			return pRet;

		else
			return _Find(pRoot->_pRight,data);
	}
	pNode _Parent(pNode pRoot, pNode pnode)
	{
		if (NULL == pnode || NULL == pRoot || pnode == pRoot)
			return NULL;
		if (pRoot->_pLeft == pnode || pRoot->_pRight == pnode)
			return pRoot;
		pNode parent = NULL;
		if (parent == _Parent(pRoot->_pLeft, pnode))
			return parent;
		return _Parent(pRoot->_pRight, pnode);
	}
	pNode _CopyBinTree(pNode pRoot)
	{
		pNode pNewRoot = NULL;
		if (pRoot)
		{
			pNewRoot = new pNode(pRoot->_data);
			if (pRoot->pLeft)
			{
				pNewRoot->pLeft = _CopyBinTree(pRoot->_pRight);

			}

		}
		return pNewRoot;
	}
	
private:
	pNode _pRoot;

};

void FunTest()
{
	char* pStr = "ABD###CE##F";
	BinTree<char> bt(pStr, strlen(pStr), '#');
	bt.PreOrder();
	bt.InOrder();
	bt.LevelOrder();
	bt.PostOrder();
	cout << "节点的个数" << endl;
	cout << bt.Size() << endl;
	cout << "第2层叶子节点个数" << endl;
	cout << bt.GetKLefCount(2) << endl;
	cout << "叶子节点个数" << endl;
	cout << bt.GetLeafCount() << endl;
	cout << "树的高度" << endl;
	cout << bt.Height()<< endl;
	BinTreeNode<char> *p = bt.Find('C');
	cout << "找到节点,输出节点数据:" << endl;
   	cout << p->_data << endl;
	cout << "输出左孩子" << endl;
	cout << bt.LeftChild(p)->_data << endl;
	cout << "输出右孩子" << endl;
	cout << bt.RightChild(p)->_data << endl;
	cout << "输出双亲" << endl;
	cout << bt.Parent(p)->_data << endl;
	
}
int main()
{
	FunTest();
	system("pause");
	return 0;
}


运行结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值