家谱展示
我正在创建一个家谱程序。我的问题是如何定位节点?最初我将根定位在屏幕的中心,如果它是一个完美的二叉树并且级别非常少,它就可以正常工作。然而,大多数情况并非如此。这是一个示例树:-
A
B C
D E F I J
K L N O
正如您所看到的,主要问题在于节点的位置。如果一个节点有很多子节点,并且它的相邻节点也有很多子节点,它们往往会重叠。(主要问题) 我在 Silverlight 中使用 Canvas 对节点进行绝对定位。如果您不是 Silverlight 开发人员,您可能不会关心 Silverlight 和 Canvas 部分。我只需要如何定位节点的逻辑。
通过知道树的总层数,可以相当容易地计算树的高度,但树的宽度让我感到困扰。我如何计算树的宽度(画布的总宽度)
有人可以给我一些关于如何设置画布宽度以及什么逻辑最适合节点定位的一般准则。
注意:-我并不是要求整个算法,这也不是我的作业。我已经有了算法和数据库。我只需要节点定位部分的指导。
提前致谢 :)
I am creating a family tree program. My question is how do i position the nodes? Originally i positioned the root at the center of my screen and it works fine if it is a perfect binary tree and the levels are very less. However it is not most often the case. This is a sample tree :-
A
B C
D E F I J
K L N O
As you can see, the main problem is regarding the position of the nodes. If a node has many childs and it's adjacent node also has many children, they tend to overlap.(MAIN PROBLEM) I am using absolute positioning of the node using Canvas in Silverlight. You may not bother with the Silverlight and Canvas part if you are not a Silverlight developer. I just need the logic of how to position the nodes.
The height of the tree can be computed fairly easily by knowing the total number of levels of tree but the width of the tree is what is troubling me. How can i calculate the width of the tree (total width of the canvas)
Can somebody give me some general guidelines regarding how to set the width of the canvas and what logic will work perfect for the positioning of the nodes.
NOTE :- I am not asking for the whole algorithm and it is not my homework. I already have the algorithm and database. I just need guideline for the positioning part of the node.
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你为这棵树的任意节点实现一个函数:
width(node)
,那么很容易定位每个节点这个函数可以递归地定义:
- 对于高度为 1 的树,这正是该节点的长度
- 对于高度大于 1 的树,这是该节点的所有直接子节点的长度之和(加上这些节点之间的一些空格)
If you implement a function:
width(node)
for arbitrary node of this tree, it is easy to positioning each nodeThis function may be defined recursively:
- for a tree of height 1 it tree this is exactly length of this node
- for a tree of height bigger than 1 this is a sum of lengths of all direct children of this node (plus some spaces between those)
如果您想猜测画布的宽度,我建议从树的最宽级别开始。可以通过广度优先遍历树来计算。将该级别的节点数乘以每个节点所需的横向空间量,就得到了所需的画布宽度。
但是,这并不能保证最广泛级别上的相邻节点不会都有很多子节点。因此,要执行不重叠的间距,首先将树的叶子定位在最深的位置,然后向后遍历树,在上面添加父级并将叶子放入间隙和侧面。
I would recommend starting with the widest level of the tree if you want to guess the width of the canvas. You can calculate by traversing the tree breadth-first. Multiply the number of nodes at that level by the amount of lateral space each node needs and you have the width of canvas you require.
However, that's no guarantee that adjacent nodes on the widest level won't each have many children. So, to perform spacing with no overlap, start by positioning the leaves of the tree at the deepest level and traverse the tree backwards, adding parents above and putting leaves into the gaps and at the sides.
我建议提供放大和缩小功能,以整理 GUI 空间。
具有许多子节点的节点可以进行分组,并且有一个特殊的图标来表示它可以放大到下一个级别,我觉得随着家庭的成长,用户可以首先获得大图片,然后可以放大到他想要的任何分支也有愿望。
从谷歌地图的用户界面中获取线索,可能会有所帮助。
I would suggest to give zoom in and zoom out functionality to unclutter the GUI Real Estate.
A Node with many children can be grouped and a special icon to denote it can be zoomed in to next level would be good i feel, as the family grows, as user can get big picture at first and as then can zoom into any branch he wishes too.
Take cues from google map's UI, might help.