Binary Counting [closed]
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.
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.
精彩评论