1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #include<iostream> #include<queue> #include"ADL.h" using namespace std;
#define TElemType string #define inp(i,x,y) for(i=x;i<=y;i++) #define Max_Vertex_Num 20 typedef enum{unvisited, visited}VisitIf;
typedef struct CSNode { TElemType data; struct CSNode *firstchild, *nextsibling; }CSNode, *CSTree; typedef struct QElem { CSTree node; int v; }QElem;
VisitIf visite[Max_Vertex_Num];
Status CreateForest(ALGraph G, CSTree &T, Status(*CreateTree)(ALGraph G, int v, CSTree &T)); Status DFSTree(ALGraph G, int v, CSTree &T); Status BFSTree(ALGraph G, int v, CSTree &T);
Status CSFPreRootOrder(CSTree T, Status(*Visit)(TElemType e)); Status CSFPostRootOrder(CSTree T, Status(*Visit)(TElemType e)); Status LevelOrderTraverse(CSTree T, Status(*Visit)(TElemType e)); Status TreeTransOperator(TElemType e); Status ForestPrint(CSTree T); int main() { ALGraph G, GR; CreateGraph(G, GR); CSTree DFST, BFST; CreateForest(G, DFST, DFSTree); CreateForest(G, BFST, BFSTree); GraphPrint(G, GR); cout << "------------------------CS_Forest DFS Traverse-------------------------\n"; ForestPrint(DFST); cout << endl << "------------------------CS_Forest BFS Traverse-------------------------\n"; ForestPrint(BFST); return 0; }
Status CreateForest(ALGraph G, CSTree &T, Status(*CreateTree)(ALGraph G, int v, CSTree &T)) { CSTree p, q; T = NULL; int v = 0; inp(v,0,G.vexnum-1) visite[v] = unvisited; inp(v,0,G.vexnum-1) if(!visite[v]){ p = (CSTree) malloc(sizeof(CSNode)); *p = {G.Vextices[v].data, NULL, NULL}; if(!T) T = p; else q->nextsibling = p; q = p; CreateTree(G, v, q); } return true; }
Status DFSTree(ALGraph G, int v, CSTree &T) { CSTree p, q; visite[v] = visited; bool first = true; for(ArcNode *a=G.Vextices[v].firstarc; a; a=a->nextarc){ int w = a->adjvex; if(!visite[w]){ p = (CSTree)malloc(sizeof(CSNode)); *p = {G.Vextices[w].data, NULL, NULL}; if(first) { T->firstchild = p; first = false; } else q->nextsibling = p; q = p; DFSTree(G, w, q); } } return true; }
Status BFSTree(ALGraph G, int v, CSTree &T) { bool first = true; queue<QElem> Q; Q.push({T, v}); visite[v]=visited; while (!Q.empty()) { QElem e = Q.front(); Q.pop(); CSTree q = e.node; first = true; for(ArcNode *a=G.Vextices[e.v].firstarc; a; a=a->nextarc){ int w = a->adjvex; if(!visite[w]){ CSTree p = (CSTree)malloc(sizeof(CSNode)); *p = {G.Vextices[w].data, NULL, NULL}; Q.push({p, w});visite[w] = visited;
if(first){ q->firstchild = p; first = false; } else q->nextsibling = p; q = p; } } } return true; }
Status TreeTransOperator(TElemType e) { cout << e << " "; return true; } Status CSFPreRootOrder(CSTree T, Status(*Visit)(TElemType e)) { if(T == NULL){ return true; } Visit(T->data); CSFPreRootOrder(T->firstchild, Visit); CSFPreRootOrder(T->nextsibling, Visit); return true; } Status CSFPostRootOrder(CSTree T, Status(*Visit)(TElemType e)) { if(T == NULL){ return true; } CSFPostRootOrder(T->firstchild, Visit); Visit(T->data); CSFPostRootOrder(T->nextsibling, Visit); return true; } Status LevelOrderTraverse(CSTree T, Status(*Visit)(TElemType e)) { if(T == NULL){ cout << "This is an empty tree.\n"; return true; } queue<CSNode*> q; q.push(T); int i = 0, width = 1; while(!q.empty()) { CSTree P; inp(i,0,width-1){ if(!q.empty()){ P = q.front();q.pop(); q.push(P); } while(P->nextsibling){ q.push(P->nextsibling); P = P->nextsibling; } }
width = q.size(); cout << "Nodes number of the current layer: " << width << " "; inp(i,0,width-1){ if(!q.empty()){ P = q.front();q.pop(); } Visit(P->data); if(P->firstchild) q.push(P->firstchild); } cout << endl; } return true; } Status ForestPrint(CSTree T) { int i = 1; CSTree P = T; while(T){ cout << "--------------------Subtree " << i++ <<"------------------\n"; cout << "CS_Forest First root Traverse:\n"; TreeTransOperator(T->data); CSFPreRootOrder(T->firstchild, TreeTransOperator); cout << endl << "CS_Forest Post root Traverse:\n"; CSFPostRootOrder(T->firstchild, TreeTransOperator); TreeTransOperator(T->data); cout << endl; T = T->nextsibling; } cout << "CS_Forest Level Traverse:\n"; LevelOrderTraverse(P, TreeTransOperator); return true; }
|