数据结构(怎样将树的双亲表示法转化为孩子兄弟法)
下面是我的代码,结点输入多了,就会报错,不知道是哪里出错了?请高手帮我看看,谢谢。。。
建立树时可以参照输入以下树 :R -1 ,A 0,B 0, C 0, D 1,E 1, F 3 ,G 6,H 6,K 6;
当输入小于7个,程序不会出错,但输入7个以上,程序就报错了。
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef char ElemType;
//--树的双亲表示结点定义---
typedef struct {
ElemType data;
int parent;
}PTNode;
//--树的双亲表示储存结构定义---
typedef struct {
PTNode node[MAXSIZE];
int count;//根的位置和节点个数---
}PTree;
//--树的二叉链表(孩子-兄弟)储存表示---
typedef struct CSNode{
int ip;
ElemType data;
struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;
//--功能函数声明--------------------------------
PTree CreateTree(PTree T);//用双亲表示法创建树---
void InitTree(PTree *Tree);//初始化树结点---
CSNode GetTreeNode(ElemType x,int ip);//得到树的结点---
void Pre_Order(CSNode *T);//树的前序遍历(递归)----
void Pre_Order2(PTree T);//树的前序遍历(非递归)----
void PrintTree(PTree T);//输出树----
CSNode *change(PTree T);//将树转换为二叉树----
//----------------------------------------------------
PTree CreatTree(PTree T){
int i=1,pa;
char ch='0';
PTNode p;
for(i=1;ch!='#';i++){
cout<<"请输入第"<<i<<"个结点<data parent>,注:输入 # 1 结束"<<endl;
cin>>ch>>pa;
//if(ch!='#'){
p.data=ch;
p.parent=pa;
T.count++;
T.node[T.count].data=p.data;
T.node[T.count].parent=p.parent;
//}
//else break;
}
PrintTree(T);
return T;
}
void InitTree(PTree *Tree){
Tree->count=-1;
}
CSNode GetTreeNode(ElemType x,int ip){
CSNode C;
C.ip=ip;
C.data=x;
C.firstchild=C.nextsibling=NULL;
return C;
}
void Pre_Order(CSNode *T){
if(T!=NULL){
cout<<T->data;
Pre_Order(T->firstchild);
Pre_Order(T->nextsibling);
}
}
void Pre_Order2(PTree T){
int i;
for(i=0;i<T.count;i++)
{
cout<<T.node[i].data;
}
}
void PrintTree(PTree T){
int i;
for(i=0;i<=T.count;i++){
cout<<i<<" "<<T.node[i].data<<" "<<T.node[i].parent<<endl;
}
}
/*CSNode *ToBiTree(PTree T)
{
int i,j=0;
CSNode p[MAXSIZE];
CSNode *x,*y,*temp,*Tree;
x=(CSNode *)malloc(sizeof(CSNode));
y=(CSNode *)malloc(sizeof(CSNode));
temp=(CSNode *)malloc(sizeof(CSNode));
Tree=(CSNode *)malloc(sizeof(CSNode));
for(i=0;i<T.count;i++)
{ p[i]=GetTreeNode(T.node[i].data,i);
}
for(i=1;i<T.count;i++)
{
x=&p[i];
y=&p[j];
while(T.node[i].parent!=y->ip)
{j++;
y=&p[j];
}
if(!(y->firstchild))//如果孩子为空;
{ y->firstchild=x;//将p[i]地址给孩子;
temp=x;
}
else//如果孩子域不为空,将p[i]给兄弟域;
{ temp->nextsibling=x;
temp=x;
}
}
Tree=&p[0];
return Tree;
}
*/
CSNode *change(PTree T)
{
int i,j=0;
CSNode p[MAXSIZE];
CSNode *ip,*is,*ir,*Tree;
ip=(CSNode *)malloc(sizeof(CSNode));
is=(CSNode *)malloc(sizeof(CSNode));
ir=(CSNode *)malloc(sizeof(CSNode));
Tree=(CSNode *)malloc(sizeof(CSNode));
for(i=0;i<T.count;i++)
{
p[i]=GetTreeNode(T.node[i].data,i); //p[0]=GetTreeNode(T.node[0].data);
}
for(i=1;i<T.count;i++)
{
ip=&p[i];
is=&p[j];
while(T.node[i].parent!=is->ip)
{
j++;
is=&p[j];
}
if(!(is->firstchild))//如果第一个孩子为空
{
is->firstchild=ip;
ir=ip;
}
else
{
ir->nextsibling=ip;
ir=ip;
}
}
Tree=&p[0];
return Tree;
}
void main()
{ /*PTree *T;
CSNode *Tree;
T=new PTree;
InitTree(T);
CreatTree(*T);
Tree=change(*T);
Pre_Order(Tree);
*/
PTree T;
CSNode *Tree;
InitTree(&T);
cout<<"建立一般树,依次输入各个结点情况:\n";
cout<<"输入结点方式: data(字符) parent(整形) 如:R -1 ,A 0 ,B 0 ,C 0\n";
T=CreatTree(T);
Tree=change(T);
cout<<"树的前序遍历(递归):\n";
Pre_Order(Tree);
printf("\n");
}