【发布时间】:2012-04-01 16:51:49
【问题描述】:
这个问题通常与语言无关(尽管如果这很重要,我使用的是 Java/Processing)。我需要使用一些数据库输出绘制一个树状图。规则很简单:
图只有一个根。 每个节点可以有多个子节点,但只有一个父节点。
我在思考如何将数据提供给绘图函数时遇到了麻烦。我从中获取数据的数据库(它是 Rails/MySQL)具有以下结构(我使用伪代码来避免不必要的自引用 has_many :通过详细信息):
Graph
has_many :nodes
Node
has_many :siblings
has_one :parent
has_many :children
问题是 - 我应该如何组织我将节点信息放入的数组以及如何遍历该数组?算法的伪代码将是惊人的。
如果您发现缺少某些信息无法获得答案,请告诉我,我很乐意扩展问题/添加数据。
附:我并不是真的在寻找一些可以为我绘制图表的框架/服务的建议,我实际上需要使用我当前的工具来做到这一点。
谢谢!
编辑:
这就是我现在绘制图形的方式...它从像 ['0root', '2child', 2child2', '4child_child'] 这样的数组中一个接一个地获取单词,并假设 if (int) 前缀重构树数组的下一个元素大 2 它是一个子元素,如果它小 2,它是上一级(父元素的兄弟)。相对于兄弟姐妹的位置是根据数组中具有相同前缀的元素总数计算得出的。我想知道是否有更优雅的方法来做到这一点..?
图形(){ wordList = new ArrayList(); nodeList = new ArrayList(); sibList = new ArrayList(); }
Integer sibNumber(int idx<char>) {
Map<Integer, Integer> sibCount = new HashMap<Integer, Integer>();
for (Integer sibling: sibList) {
Integer count = sibCount.get(sibling);
sibCount.put(sibling, (count==null) ? 1 : count+1);
}
Integer result = sibCount.get(idx);
return result;
}
void update(String wrd) {
wordList.add(wrd);
String word = wrd;
String[][] m = matchAll(wrd, "(\\d*)");
int index = (int) m[0][0];
char number = (char) index;
sibList.add(index);
// println(wrd);
word = word.replaceAll(number,"");
Integer siblingsNum = sibNumber(index);
if (sibList.size()>=2) {
lastIndex = sibList.get(sibList.size()-2);
int indexDiff = index-lastIndex;
if (indexDiff != 0) {
for (int i=sibList.size()-1; i>0; i--) {
if (index-sibList.get(i) == 2) {
parNode = nodeList.get(i);
break;
}
}
}
}
if (index == 2) {
parNode = nodeList.get(0);
}
node = new Node(word, index, siblingsNum, parNode);
nodeList.add(node);
}
void clean() {
nodeList = new ArrayList<Node>();
sibList = new ArrayList<Integer>();
}
void display() {
for (int i = 0; i < nodeList.size(); i++) {
nd = nodeList.get(i);
if (nd.parent != null) {
stroke(255, 0, 0);
VerletSpring2D spring=new VerletSpring2D(nd.origin,nd.parent.origin,80,0.01);
physics.addParticle(nd.origin);
physics.addSpring(spring);
line(nd.origin.x+nd.w/2, nd.origin.y, nd.parent.origin.x+nd.w/2, nd.parent.origin.y+nd.parent.h);
}
nd.display();
}
}
}
【问题讨论】:
-
不使用数组,为什么不能有一个适合目的的名为“Node”的类?
标签: java graph tree pseudocode graph-algorithm