/* The purpose of this code is to creat Child-Sibling Tree * from Non-connected UG. * version:0.1 * by double * date:2009-06-16 */ #include "/usr/c/head.h" #define MAX 20 #ifndef INT_MAX #define INT_MAX ((int)(~0U>>1)) #endif typedef char vertex_type; typedef char info_type; typedef struct arc_type { int mark; /* Not used in this code*/ int ivex, jvex; struct arc_type * ilink, * jlink; info_type * info; }arc_type; typedef struct vex_type { vertex_type data; arc_type * first; }vex_type; typedef struct { vex_type vexs[MAX]; int vexnum, arcnum; }AML_graph; typedef struct CS_tree_node { vertex_type data; struct CS_tree_node * child, * sibling; }CS_tree_node, * CS_tree; AML_graph G; int flag[MAX]; int locate_vex (AML_graph G, vertex_type v) { int i; for (i = 0; i < G.vexnum; i++) if (G.vexs[i].data == v) return i; return -1; } status init_graph(AML_graph * G) { int temp, i, p1, p2; vertex_type v1, v2; arc_type * p; printf("Input the vexnum:"); if (scanf("%d", & G->vexnum) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} for (i = 0; i < G->vexnum; i++) { printf("No.%d:", i + 1); if (scanf("%c", & G->vexs[i].data) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} G->vexs[i].first = NULL; } printf("Input the arcnum:"); if (scanf("%d", & G->arcnum) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} for (i = 0; i < G->arcnum; i++) { printf("No.%d, v1:", i + 1); if (scanf("%c", & v1) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} printf("No.%d, v2:", i + 1); if (scanf("%c", & v2) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} p1 = locate_vex(*G, v1); if (p1 == -1) { printf("v1 = %c out of range!/n", v1); exit(ERROR); } p2 = locate_vex(*G, v2); if (p2 == -1) { printf("v2 = %c out of range!/n", v2); exit(ERROR); } p = (arc_type *)malloc(sizeof(arc_type)); if (!p) exit(OVERFLOW); p->mark = 0; p->ivex = p1; p->jvex = p2; p->ilink = G->vexs[p1].first; p->jlink = G->vexs[p2].first; p->info = NULL; G->vexs[p1].first = p; G->vexs[p2].first = p; } return OK; } int first_adj(int v) { arc_type * p; p = G.vexs[v].first; if (p) return p->ivex == v ? p->jvex : p->ivex; return -1; } int next_adj(int v, int w) { arc_type * p, * q; p = G.vexs[v].first; while (p) { if (p->ivex + p->jvex == v + w) { q = v == p->ivex ? p->ilink : p->jlink; if (q) return v == q->ivex ? q->jvex : q->ivex; } p = v == p->ivex ? p->ilink : p->jlink; } return -1; } status DFS_tree(AML_graph G, int v, CS_tree * T) { CS_tree p , q; int first = 1, w; flag[v] = TRUE; for (w = first_adj(v); w >= 0; w = next_adj(v, w)) if (flag[w] == FALSE) { p = (CS_tree)malloc(sizeof(CS_tree_node)); if (!p) exit(OVERFLOW); p->data = G.vexs[w].data; p->child = p->sibling = NULL; if (first) { (*T)->child = p; first = 0; } else q->sibling = p; q = p; DFS_tree(G, w, &p); } return OK; } status DFS_forest(AML_graph G, CS_tree * T) { int i; *T = NULL; CS_tree p, q; for (i = 0; i < G.vexnum; i++) flag[i] = FALSE; for (i = 0; i < G.vexnum; i++) if (flag[i] == FALSE) { p = (CS_tree)malloc(sizeof(CS_tree_node)); if (!p) exit(OVERFLOW); p->data = G.vexs[i].data; p->child = p->sibling = NULL; if (!(*T)) *T = p; else q->sibling = p; q = p; DFS_tree(G, i, &p); } return OK; } status visit(vertex_type e) { printf("%c/t", e); return OK; } status first_order_traverse(CS_tree T) { if (T) { visit(T->data); first_order_traverse(T->child); first_order_traverse(T->sibling); } return OK; } int main(void) { CS_tree T; init_graph (&G); printf("AML_graph initilized OK!/n"); DFS_forest(G, &T); printf("CS_tree initilized OK!/nThe tree is:/n"); first_order_traverse(T); printf("/n"); return 0; }