0
Q
解决类继承算法
A
回答
1
在最后,它归结为一个DAG(有向无环图)。因此,您可以执行广度优先搜索或深度优先搜索。你只需要树木的简单情况。
例(BFS,伪代码,未经测试):
List<Array<Typespec>> flatten(Array<Pair<Typespec,Typespec>> input) {
List<Array<Typespec>> result;
Queue<Array<Typespec>*> q;
var elem=&result.append([null]);
q.append(elem);
while (!q.empty()) {
for (i in input) {
if (i.first==q.front().back()) {
var elem=&result.append(q.front().clone().append(i.second));
q.append(elem);
}
}
q.pop_front();
}
return result;
}
这是假设你的意思是[null,Person]
,而不是倒过来。请注意,它在每个结果开始时产生一个null
,与您的示例不同。
+0
我曾经见过的最糟糕的伪代码^^但请注意一点... – c0rnh0li0 2010-11-28 19:06:35
不应该第一行是'[null,Person]`? – Dialecticus 2010-11-28 13:25:05