C# 泛型 B 树
我正在使用 C# 实现 B+ 树。
现在,据我了解,树节点应该保存许多(顺序 - 1)键,以及指向记录或其他节点的指针的顺序号,即,只有叶节点将保存指向记录的实际指针,而内部节点将保存指向其他节点的指针。
我在此实现中遇到的问题是 C# 泛型,
节点类声明为:
class Node< K,V >
{
K [] keys ;
V [] values;
}
现在,当我尝试将节点放入值数组中时,
_root.Values[0] = left ; // left being of type Node<K,V>
出现以下错误:
无法将类型“BTree_Library.Node”隐式转换为“V”
因此,我试图找到一种方法来解决此问题,另一种选择是更改实现以保存一个用于节点的数组和一个用于记录的数组。
所以,总而言之:
在 CI 中会使用:
( void*values; )
我正在寻找 C# 中的等效项。当我们讨论这个主题时,我是否正确理解了 B+树结构 参考节点和记录可互换的节点指针?
I'm implementing a B+tree using C#.
Now as i understand it, a tree node should hold a number of (order - 1) keys, and order number of pointers to records or other nodes, i.e., only the leaf nodes would hold actual pointers to records , and internal nodes would hold pointers to other nodes.
The problem I'm having with this implementation is with C# generics
the Node Class is Declared as:
class Node< K,V >
{
K [] keys ;
V [] values;
}
Now when i attempt to place a node into the values array as such
_root.Values[0] = left ; // left being of type Node<K,V>
I get the following Error:
Cannot implicitly convert type 'BTree_Library.Node' to 'V'
So, I'm trying to find a way to work around this, the other option is to change the implementation to hold one array for nodes and one for records.
So, in conclusion :
In C I would have used :
( void* values; )
I'm looking for an equivalent of that in C# .While we're on the subject did I understand the B+tree structure correctly
in reference to nodes and records being interchangeable for the nodes pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望叶节点和内部节点的行为不同,同时仍然能够将它们都称为节点。这描述了继承层次结构:
另一种选择是使用 C# 中的
void*
等价物,即object
,但这意味着代码不再是类型安全的,并且你必须到处都有演员阵容。我不建议这样做。话虽这么说,你为什么要创建自己的 B 树?仅当将数据保存在磁盘上而不是内存中时才有用。如果您这样做只是为了拥有关联数组,.Net 中已经有一些类已经实现了它(例如
Dictionary
或SortedDictionary
>)并且工作得很好。You want your leaf nodes and your inner nodes to act differently, while still being able to refer to both of them as nodes. That describes an inheritance hierarchy:
Another option would be to use the C# equivalent of
void*
, which isobject
, but that would mean the code is not type-safe anymore and you would have to have casts everywhere. I would not recommend doing this.That being said, why are you creating your own B-tree? It's useful only when keeping the data on disk, not in memory. If you're doing it only to have associative array, there are classes in .Net that already implement it (like
Dictionary<K,V>
orSortedDictionary<K,V>
) and that work perfectly fine.假设
_root.Values[0] = left
从类内部执行,并且Values
是与values
等效的属性。如果 V 始终是
Node
的类型,您可以尝试类似的方法,这将强制 V 从
BTree_Library.Node
派生,这将允许该行编译而不会出现任何错误。如果 V 并不总是从
Node
派生,那么我认为泛型可能是解决这个问题的错误方法。Assuming
_root.Values[0] = left
is being executed from within the class and thatValues
is a property equivalent tovalues
.If V will always be a type of
Node
, you can try something likeThis will force V to derive from
BTree_Library.Node
, which will allow that line to compile without any errors.If V will not always derive from
Node
, then I think generics might be the wrong way to tackle this.