#include<iostream>
#include<cstdio>
using namespace std;
template<class ElemType>
struct BinTreeNode
{
ElemType data;
BinTreeNode<ElemType> *leftChild;
BinTreeNode<ElemType> *rightChild;
BinTreeNode();
BinTreeNode(const ElemType &val,BinTreeNode<ElemType> *lChild=NULL,BinTreeNode<ElemType> *rChild=NULL);
};
template<class ElemType>
BinTreeNode<ElemType>::BinTreeNode()
{
leftChild=rightChild=NULL;
}
template<class ElemType>
BinTreeNode<ElemType>::BinTreeNode(const ElemType &val,BinTreeNode<ElemType> *lChild,BinTreeNode<ElemType> *rChild)
{
data=val;
leftChild=lChild;
rightChild=rChild;
}
template<class ElemType>
class BinaryTree
{
protected:
BinTreeNode<ElemType> *root;
BinTreeNode<ElemType> *CopyTreeHelp(BinTreeNode<ElemType> *r);
void DestoryHelp(BinTreeNode<ElemType> *&r);
void PreOrderHelp(const BinTreeNode<ElemType> *r) const;
void InOrderHelp(const BinTreeNode<ElemType> *r) const;
void PostOrderHelp(const BinTreeNode<ElemType> *r) const;
int HeightHelp(const BinTreeNode<ElemType> *r) const;
int NodeCountHelp(const BinTreeNode<ElemType> *r) const;
BinaryTree<ElemType> *ParentHelp(const BinTreeNode<ElemType> *r,const BinaryTree<ElemType> *cur) const;
public:
BinaryTree();
~BinaryTree();
BinTreeNode<ElemType> *GetRoot() const;
bool Empty() const;
bool GetElem(const BinaryTree<ElemType> *cur,ElemType &e);
bool SetElem(BinaryTree<ElemType> *cur,const ElemType &e);
void InOrder() const;
void PreOrder() const;
void PostOrder() const;
void LevelOrder() const;
int NodeCount() const;
BinTreeNode<ElemType> *LeftChild(const BinaryTree<ElemType> *cur);
BinTreeNode<ElemType> *RightChild(const BinaryTree<ElemType> *cur);
BinTreeNode<ElemType> *Parent(const BinTreeNode<ElemType> *cur) const;
void InsertRightChild(BinaryTree<ElemType> *cur,const ElemType &e) const;
void DeleteLeftChild(BinaryTree<ElemType> *cur,const ElemType &e);
int Height() const;
BinaryTree(const ElemType &e);
BinaryTree(const BinaryTree<ElemType> ©);
BinaryTree(BinTreeNode<ElemType> *r);
BinaryTree<ElemType>&operator=(const BinaryTree<ElemType> ©);
};
template<class ElemType>
BinaryTree<ElemType>::BinaryTree()
{
root=NULL;
}
template<class ElemType>
BinaryTree<ElemType>::BinaryTree(const ElemType &e)
{
root=new BinTreeNode<ElemType>();
root->data=e;
}
template<class ElemType>
BinaryTree<ElemType>::BinaryTree(BinTreeNode<ElemType> *r)
{
root=r;
}
template<class ElemType>
void BinaryTree<ElemType>::DestoryHelp(BinTreeNode<ElemType> *&r)
{
if(r!=NULL)
{
DestoryHelp(r->leftChild);
DestoryHelp(r->rightChild);
delete r;
r=NULL;
}
}
template<class ElemType>
BinaryTree<ElemType>::~BinaryTree()
{
DestoryHelp(root);
}
template<class ElemType>
BinTreeNode<ElemType> *BinaryTree<ElemType>::GetRoot() const
{
return root;
}
template<class ElemType>
bool BinaryTree<ElemType>::Empty() const
{
return root==NULL;
}
template<class ElemType>
void BinaryTree<ElemType>::PreOrderHelp(const BinTreeNode<ElemType> *r) const
{
if(r!=NULL)
{
cout<<r->data<<' ';
PreOrderHelp(r->leftChild);
PreOrderHelp(r->rightChild);
}
}
template<class ElemType>
void BinaryTree<ElemType>::PreOrder() const
{
PreOrderHelp(root);
}
template<class ElemType>
void BinaryTree<ElemType>::InOrderHelp(const BinTreeNode<ElemType> *r) const
{
if(r!=NULL)
{
InOrderHelp(r->leftChild);
cout<<r->data<<' ';
InOrderHelp(r->rightChild);
}
}
template<class ElemType>
void BinaryTree<ElemType>::InOrder() const
{
InOrderHelp(root);
}
template<class ElemType>
void BinaryTree<ElemType>::PostOrderHelp(const BinTreeNode<ElemType> *r) const
{
if(r!=NULL)
{
PostOrderHelp(r->leftChild);
PostOrderHelp(r->rightChild);
cout<<r->data<<' ';
}
}
template<class ElemType>
void BinaryTree<ElemType>::PostOrder() const
{
PostOrderHelp(root);
}
template<class ElemType>
int BinaryTree<ElemType>::NodeCountHelp(const BinTreeNode<ElemType> *r) const
{
if(r==NULL) return 0;
else return NodeCountHelp(r->leftChild)+NodeCountHelp(r->rightChild)+1;
}
template<class ElemType>
int BinaryTree<ElemType>::NodeCount() const
{
return NodeCountHelp(root);
}
template<class ElemType>
int BinaryTree<ElemType>::HeightHelp(const BinTreeNode<ElemType> *r) const
{
if(r==NULL)
{
return 0;
}
else
{
int lHeight,rHeight;
lHeight=HeightHelp(r->leftChild);
rHeight=HeightHelp(r->rightChild);
return (lHeight>rHeight?lHeight:rHeight)+1;
}
}
template<class ElemType>
int BinaryTree<ElemType>::Height() const
{
return HeightHelp(root);
}
template<class ElemType>
BinTreeNode<ElemType>*BinaryTree<ElemType>::CopyTreeHelp(BinTreeNode<ElemType> *r)
{
if(r==NULL)
{
return NULL;
}
else
{
BinTreeNode<ElemType> *lChild=CopyTreeHelp(r->leftChild);
BinTreeNode<ElemType> *rChild=CopyTreeHelp(r->rightChild);
r=new BinTreeNode<ElemType>(r->data,lChild,rChild);
return r;
}
}
template<class ElemType>
BinaryTree<ElemType>::BinaryTree(const BinaryTree<ElemType> ©)
{
root=CopyTreeHelp(copy.root);
}
template<class ElemType>
BinaryTree<ElemType>&BinaryTree<ElemType>::operator=(const BinaryTree<ElemType> ©)
{
if(©!=this)
{
DestoryHelp(root);
root=CopyTreeHelp(copy.root);
}
return *this;
}
template<class ElemType>
void DisplayBTWithTreeShapeHelp(BinTreeNode<ElemType> *r,int level)
{
if(r!=NULL)
{
DisplayBTWithTreeShapeHelp<ElemType>(r->rightChild,level+1);
cout<<endl;
for(int i=0;i<level-1;i++)
{
cout<<' ';
}
cout<<r->data;
DisplayBTWithTreeShapeHelp<ElemType>(r->leftChild,level+1);
}
}
template<class ElemType>
void DisplayBTWithTreeShape(const BinaryTree<ElemType> &bt)
{
DisplayBTWithTreeShapeHelp<ElemType>(bt.GetRoot(),1);
cout<<endl;
}
template<class ElemType>
void CreateBinaryTreeHelp(BinTreeNode<ElemType>* &r,ElemType pre[],ElemType in[],int preLeft,int preRight,int inLeft,int inRight)
{
if(preLeft>preRight||inLeft>inRight)
{
r=NULL;
}
else
{
r=new BinTreeNode<ElemType>;
r->data=pre[preLeft];
int mid=inLeft;
while(in[mid]!=pre[preLeft])
{
mid++;
}
CreateBinaryTreeHelp(r->leftChild,pre,in,preLeft+1,preLeft+mid-inLeft,inLeft,mid-1);
CreateBinaryTreeHelp(r->rightChild,pre,in,preLeft+mid-inLeft+1,preRight,mid+1,inRight);
}
}
template<class ElemType>
BinaryTree<ElemType> CreateBinaryTree(ElemType pre[],ElemType in[],int n)
{
BinTreeNode<ElemType> *r;
CreateBinaryTreeHelp<ElemType>(r,pre,in,0,n-1,0,n-1);
return BinaryTree<ElemType>(r);
}
int main()
{
BinaryTree<int>bt;
int pre[5]={1,2,4,5,3};
int in[5]={4,2,5,1,3};
bt=CreateBinaryTree(pre,in,5);
cout<<bt.Height()<<endl;
cout<<bt.NodeCount()<<endl;
bt.PreOrder();
cout<<endl;
bt.InOrder();
cout<<endl;
bt.PostOrder();
cout<<endl;
return 0;
}
二叉树的建立与一些操作
最新推荐文章于 2022-12-11 10:10:40 发布