关于二叉树的功能有二叉树的创建和销毁,前序遍历,中序遍历,后续遍历,求二叉树中节点的个数,求二叉树的深度,查找二叉树中的某一个节点
#include<iostream>
using namespace std;
template<class T>
struct BinaryTreeNode
{
T _data;
BinaryTreeNode<T>* _LeftChild;
BinaryTreeNode<T>* _RightChild;
BinaryTreeNode(const T& x)
:_data(x)
, _LeftChild(NULL)
, _RightChild(NULL)
{}
};
template<class T>
class BinTree
{
public:
BinTree()
:_root(NULL)
{}
BinTree(const T* a, size_t size)
{
size_t index = 0;
_root=_CreateBinTree(a, size, index);
}
BinTree(const BinTree<T> &t)
{
_root = Copy(t._root);
}
BinTree<T>& operator=(BinTree<T> t)
{
swap(t._root, _root);
return *this;
}
~BinTree() //销毁二叉树
{
_Destroy(_root);
}
void _PreOrder() //前序遍历
{
PreOrder(_root);
}
void _InOrder() //中序遍历
{
InOrder(_root);
}
void _PostOrder() //后续遍历
{
PostOrder(_root);
}
void _LevelOrder() //层次遍历
{
LevelOrder(_root);
}
int _Size() //求二叉树节点的个数
{
return Size(_root);
}
int _Depth() //求二叉树的深度
{
return Depth(_root);
}
BinaryTreeNode<T>* _Find(const T& x) //查找二叉树中的某个节点
{
return Find(_root, x);
}
protected:
BinaryTreeNode<T>* _CreateBinTree(const T* a, size_t size, size_t& index)
{
BinaryTreeNode<T>* root = NULL;
if (index < size&&a[index] != '#')
{
root = new BinaryTreeNode<T>(a[index]);
root->_LeftChild=_CreateBinTree(a, size, ++index);
root->_RightChild=_CreateBinTree(a, size, ++index);
}
return root;
}
void _Destroy(BinaryTreeNode<T>*& root)
{
if (root == NULL)
return;
_Destroy(root->_LeftChild);
_Destroy(root->_RightChild);
delete root;
}
void PreOrder(BinaryTreeNode<T>* root) //递归
{
if (root == NULL)
return;
cout << root->_data << " ";
PreOrder(root->_LeftChild);
PreOrder(root->_RightChild);
}
void InOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
return;
InOrder(root->_LeftChild);
cout << root->_data << " ";
InOrder(root->_RightChild);
}
void PostOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
return;
InOrder(root->_LeftChild);
InOrder(root->_RightChild);
cout << root->_data << " ";
}
void LevelOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
return;
queue<BinaryTreeNode<T>*> q;
q.push(root);
while (!q.empty())
{
BinaryTreeNode<T> *cur = q.front();
cout << cur->_data << " ";
q.pop();
if (cur->_RightChild)
q.push(cur->_RightChild);
if (cur->_LeftChild)
q.push(cur->_LeftChild);
}
}
int Size(BinaryTreeNode<T>* root)
{
if (root == NULL)
return 0;
return Size(root->_LeftChild) + Size(root->_RightChild) + 1;
}
int Depth(BinaryTreeNode<T>* root)
{
if (root == NULL)
return 0;
int left = Depth(root->_LeftChild);
int right = Depth(root->_RightChild);
return (left > right ? left :right)+1;
}
BinaryTreeNode<T>* Find(BinaryTreeNode<T>* root,const T& x)
{
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
else
{
BinaryTreeNode<T>* cur = Find(root->_LeftChild, x);
while (cur != NULL)
{
if (cur-> _data == x)
return cur;
}
return Find(root->_RightChild, x);
}
}
BinaryTreeNode<T>* Copy(BinaryTreeNode<T>* root)
{
BinaryTreeNode<T>* NewRoot = NULL;
if (root)
{
NewRoot = new BinaryTreeNode<T>(root->_data);
NewRoot->_LeftChild =Copy(root->_LeftChild);
NewRoot->_RightChild =Copy(root->_RightChild);
}
return NewRoot;
}
private:
BinaryTreeNode<T>* _root;
};