简体   繁体   English

如何在关系数据库中存储二进制空间分区树?

[英]How can I store a binary space partitioning tree in a relational database?

I'm trying to store the data in a binary space partitioning tree in a relational database. 我试图将数据存储在关系数据库的二进制空间分区树中。 The tricky part about this data structure is it has two different types of nodes. 关于此数据结构的棘手部分是它具有两种不同类型的节点。 The first type, which we call a data node, simply holds a certain number of items. 第一种类型,我们称为数据节点,仅包含一定数量的项目。 We define the maximum number of items able to be held as t . 我们将可保存的最大项目数定义为t The second type, which we refer to as a container node, holds two other child nodes. 第二种类型,我们称为容器节点,包含另外两个子节点。 When an item is added to the tree, the nodes are recursed until a data node is found. 将项目添加到树时,将递归节点,直到找到数据节点为止。 If the number of items in the data node are less than t , then the item is inserted into the data node. 如果数据节点中的项目数小于t ,则将项目插入到数据节点中。 Otherwise the data node is split into two other data nodes, and is replaced by one of the container nodes. 否则,该数据节点将拆分为其他两个数据节点,并由容器节点之一替换。 When an element is deleted, a reverse process must happen. 删除元素时,必须执行相反的过程。

I'm a little bit lost. 我有点迷路了。 How am I supposed to make this work using a relational model? 我应该如何使用关系模型来完成这项工作?

Why not have two tables, one for nodes and one for items? 为什么没有两个表,一个用于节点,一个用于项目? (Note that I used the term "leaf" instead of "data" nodes below when I wrote my answer; a "leaf" node has data items, a non-"leaf" node contains other nodes.) (请注意,在我写答案时,在下面使用术语“叶子”而不是“数据”节点;“叶子”节点具有数据项,非“叶子”节点包含其他节点。)

The node table would have columns like this: id primary key, parentid references node, leaf boolean and in addition some columns to describe the spatial bounaries of the node and how it will/has been split. 节点表将具有以下列: id primary key, parentid references node, leaf boolean ,此外,还有一些列用于描述节点的空间边界以及如何/将其拆分。 (I don't know if you're working in 2D or 3D so I haven't given details on the geometry.) (我不知道您是在2D还是3D模式下工作,因此我没有提供有关几何的详细信息。)

The data table would have id primary key, leafid references node and whatever data. 数据表将具有id primary key, leafid references node以及任何数据。

You can traverse the tree downward by issuing SELECT * FROM node WHERE parentid = ? 您可以通过发出SELECT * FROM node WHERE parentid = ?向下遍历树SELECT * FROM node WHERE parentid = ? queries at each level and checking which child to descend into. 在每个级别进行查询,并检查要归入哪个子级。 Adding a data item to a leaf is a simple INSERT . 向叶子添加数据项是一个简单的INSERT Splitting a node requires unsetting the leaf flag, inserting two new leaf nodes, and updating all the data items in the node to point to the appropriate child node by changing their leafid values. 拆分节点需要取消设置叶子标志,插入两个新的叶子节点,并通过更改其叶子标识值来更新节点中的所有数据项以指向适当的子节点。

Note that SQL round trips can be expensive, so if you're looking to use this for a real application, consider using a relatively large t in the DB constructing a finer-grained tree in memory of the leaves you are interested in after you have the data items. 请注意,SQL往返可能会很昂贵,因此,如果您想在实际的应用程序中使用它,请考虑在数据库中使用相对较大的t来构造细粒度的树,以存储您感兴趣的叶子之后数据项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM