我正在开发一个使用Sqlite的Android应用程序,我有一个树形结构,我在数据库中表示如下:
+------+------+-------+------+
|comp_id nodeId parent| text |
|------|------|-------|------|
| 146 | 1 | -1 | Top |
| | | | |
| 146 | 2 | 1 | Ch1 |
| | | | |
| 146 | 3 | 2 | Leaf |
| | | | |
| ... | | | |
| 152 | 1 | -1 | Top |
+------+------+-------+------+
我在编写一个算法时遇到了困难,希望能够以下面这种自包含的方法来返回任意节点下的所有叶子节点。
Node
{
public Node[] getAllLeafs()
{
// traverse all the way down the tree
// and get only leafs
}
}
如果通过修改我的表结构和/或使用SQL能够更轻松地完成此操作,请在翻译中提到,因为我能够这样做。
comp_id
的意思是什么?展示一个期望输出的例子! - CL.comp_id
意味着编译 ID,就像一本书,节点是章节名称(可能有子章节),而叶子节点就像页面或段落。这是我能给出的最接近的例子。 - sprocket12