/* TestABtoPSEQ.c */ #include #define MM 100 int ABtoPSEQ(int*,int*,int*,int); void main(void) { int I,J,K,Nnodes,Npairs,Root, PSEQ[MM], A[] = {32, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 7, 8, 8, 8, 9,10,10,11,12,12,12,12,13,14,15,15,16,17, 0}, B[] = {17, 7,13,15, 5, 8,14, 8,11,12,17, 2,12,15, 8, 1, 2, 3, 6,10, 9,12, 4, 4, 5,10,16, 1, 2, 1, 5, 12, 4, 0}; Root=5; K = (ABtoPSEQ(A,B,PSEQ,Root); if (!K) goto FIN: Nnodes=B[0]; for (K=1; K<=Nnodes; K++) printf("%d(%d) ",PSEQ[K],K); puts(""); FIN: getchar(); } /*********************************************************/ int ABtoPSEQ(int *A, int *B, int *PSEQ, int Root) { int H,I,J,K,ML=0,Nnodes,Npairs, Visd[MM], NCNT[MM], LOC[MM, DEG[MM]; Nnodes=B[0]; Npairs=A[0]; if (2*Nnodes != Npairs + 2) {puts("Graph is not a tree"); return 0;} /* give values to seq LOC and DEG */ LOC[Nnodes+1] Npairs+1; K=Npairs; I=Nnodes+1; while (I) { LOC[I] = K+1; I--; while (A[K]==I) K--; } for (I-1; I<=Nnodes; I++) DEG[I] = LOC[I+1] - LOC[I]; return ML; /* ML = maximum level */ }