C语言数据结构树的双亲表示法实例详解

时间:2021-05-20

1、树的双亲表示法:

树的双亲表示法


2、

Status InitTree(PTree *T) { (*T).n=0; return OK; } void DestroyTree() { } typedef struct { int num; TElemType name; }QElemType; #include"c3-2.h" #include"bo3-2.c" Status CreateTree(PTree *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("结点数超过数组容量\n"); exit(OVERFLOW); } (*T).n=i; } else (*T).n=0; return OK; } #define ClearTree InitTree Status TreeEmpty(PTree T) { if(T.n) return FALSE; else return TRUE; } int TreeDepth(PTree 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) { 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) { if(i<T.n) return T.nodes[i].data; else return Nil; } Status Assign(PTree *T,TElemType cur_e,TElemType 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) { int j; for(j=1;j<T.n;j++) if(T.nodes[j].data==cur_e) return T.nodes[T.nodes[j].parent].data; return Nil; } TElemType LeftChild(PTree T,TElemType cur_e) { int i,j; for(i=0;i<T.n;i++) if(T.nodes[i].data==cur_e) 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) { int i; for(i=0;i<T.n;i++) if(T.nodes[i].data==cur_e) break; if(T.nodes[i+1].parent==T.nodes[i].parent) return T.nodes[i+1].data; return Nil; } Status Print(PTree T) { int i; printf("结点个数=%d\n",T.n); printf(" 结点 双亲\n"); 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("\n"); } return OK; } Status InsertChild(PTree *T,TElemType p,int i,PTree c) { int j,k,l,f=1,n=0; PTNode t; if(!TreeEmpty(*T)) { for(j=0;j<(*T).n;j++) if((*T).nodes[j].data==p) break; l=j+1; if(i>1) { for(k=j+1;k<(*T).n;k++) if((*T).nodes[k].parent==j) { n++; if(n==i-1) break; } l=k+1; } if(l<(*T).n) for(k=(*T).n-1;k>=l;k--) { (*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; (*T).nodes[l+k].parent=c.nodes[k].parent+l; } (*T).nodes[l].parent=j; (*T).n+=c.n; while(f) { f=0; for(j=l;j<(*T).n-1;j++) if((*T).nodes[j].parent>(*T).nodes[j+1].parent) { t=(*T).nodes[j]; (*T).nodes[j]=(*T).nodes[j+1]; (*T).nodes[j+1]=t; f=1; for(k=j;k<(*T).n;k++) if((*T).nodes[k].parent==j) (*T).nodes[k].parent++; else if((*T).nodes[k].parent==j+1) (*T).nodes[k].parent--; } } return OK; } else return ERROR; } Status deleted[MAX_TREE_SIZE+1]; void DeleteChild(PTree *T,TElemType p,int i) { int j,k,n=0; LinkQueue q; QElemType pq,qq; for(j=0;j<=(*T).n;j++) deleted[j]=0; pq.name='a'; InitQueue(&q); for(j=0;j<(*T).n;j++) if((*T).nodes[j].data==p) break; for(k=j+1;k<(*T).n;k++) { if((*T).nodes[k].parent==j) n++; if(n==i) break; } if(k<(*T).n) { 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; } } void TraverseTree(PTree T,void(*Visit)(TElemType)) { int i; for(i=0;i<T.n;i++) Visit(T.nodes[i].data); printf("\n"); }

3、

#define MAX_TREE_SIZE 100 typedef struct { TElemType data; int parent; } PTNode; typedef struct { PTNode nodes[MAX_TREE_SIZE]; int n; } PTree

4、

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\n",TreeEmpty(T),Root(T),TreeDepth(T)); CreateTree(&T); printf("构造树T后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",TreeEmpty(T),Root(T),TreeDepth(T)); printf("层序遍历树T:\n"); TraverseTree(T,vi); printf("请输入待修改的结点的值 新值: "); scanf("%c%*c%c%*c",&e,&e1); Assign(&T,e,e1); printf("层序遍历修改后的树T:\n"); TraverseTree(T,vi); printf("%c的双亲是%c,长子是%c,下一个兄弟是%c\n",e1,Parent(T,e1),LeftChild(T,e1),RightSibling(T,e1)); printf("建立树p:\n"); InitTree(&p); CreateTree(&p); printf("层序遍历树p:\n"); 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); }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章