有向图DFS森林

下午效率不是很高。

在收尾的时候把两棵树的显示分开了,更有“森林”的感觉。

但是美中不足的每棵树的最后一个节点还是有“->",虽然我已经模仿Yan版教材加上了begin&end,稍微看着不那么突兀。

本想写个判断把最后那个箭头删了,目前还是没有成功……

  1 #include <iostream>
2 using namespace std;
3
4 template <class TElemType>
5 struct Treenode
6 {
7 TElemType data;
8 Treenode<TElemType> *lchild,*nextsibling;
9 }; //用于深度优先生成森林
10
11
12 template <class TElemType>
13 class Graph
14 {
15 public:
16 void CreateAlgraph();
17 void DFSForest(Treenode<TElemType> *&T);
18 void DFSTree(int v,Treenode<TElemType> *&T,bool *visited);
19
20 private:
21
22 struct Arcnode
23 {
24 int adjvex;
25 Arcnode *nextarc;
26 float weight;
27 };
28
29 template <class TElemType>
30 struct Vexnode
31 {
32 TElemType data;
33 Arcnode *firarc;
34 };
35 struct ALgraph
36 {
37 int vexnum;
38 int arcnum;
39 bool kind;
40 Vexnode<TElemType> *vex;
41 };
42 ALgraph algraph; //邻接表存储
43
44
45 };
46
47
48 template <class TElemType>
49 void Graph<TElemType>::CreateAlgraph()
50 {
51 int i,j,m,n;
52 float w;
53 TElemType v1,v2;
54 Arcnode *p;
55 cout << "输入图类型(1是无向图,0是有向图):" << endl;
56 cin >> algraph.kind;
57 cout << "输入顶点数和边数:" << endl;
58 cin >> algraph.vexnum >> algraph.arcnum;
59 algraph.vex = (Vexnode<TElemType> *)malloc(algraph.vexnum * sizeof(Vexnode<TElemType>));
60 cout << "输入顶点信息:" << endl;
61 for(i = 0;i < algraph.vexnum;i++)
62 {
63 cin >> algraph.vex[i].data;
64 algraph.vex[i].firarc = NULL;
65 }
66
67 if(algraph.kind)
68 {
69 cout << "输入各边依附的两点和权值:" << endl;
70 for(i = 0;i < algraph.arcnum;i++)
71 {
72 cin >> v1 >> v2 >>w;
73 for(j = 0;j < algraph.vexnum;j++)
74 {
75 if(v1 == algraph.vex[j].data) m = j;
76 if(v2 == algraph.vex[j].data) n = j;
77 }
78 p = (Arcnode *)malloc(2*sizeof(Arcnode));
79 p[0].adjvex = n;p[0].weight = w;
80 p[1].adjvex = m;p[1].weight = w;
81 p[0].nextarc = algraph.vex[m].firarc;algraph.vex[m].firarc = p;
82 p[1].nextarc = algraph.vex[n].firarc;algraph.vex[n].firarc = ++p;
83 }
84 }
85
86 else
87 {
88 cout << "输入各边的弧尾与弧头结点及有向边的权值:" << endl;
89 for(i = 0;i < algraph.arcnum;i++)
90 {
91 cin >> v1 >> v2 >> w;
92 for(j = 0;j < algraph.vexnum;j++)
93 {
94 if(v1 == algraph.vex[j].data) m = j;
95 if(v2 == algraph.vex[j].data) n = j;
96 }
97 p = (Arcnode *)malloc(sizeof(Arcnode));
98 p->adjvex = n;p->weight = w;
99 p->nextarc = algraph.vex[m].firarc;algraph.vex[m].firarc = p;
100 }
101 }
102 } //构造完成
103
104
105
106 template <class TElemType>
107 void Graph<TElemType>::DFSForest(Treenode<TElemType> *&T)
108 {
109 Treenode<TElemType> *q,*r;
110 T = NULL;
111 int v;
112 bool *visited = (bool *)malloc(algraph.vexnum * sizeof(bool));
113 for(int i = 0;i < algraph.vexnum;i++)
114 visited[i] = false;
115 cout << "begin!: ";
116 for(v = 0;v < algraph.vexnum;v++)
117 {
118 if(!visited[v])
119 {
120 visited[v] = true;
121 r = (Treenode<TElemType> *)malloc(sizeof(Treenode<TElemType>));
122 r->data = algraph.vex[v].data;
123
124 r->lchild = r->nextsibling = NULL;
125 if(!T)
126 {
127 T = r;
128 cout << r->data << "->"<<"";//第一棵树的头结点情况
129 }
130
131 else
132 { cout <<endl<<""<< r->data << "->"<<"";//另一棵树的头结点
133 q->nextsibling = r;
134 }
135 q = r;
136 DFSTree(v,r,visited);
137 }
138 }
139 cout<<"end!";
140 free(visited);
141 }
142
143
144 template <class TElemType>
145 void Graph<TElemType>::DFSTree(int v,Treenode<TElemType> *&T,bool *visited)
146 {
147 Arcnode *w;
148 Treenode<TElemType> *p,*q;
149 int v1;
150 bool first = true;
151 for(w = algraph.vex[v].firarc;w;w = w->nextarc)
152 {
153 v1 = w->adjvex;
154 if(!visited[v1])
155 {
156 visited[v1] = true;
157 p = (Treenode<TElemType> *)malloc(sizeof(Treenode<TElemType>));
158 p->data = algraph.vex[v1].data;
159 cout << p->data << "->";
160 p->lchild = p->nextsibling = NULL;
161 if(first) {T->lchild = p;first = false;}
162 else q->nextsibling = p;
163 q = p;
164 DFSTree(v1,p,visited);
165 }
166 }
167 } //EndDFSTree
168
169
170
171 int main()
172 {
173 /*Graph<int> gph;
174 gph.CreateDN();
175 int a,b;
176 cout << "输入终点和起点:";
177 cin >> a >> b;
178 gph.Shortestpath_DIJ(a,b);
179 gph.DestroyDN();
180 return 0;*/
181 Treenode<int> *t;
182 Graph<int> gph;
183
184
185 //定义一个节点,准备传给DFSForest()
186
187 gph.CreateAlgraph();
188 gph.DFSForest(t);
189
190 return 0;
191 }



posted @ 2012-02-23 18:59  uniquews  阅读(1058)  评论(0编辑  收藏  举报