二叉树:将一数组创建二叉树,四种遍历方法:前序,中序,后序,层次遍历,与求不同情况下的结点个数等。
代码实现:
#include <iostream>
#include <cstdlib>
#include <assert.h>
#include <queue>
#include <stack>
using namespace std;
template <class T>
struct BinaryTreeNode
{
T _data;
BinaryTreeNode<T> *_left;
BinaryTreeNode<T> *_right;
BinaryTreeNode(const T& x)
:_data(x)
,_left(NULL)
,_right(NULL)
{}
};
template <class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree(T* a,size_t size,const T& invalid=T())
{
assert(a);
size_t index=0;
_root=_CreateTree(a,size,invalid,index); //递归创建二叉树
}
BinaryTree()
:_root(NULL)
{}
BinaryTree(const BinaryTree<T>& t)
{
_root=_Copy(t._root);
}
BinaryTree<T>& operator=(BinaryTree<T>& t)
{
if(this!=&t)
{
Node* tmp=_Copy(t._root);
_root=_Destroy(_root);
_root=tmp;
}
return *this;
}
~BinaryTree()
{
_root=_Destroy(_root);
}
void PrevOrder() //递归前序遍历
{
_PrevOrder(_root);
cout<<endl;
}
void PrevOrderNonR() //非递归实现前序
{
assert(_root);
stack<Node*> s;
Node* cur=_root;
while(!s.empty()||cur)
{
while(cur)
{
cout<<cur->_data<<" ";
s.push(cur);
cur=cur->_left;
}
Node* top=s.top();
s.pop();
cur=top->_right;
}
cout<<endl;
}
void InOrder() //递归中序遍历
{
_InOrder(_root);
cout<<endl;
}
void InOrderNonR() //非递归实现中序
{
assert(_root);
stack<Node*> s;
Node* cur=_root;
while(!s.empty()||cur)
{
while(cur)
{
s.push(cur);
cur=cur->_left;
}
Node* top=s.top();
cout<<top->_data<<" ";
s.pop();
cur=top->_right;
}
cout<<endl;
}
void PostOrder() //递归后序遍历
{
_PostOrder(_root);
cout<<endl;
}
void PostOrderNonR() //非递归实现后序
{
assert(_root);
stack<Node*> s;
Node* cur=_root;
Node* prev=NULL;
while(!s.empty()||cur)
{
while(cur)
{
s.push(cur);
cur=cur->_left;
}
Node* top=s.top();
if(top->_right==NULL||top->_right==prev)
{
cout<<top->_data<<" ";
s.pop();
prev=top;
}
else
{
cur=top->_right;
}
}
cout<<endl;
}
void LevlOrder() //非递归层次遍历
{
_LevOrder(_root);
cout<<endl;
}
size_t Size() //求结点个数
{
size_t size=0;
_Size(_root,size);
return size;
}
size_t Depth() //求深度
{
return _Depth(_root);
}
size_t GetLeafSize() //求叶子结点个数
{
size_t size=0;
_GetLeafSize(_root,size);
return size;
}
size_t GetKLevelSize(size_t k) //求K层结点个数
{
size_t count=0;
size_t level=1;
_GetKLevelSize(_root,k,count,level);
return count;
}
Node* Find(const T& x ) //寻找某一个结点
{
return _find(_root,x);
}
protected:
Node* _find(Node* root,const T& x)
{
if(root==NULL)
return NULL;
if(root->_data==x)
return root;
Node* tmp=find(root->_left,x);
if(tmp==NULL)
{
tmp=find(root->_right,x);
}
return tmp;
}
void _GetKLevelSize(Node* root,size_t k,size_t& count,size_t level)
{
if(root==NULL)
return;
if(level==k)
{
++count;
return;
}
_GetKLevelSize(root->_left,k,count,level+1);
_GetKLevelSize(root->_right,k,count,level+1);
}
void _GetLeafSize(Node* root,size_t& size)
{
if(root!=NULL)
{
if(root->_left==NULL&&root->_right==NULL)
{
++size;
return;
}
_GetLeafSize(root->_left,size);
_GetLeafSize(root->_right,size);
}
}
size_t _Depth(Node* root)
{
if(root==NULL)
{
return 0;
}
size_t leftcount=_Depth(root->_left)+1;
size_t rightcount=_Depth(root->_right)+1;
return leftcount>rightcount?leftcount:rightcount;
}
void _Size(Node* root,size_t& size)
{
if(root!=NULL)
{
++size;
_Size(root->_left,size);
_Size(root->_right,size);
}
}
void _LevOrder(Node* root)
{
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* tmp=q.front();
cout<<tmp->_data<<" ";
q.pop();
if(tmp->_left!=NULL)
{
q.push(tmp->_left);
}
if(tmp->_right!=NULL)
{
q.push(tmp->_right);
}
}
}
void _InOrder(Node* root)
{
if(root!=NULL)
{
_InOrder(root->_left);
cout<<root->_data<<" ";
_InOrder(root->_right);
}
}
void _PostOrder(Node* root)
{
if(root!=NULL)
{
_PostOrder(root->_left);
_PostOrder(root->_right);
cout<<root->_data<<" ";
}
}
void _PrevOrder(Node* root)
{
if(root!=NULL)
{
cout<<root->_data<<" ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
}
Node* _CreateTree(T* a,size_t size,const T& invalid,size_t& index)
{
Node* NewNode=NULL;
if(index<size&&a[index]!=invalid)
{
NewNode=new Node(a[index]);
NewNode->_left=_CreateTree(a,size,invalid,++index);
NewNode->_right=_CreateTree(a,size,invalid,++index);
}
return NewNode;
}
Node* _Copy(Node* root)
{
Node* NewNode=NULL;
if(root!=NULL)
{
NewNode=new Node(root->_data);
NewNode->_left=_Copy(root->_left);
NewNode->_right=_Copy(root->_right);
}
return NewNode;
}
Node* _Destroy(Node* root)
{
if(root!=NULL)
{
root->_left=_Destroy(root->_left);
root->_right=_Destroy(root->_right);
delete root;
root=NULL;
}
return root;
}
protected:
Node *_root;
};
void TestBinaryTree()
{
/*int a[10]={1,2,3,'#','#',4,'#','#',5,6};
BinaryTree<int> t1(a,sizeof(a)/sizeof a[0],'#');
BinaryTree<int> t2=t1;
t2.PrevOrder();
t2.InOrder();
t2.PostOrder();
t2.LevlOrder();
cout<<"深度:"<<t2.Depth()<<endl;
cout<<"结点个数:"<<t2.Size()<<endl;
cout<<"叶子结点个数:"<<t2.GetLeafSize()<<endl;
cout<<"层数结点个数:"<<t2.GetKLevelSize(2)<<endl;
cout<<endl<<endl;
*/
int a1[]={1,2,'#',3,'#','#',4,5,'#',6,'#',7,'#','#',8};
BinaryTree<int> t3(a1,sizeof(a1)/sizeof a1[0],'#');
BinaryTree<int> t4;
t4=t3;
t4.PrevOrder();
t4.PrevOrderNonR();
t4.InOrder();
t4.InOrderNonR();
t4.PostOrder();
t4.PostOrderNonR();
t4.LevlOrder();
cout<<"深度:"<<t4.Depth()<<endl;
cout<<"结点个数:"<<t4.Size()<<endl;
cout<<"叶子结点个数:"<<t4.GetLeafSize()<<endl;
cout<<"层数结点个数:"<<t4.GetKLevelSize(3)<<endl;
}
int main()
{
TestBinaryTree();
system("pause");
return 0;
}