0

我正在重写一个旧项目。出于神秘的原因(遗留公司数据库,无法更改),树形数据以一种特殊的方式存储在数据库中:每个节点都定义了两个属性:节点深度和最底层深度子​​节点的列表。

我将如何处理将其转换为常规树?我目前处于将一组所有节点放置在树中的级别,但我现在不知所措。我想到的一件事是从最深层次添加节点并上升到根节点,但这很麻烦悬空节点和调整树的大小。

编辑:刚刚意识到我的方法将涉及检查较低级别节点的每个组合,以找到其子节点总和等于较高级别节点的节点。没有。

4

1 回答 1

0

Just sort the nodes on depth and insert the nodes level by level.

There is only one root (depth 0 or 1, depending on your input), so the first step is easy.

At a generic step, you need to assign each node of the next level to the correct parent. The criterion is simple: the bottom-most children of the child node are a subset of the bottom-most children of the parent. If running time is not an issue, just check each child node against all parent nodes until you find a match. If that is too slow, please add a comment and I will think some more ;-)

于 2013-09-25T07:12:35.997 回答