C# 泛型 B 树

发布于 2024-11-17 03:35:11 字数 677 浏览 2 评论 0原文

我正在使用 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”

因此,我试图找到一种方法来解决此问题,另一种选择是更改实现以保存一个用于节点的数组和一个用于记录的数组。

所以,总而言之:

  1. 在 CI 中会使用:( void*values; ) 我正在寻找 C# 中的等效项。

  2. 当我们讨论这个主题时,我是否正确理解了 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 :

  1. In C I would have used :( void* values; )
    I'm looking for an equivalent of that in C# .

  2. 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 技术交流群。

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

发布评论

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

评论(3

避讳 2024-11-24 03:35:11

您希望叶节点和内部节点的行为不同,同时仍然能够将它们都称为节点。这描述了继承层次结构:

abstract class Node<K, V>
{
    public K[] Keys { get; protected set; }
}

class LeafNode<K, V> : Node<K, V>
{
    public V[] Values { get; protected set; }
}

class InnerNode<K, V> : Node<K, V>
{
    public Node<K, V> Children { get; protected set; }
}

另一种选择是使用 C# 中的 void* 等价物,即 object,但这意味着代码不再是类型安全的,并且你必须到处都有演员阵容。我不建议这样做。

话虽这么说,你为什么要创建自己的 B 树?仅当将数据保存在磁盘上而不是内存中时才有用。如果您这样做只是为了拥有关联数组,.Net 中已经有一些类已经实现了它(例如 DictionarySortedDictionary >)并且工作得很好。

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:

abstract class Node<K, V>
{
    public K[] Keys { get; protected set; }
}

class LeafNode<K, V> : Node<K, V>
{
    public V[] Values { get; protected set; }
}

class InnerNode<K, V> : Node<K, V>
{
    public Node<K, V> Children { get; protected set; }
}

Another option would be to use the C# equivalent of void*, which is object, 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> or SortedDictionary<K,V>) and that work perfectly fine.

掀纱窥君容 2024-11-24 03:35:11

假设 _root.Values[0] = left 从类内部执行,并且 Values 是与 values 等效的属性。

如果 V 始终是 Node 的类型,您可以尝试类似的方法

interface INode {
}

class Node<K, V> : INode where V : INode {

}

,这将强制 V 从 BTree_Library.Node 派生,这将允许该行编译而不会出现任何错误。

如果 V 并不总是从 Node 派生,那么我认为泛型可能是解决这个问题的错误方法。

Assuming _root.Values[0] = left is being executed from within the class and that Values is a property equivalent to values.

If V will always be a type of Node, you can try something like

interface INode {
}

class Node<K, V> : INode where V : INode {

}

This 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.

平生欢 2024-11-24 03:35:11
class Node
{
   bool IsLeaf;
   List<TKey> Keys;
}

class InnerNode : Node
{
    List<Node> Children;
}

class LeafNode : Node
{
    List<TValue> Values;
    LeafNode Next, Prev;
}

class BPTree<TKey, TValue> 
{
    Node  Root;
}
class Node
{
   bool IsLeaf;
   List<TKey> Keys;
}

class InnerNode : Node
{
    List<Node> Children;
}

class LeafNode : Node
{
    List<TValue> Values;
    LeafNode Next, Prev;
}

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