#include<algorithm>
using namespace std;
int n=0;
template <class Datatype>
struct Node
{
Datatype data;
Node <Datatype> *lchild,*rchild;
};
template <class Datatype>
class tree
{
public:
tree(){root=created(root);}
~tree(){release(root);}
void preorder(){preorder(root);}
void inorder(){inorder(root);}
void postorder(){postorder(root);}
void levelorder(Node <Datatype> *root);
void jiedian(Node <Datatype> *root);
int yezijiedian(Node <Datatype> *root);
int shendu(Node <Datatype> *root);
Node <Datatype> *getroot();
private:
Node <Datatype> *root;
Node <Datatype> *created(Node <Datatype> *bt);
void release(Node <Datatype> *bt);
void preorder(Node <Datatype> *bt);
void inorder(Node <Datatype> *bt);
void postorder(Node <Datatype> *bt);
};
template <class Datatype>
Node <Datatype> * tree <Datatype>::getroot()
{
return root;
}
template <class Datatype>
Node <Datatype> * tree <Datatype>::created(Node <Datatype> *bt)
{
Datatype ch;
cin>>ch;
if(ch=='#')
{
bt=NULL;
}
else
{
bt=new Node <Datatype>;
bt->data=ch;
bt->lchild=created(bt->lchild);
bt->rchild=created(bt->rchild);
}
return bt;
}
template <class Datatype>
void tree <Datatype>::preorder(Node <Datatype> *bt)
{
if(bt==NULL)
{
return;
}
else
{
cout<<bt->data<<' ';
preorder(bt->lchild);
preorder(bt->rchild);
}
}
template <class Datatype>
void tree <Datatype>::inorder(Node <Datatype> *bt)
{
if(bt==NULL)
{
return;
}
else
{
inorder(bt->lchild);
cout<<bt->data<<' ';
inorder(bt->rchild);
}
}
template <class Datatype>
void tree <Datatype>::postorder(Node <Datatype> *bt)
{
if(bt==NULL)
{
return;
}
else
{
postorder(bt->lchild);
postorder(bt->rchild);
cout<<bt->data<<' ';
}
}
template <class Datatype>
void tree <Datatype>::levelorder(Node <Datatype> *root)
{
int front1=-1,rear=-1;
Node <Datatype> *Q[101];
Node <Datatype> *q;
if(root==NULL)
{
return;
}
Q[++rear]=root;
while(front1!=rear)
{
q=Q[++front1];
cout<<q->data<<' ';
if(q->lchild!=NULL) Q[++rear]=q->lchild;
if(q->rchild!=NULL) Q[++rear]=q->rchild;
}
}
template <class Datatype>
void tree <Datatype>::release(Node <Datatype> *bt)
{
if(bt!=NULL)
{
release(bt->lchild);
release(bt->rchild);
delete bt;
}
}
template <class Datatype>
void tree <Datatype>::jiedian(Node <Datatype> *root)
{
if(root!=NULL)
{
jiedian(root->lchild);
n++;
jiedian(root->rchild);
}
}
template <class Datatype>
int tree <Datatype>::yezijiedian(Node <Datatype> *root)
{
if(root==NULL)
{
return 0;
}
if(root->lchild==NULL&&root->rchild==NULL)
{
return 1;
}
return yezijiedian(root->lchild)+yezijiedian(root->rchild);
}
template <class Datatype>
int tree <Datatype>::shendu(Node <Datatype> *root)
{
if(root==NULL)
{
return 0;
}
else
{
int hl=shendu(root->lchild);
int hr=shendu(root->rchild);
return hl>hr?hl+1:hr+1;
}
}
int main()
{
tree <char> T;
cout<<"树的前序遍历:";
T.preorder();
cout<<endl;
cout<<"树的中序遍历:";
T.inorder();
cout<<endl;
cout<<"树的后序遍历:";
T.postorder();
cout<<endl;
cout<<"树的层序遍历:";
T.levelorder(T.getroot());
cout<<endl;
cout<<"树的结点个数为:";
T.jiedian(T.getroot());
cout<<n<<endl;
cout<<"树的叶子结点的个数为:";
cout<<T.yezijiedian(T.getroot())<<endl;
cout<<"树的深度为:";
cout<<T.shendu(T.getroot())<<endl;
return 0;
}
树的操作
最新推荐文章于 2022-10-19 21:21:23 发布