在 C++ 中设计 B+Tree 模板类时出现问题

发布于 2024-09-27 22:11:50 字数 191 浏览 2 评论 0原文

我正在尝试编写 B+Tree 的通用 C++ 实现。我的问题来自于 B+Tree 中有两种节点;内部节点(包含键和指向子节点的指针)和叶节点(包含键和值),内部节点中的指针可以指向其他内部节点或叶节点。 我无法弄清楚如何使用模板来建模这种关系(我不想使用强制转换或虚拟类)。

希望有一个解决我的问题的方法或者更好的方法在 C++ 中实现 B+Tree。

I'm trying to write a generic C++ implementation of B+Tree. My problem comes from the fact that there are two kinds of nodes in a B+Tree; the internal nodes, which contain keys and pointers to child nodes, and leaf nodes, which contain keys and values, and pointers in an internal node can either point to other internal nodes, or leaf nodes.
I can't figure how to model such a relationship with templates (I don't want to use casts or virtual classes).

Hope there is a solution to my problem or a better way to implement B+Tree in C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初吻给了烟 2024-10-04 22:11:50

最简单的方法:

bool mIsInternalPointer;
union {
  InternalNode<T>* mInternalNode;
  LeafNode<T>* mLeafNode;
};

使用 boost::variant 可以稍微简化一下:)

The simplest way:

bool mIsInternalPointer;
union {
  InternalNode<T>* mInternalNode;
  LeafNode<T>* mLeafNode;
};

This can be somewhat simplified by using boost::variant :)

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