ツリーの親テーブルストレージ
10687 ワード
/* c6-4.h */
#define MAX_TREE_SIZE 100
typedef struct
{
TElemType data;
int parent; /* */
} PTNode;
typedef struct
{
PTNode nodes[MAX_TREE_SIZE];
int n; /* */
} PTree;
/* bo6-4.c ( c6-4.h ) (14 ) */
Status InitTree(PTree *T)
{ /* : T */
(*T).n=0;
return OK;
}
void DestroyTree()
{ /* PTree , */
}
typedef struct
{
int num;
TElemType name;
}QElemType; /* */
#include"c3-2.h" /* LinkQueue */
#include"bo3-2.c" /* LinkQueue */
Status CreateTree(PTree *T)
{ /* : T */
LinkQueue q;
QElemType p,qq;
int i=1,j,l;
char c[MAX_TREE_SIZE]; /* */
InitQueue(&q); /* */
printf(" ( , ): ");
scanf("%c%*c",&(*T).nodes[0].data); /* 0,%*c */
if((*T).nodes[0].data!=Nil) /* */
{
(*T).nodes[0].parent=-1; /* */
qq.name=(*T).nodes[0].data;
qq.num=0;
EnQueue(&q,qq); /* */
while(i<MAX_TREE_SIZE&&!QueueEmpty(q)) /* */
{
DeQueue(&q,&qq); /* */
printf(" %c : ",qq.name);
gets(c);
l=strlen(c);
for(j=0;j<l;j++)
{
(*T).nodes[i].data=c[j];
(*T).nodes[i].parent=qq.num;
p.name=c[j];
p.num=i;
EnQueue(&q,p); /* */
i++;
}
}
if(i>MAX_TREE_SIZE)
{
printf("
");
exit(OVERFLOW);
}
(*T).n=i;
}
else
(*T).n=0;
return OK;
}
#define ClearTree InitTree /* */
Status TreeEmpty(PTree T)
{ /* : T 。 : T , TRUE, FALSE */
if(T.n)
return FALSE;
else
return TRUE;
}
int TreeDepth(PTree T)
{ /* : T 。 : T */
int k,m,def,max=0;
for(k=0;k<T.n;++k)
{
def=1; /* */
m=T.nodes[k].parent;
while(m!=-1)
{
m=T.nodes[m].parent;
def++;
}
if(max<def)
max=def;
}
return max; /* */
}
TElemType Root(PTree T)
{ /* : T 。 : T */
int i;
for(i=0;i<T.n;i++)
if(T.nodes[i].parent<0)
return T.nodes[i].data;
return Nil;
}
TElemType Value(PTree T,int i)
{ /* : T ,i T 。 : i */
if(i<T.n)
return T.nodes[i].data;
else
return Nil;
}
Status Assign(PTree *T,TElemType cur_e,TElemType value)
{ /* : T ,cur_e T 。 : cur_e value */
int j;
for(j=0;j<(*T).n;j++)
{
if((*T).nodes[j].data==cur_e)
{
(*T).nodes[j].data=value;
return OK;
}
}
return ERROR;
}
TElemType Parent(PTree T,TElemType cur_e)
{ /* : T ,cur_e T */
/* : cur_e T , , " " */
int j;
for(j=1;j<T.n;j++) /* 0 */
if(T.nodes[j].data==cur_e)
return T.nodes[T.nodes[j].parent].data;
return Nil;
}
TElemType LeftChild(PTree T,TElemType cur_e)
{ /* : T ,cur_e T */
/* : cur_e T , , " " */
int i,j;
for(i=0;i<T.n;i++)
if(T.nodes[i].data==cur_e) /* cur_e, i */
break;
for(j=i+1;j<T.n;j++) /* , > */
if(T.nodes[j].parent==i) /* , ( ) < */
return T.nodes[j].data;
return Nil;
}
TElemType RightSibling(PTree T,TElemType cur_e)
{ /* : T ,cur_e T */
/* : cur_e ( ) , , " " */
int i;
for(i=0;i<T.n;i++)
if(T.nodes[i].data==cur_e) /* cur_e, i */
break;
if(T.nodes[i+1].parent==T.nodes[i].parent)
/* , cur_e */
return T.nodes[i+1].data;
return Nil;
}
Status Print(PTree T)
{ /* T。 */
int i;
printf(" =%d
",T.n);
printf("
");
for(i=0;i<T.n;i++)
{
printf(" %c",Value(T,i)); /* */
if(T.nodes[i].parent>=0) /* */
printf(" %c",Value(T,T.nodes[i].parent)); /* */
printf("
");
}
return OK;
}
Status InsertChild(PTree *T,TElemType p,int i,PTree c)
{ /* : T ,p T ,1≤i≤p +1, c T */
/* : c T p i */
int j,k,l,f=1,n=0; /* f 1,p n 0 */
PTNode t;
if(!TreeEmpty(*T)) /* T */
{
for(j=0;j<(*T).n;j++) /* T p */
if((*T).nodes[j].data==p) /* p j */
break;
l=j+1; /* c p 1 , j+1 */
if(i>1) /* c p 1 */
{
for(k=j+1;k<(*T).n;k++) /* j+1 p i-1 */
if((*T).nodes[k].parent==j) /* p */
{
n++; /* 1 */
if(n==i-1) /* p i-1 , k1 */
break;
}
l=k+1; /* c k+1 */
} /* p j,c l */
if(l<(*T).n) /* l */
for(k=(*T).n-1;k>=l;k--) /* l c.n */
{
(*T).nodes[k+c.n]=(*T).nodes[k];
if((*T).nodes[k].parent>=l)
(*T).nodes[k+c.n].parent+=c.n;
}
for(k=0;k<c.n;k++)
{
(*T).nodes[l+k].data=c.nodes[k].data; /* c */
(*T).nodes[l+k].parent=c.nodes[k].parent+l;
}
(*T).nodes[l].parent=j; /* c p */
(*T).n+=c.n; /* T c.n */
while(f)
{ /* , */
f=0; /* 0 */
for(j=l;j<(*T).n-1;j++)
if((*T).nodes[j].parent>(*T).nodes[j+1].parent)
{/* j j+1 ( ), */
t=(*T).nodes[j];
(*T).nodes[j]=(*T).nodes[j+1];
(*T).nodes[j+1]=t;
f=1; /* 1 */
for(k=j;k<(*T).n;k++) /* */
if((*T).nodes[k].parent==j)
(*T).nodes[k].parent++; /* j+1 */
else if((*T).nodes[k].parent==j+1)
(*T).nodes[k].parent--; /* j */
}
}
return OK;
}
else /* T */
return ERROR;
}
Status deleted[MAX_TREE_SIZE+1]; /* ( ) */
void DeleteChild(PTree *T,TElemType p,int i)
{ /* : T ,p T ,1≤i≤p */
/* : T p i */
int j,k,n=0;
LinkQueue q;
QElemType pq,qq;
for(j=0;j<=(*T).n;j++)
deleted[j]=0; /* 0( ) */
pq.name='a'; /* */
InitQueue(&q); /* */
for(j=0;j<(*T).n;j++)
if((*T).nodes[j].data==p)
break; /* j p */
for(k=j+1;k<(*T).n;k++)
{
if((*T).nodes[k].parent==j)
n++;
if(n==i)
break; /* k p i */
}
if(k<(*T).n) /* p i */
{
n=0;
pq.num=k;
deleted[k]=1; /* */
n++;
EnQueue(&q,pq);
while(!QueueEmpty(q))
{
DeQueue(&q,&qq);
for(j=qq.num+1;j<(*T).n;j++)
if((*T).nodes[j].parent==qq.num)
{
pq.num=j;
deleted[j]=1; /* */
n++;
EnQueue(&q,pq);
}
}
for(j=0;j<(*T).n;j++)
if(deleted[j]==1)
{
for(k=j+1;k<=(*T).n;k++)
{
deleted[k-1]=deleted[k];
(*T).nodes[k-1]=(*T).nodes[k];
if((*T).nodes[k].parent>j)
(*T).nodes[k-1].parent--;
}
j--;
}
(*T).n-=n; /* n */
}
}
void TraverseTree(PTree T,void(*Visit)(TElemType))
{ /* : T ,Visit */
/* : T, Visit */
int i;
for(i=0;i<T.n;i++)
Visit(T.nodes[i].data);
printf("
");
}
/* main6-4.c bo6-4.c */
#include"c1.h"
typedef char TElemType;
TElemType Nil=' '; /* */
#include"c6-4.h"
#include"bo6-4.c"
void vi(TElemType c)
{
printf("%c ",c);
}
void main()
{
int i;
PTree T,p;
TElemType e,e1;
InitTree(&T);
printf(" , ? %d(1: 0: ) %c %d
",TreeEmpty(T),Root(T),TreeDepth(T));
CreateTree(&T);
printf(" T , ? %d(1: 0: ) %c %d
",TreeEmpty(T),Root(T),TreeDepth(T));
printf(" T:
");
TraverseTree(T,vi);
printf(" : ");
scanf("%c%*c%c%*c",&e,&e1);
Assign(&T,e,e1);
printf(" T:
");
TraverseTree(T,vi);
printf("%c %c, %c, %c
",e1,Parent(T,e1),LeftChild(T,e1),RightSibling(T,e1));
printf(" p:
");
InitTree(&p);
CreateTree(&p);
printf(" p:
");
TraverseTree(p,vi);
printf(" p T , T p : ");
scanf("%c%d%*c",&e,&i);
InsertChild(&T,e,i,p);
Print(T);
printf(" T e i , e i: ");
scanf("%c%d",&e,&i);
DeleteChild(&T,e,i);
Print(T);
}