二进制计数
我的数据结构是这样的:
+-------------+------+-------+
| USERID (PK) | LEFT | RIGHT |
+-------------+------+-------+
| 001 | | 002 |
| 002 | 003 | 004 |
| 003 | 005 | |
| 004 | | |
| 005 | 008 | 007 |
| 008 | | |
| 007 | 009 | |
| 009 | | |
+-------------+------+-------+
这个数据结构代表一棵二叉树。每行代表一个节点,每个节点都有一个USERID
。 LEFT
和 RIGHT
列中的条目通过使用 USERID
引用来表示该节点的两个子节点。我想遍历这棵树。
我正在使用带有 Access 数据库的 Visual Studio 2005。
my data structure is like this:
+-------------+------+-------+
| USERID (PK) | LEFT | RIGHT |
+-------------+------+-------+
| 001 | | 002 |
| 002 | 003 | 004 |
| 003 | 005 | |
| 004 | | |
| 005 | 008 | 007 |
| 008 | | |
| 007 | 009 | |
| 009 | | |
+-------------+------+-------+
This data structure represents a binary tree. Each row represents a node, each having a USERID
. The entries in the LEFT
and RIGHT
columns represent the two children of that node by referring them with USERID
s. I want to traverse this tree.
I am using Visual Studio 2005 with an Access database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的最好答案是,您选择了完全错误的表示数据的方式。
表示用户 ID 的更明智的方法是(我猜)表中的一个简单列保存用户的详细信息,并使用索引来快速查找。然后“遍历”退化为简单的选择并迭代结果集。
如果您想继续使用当前的(IMO 愚蠢的)表结构,那么 SQL 将不会帮助您遍历树。如果您尝试遍历数据库中的树,您最终将对树中的每个节点进行选择,这将非常慢。
最好的选择是选择表的所有行,在内存中构建一棵树,然后遍历该树。
The best answer I can come up with is that you've picked completely the wrong way of representing your data.
A far more sensible way to represent the user ids is as a simple column in (I guess) the table that holds the details for the users, with an index to give you fast lookup. Then "traversal" degenerates to a simple select and iterating over the resultset.
If you want to proceed with your current (IMO silly) table structure, then SQL is not going to help you to do traversal of the tree. If you try to traverse the tree in the database, you'll end up doing a select for each node in the tree which will be horribly slow.
Your best bet is to select all rows of the table, construct a tree in memory, and traverse that tree.