-
-
using namespace std;
-
const int Size= 20;
-
class Tree
-
{
-
char tree[ 20];
-
int end;
-
public:
-
Tree(){end= 1;}
-
Tree( char a[], int n);
-
~Tree(){ cout<< "end!"<< endl;}
-
void PrintAll();
-
void PrintParent();
-
void PrintChild();
-
};
-
Tree::Tree( char a[], int n)
-
{
-
for( int i= 0;i<n;i++)
-
tree[i]=a[i];
-
end=n;
-
}
-
void Tree::PrintAll()
-
{
-
for( int i= 0;i<end;i++)
-
{
-
if(tree[i]!= 0)
-
{
-
cout<< "序号为:"<<i+ 1<< " 数据为:"<<tree[i]<< endl;
-
}
-
}
-
cout<< endl<< endl;
-
}
-
void Tree::PrintChild()
-
{
-
for( int i= 0;i<end;i++)
-
{
-
if((i+ 1)* 2+ 1<end)
-
{
-
if(tree[i* 2+ 1]!= 0)
-
{
-
cout<< "序号为:"<<i+ 1<< " 数据为:"<<tree[i]<< " 左孩子:"<<tree[i* 2+ 1]<< "("<<i* 2+ 2<< ")";
-
}
-
if(tree[i* 2+ 2]!= 0)
-
{
-
cout<< " 右孩子:"<<tree[i* 2+ 2]<< "("<<i* 2+ 3<< ")";
-
}
-
if(tree[i* 2+ 1]!= 0&&tree[i* 2+ 2]!= 0) cout<< endl;
-
}
-
}
-
cout<< endl<< endl;
-
}
-
void Tree::PrintParent()
-
{
-
for( int i= 1;i<end;i++)
-
{
-
if((i+ 1)% 2== 0&&tree[i]!= 0)
-
{
-
cout<< "序号为:"<<i+ 1<< " 数据为:"<<tree[i]<< " 父母结点:"<<tree[i/ 2]<< "("<<(i/ 2)+ 1<< ")"<< endl;
-
}
-
if((i+ 1)% 2!= 0&&tree[i]!= 0)
-
{
-
cout<< "序号为:"<<i+ 1<< " 数据为:"<<tree[i]<< " 父母结点:"<<tree[(i -1)/ 2]<< "("<<(i+ 1)/ 2<< ")"<< endl;
-
}
-
}
-
}
-
int main()
-
{
-
Tree T;
-
char a[ 20]={ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'M'};
-
T=Tree(a, 20);
-
T.PrintAll();
-
T.PrintChild();
-
T.PrintParent();
-
return 0;
-
}