开发者

Binary Counting [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜