二进制计数

发布于 2024-10-01 04:12:19 字数 617 浏览 0 评论 0原文

我的数据结构是这样的:

+-------------+------+-------+
| USERID (PK) | LEFT | RIGHT |
+-------------+------+-------+
|     001     |      |  002  |
|     002     | 003  |  004  |
|     003     | 005  |       |
|     004     |      |       |
|     005     | 008  |  007  |
|     008     |      |       |
|     007     | 009  |       |
|     009     |      |       |
+-------------+------+-------+

这个数据结构代表一棵二叉树。每行代表一个节点,每个节点都有一个USERIDLEFTRIGHT 列中的条目通过使用 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 USERIDs. I want to traverse this tree.

I am using Visual Studio 2005 with an Access database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-10-08 04:12:19

我能想到的最好答案是,您选择了完全错误的表示数据的方式。

表示用户 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文