#include<iostream.h>
const int maxsize=7;
class Tree
{
public:
Tree();
Tree(int a[],int n);
~Tree(){};
void Getparent();
void Getchild();
void Getleaf();
void LeverOrder();
private:
int data[maxsize];
};
Tree::Tree(int a[],int n)
{
for(int i=1;i<=n;i++)
data[i]=a[i];
}
void Tree::Getparent()
{
cout<<"序号为1的结点无双亲"<<endl;
for(int i=2;i<=maxsize;i++)
{
if(i%2==0)
cout<<"序号为"<<i<<"的结点的双亲序号为"<<i/2<<"其双亲的数据为"<<data[i/2]<<endl;
else
cout<<"序号为"<<i<<"的结点的双亲序号为"<<(i-1)/2<<"其双亲的数据为"<<data[(i-1)/2]<<endl;
}
}
void Tree::Getchild()
{
for(int i=1;i<=maxsize;i++)
{
if(i*2<=maxsize)
{
cout<<"序号为"<<i<<"的结点的左孩子序号为"<<2*i<<"其左孩子的数据为"<<data[2*i]<<endl;
cout<<"序号为"<<i<<"的结点的右孩子序号为"<<2*i+1<<"其右孩子的数据为"<<data[2*i+1]<<endl;
}
else
cout<<"序号为"<<i<<"的结点无孩子"<<endl;
}
}
void Tree::Getleaf()
{
for(int i=1;i<=maxsize;i++)
{
if(i*2>maxsize)
cout<<"序号为"<<i<<"的结点为叶子,其数据为"<<data[i]<<endl;
}
}
void Tree::LeverOrder()
{
for(int i=1;i<=maxsize;i++)
cout<<data[i]<<endl;
}
int main()
{
int a[8]={0,1,3,5,7,9,11,13};
Tree T(a,7);
T.Getparent();
T.Getchild();
T.Getleaf();
return 0;
}
const int maxsize=7;
class Tree
{
public:
Tree();
Tree(int a[],int n);
~Tree(){};
void Getparent();
void Getchild();
void Getleaf();
void LeverOrder();
private:
int data[maxsize];
};
Tree::Tree(int a[],int n)
{
for(int i=1;i<=n;i++)
data[i]=a[i];
}
void Tree::Getparent()
{
cout<<"序号为1的结点无双亲"<<endl;
for(int i=2;i<=maxsize;i++)
{
if(i%2==0)
cout<<"序号为"<<i<<"的结点的双亲序号为"<<i/2<<"其双亲的数据为"<<data[i/2]<<endl;
else
cout<<"序号为"<<i<<"的结点的双亲序号为"<<(i-1)/2<<"其双亲的数据为"<<data[(i-1)/2]<<endl;
}
}
void Tree::Getchild()
{
for(int i=1;i<=maxsize;i++)
{
if(i*2<=maxsize)
{
cout<<"序号为"<<i<<"的结点的左孩子序号为"<<2*i<<"其左孩子的数据为"<<data[2*i]<<endl;
cout<<"序号为"<<i<<"的结点的右孩子序号为"<<2*i+1<<"其右孩子的数据为"<<data[2*i+1]<<endl;
}
else
cout<<"序号为"<<i<<"的结点无孩子"<<endl;
}
}
void Tree::Getleaf()
{
for(int i=1;i<=maxsize;i++)
{
if(i*2>maxsize)
cout<<"序号为"<<i<<"的结点为叶子,其数据为"<<data[i]<<endl;
}
}
void Tree::LeverOrder()
{
for(int i=1;i<=maxsize;i++)
cout<<data[i]<<endl;
}
int main()
{
int a[8]={0,1,3,5,7,9,11,13};
Tree T(a,7);
T.Getparent();
T.Getchild();
T.Getleaf();
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/d4df740cdf571ac0469a1cd604a37ed4.png)