B+树节点实现
B+ Tree node implementation
Im为类实现一个B+树。节点目前是这样实现的:
class Node {
public:
E* keys[order*2];
Node *children[order*2+1];
int size;
Node(){
size = 0;
}
bool empty() {
return size == 0;
}
bool isLeafNode() {
return false;
}
};
class LeafNode : public Node {
public:
E* data[order*2+1];
bool isLeafNode() {
return true;
}
};
当我想向叶节点添加元素时(通过访问LeafNode->data(,我会得到
error: request for member ‘data’ in ‘left<int>’, which is of non-class type ‘BTree<int>::LeafNode*()’
我想发生这种情况是因为编译器不知道我访问的节点是内部节点还是叶节点,尽管我首先使用isLeafNode((进行检查。我无法将这两个类合并为一个类,因为叶节点比内部节点需要多一个Bucket来处理数据。
我意识到这是一个设计问题,但我是否缺少一些解决这个问题的琐碎方法?我对C++还相当陌生。
对于这样的事情,您确实应该使用虚拟方法。您可以更改isLeafNode()
查询以返回指向叶节点的指针(如果是(,否则返回NULL。
class LeafNode; // forward declare
class Node {
//...
public:
virtual ~Node () {}
virtual LeafNode * isLeafNode () { return 0; }
//...
};
class LeafNode : public Node {
//...
public:
LeafNode * isLeafNode () { return this; }
//...
};
然后,如果data
实际上是LeafNode
,则可以从Node
使用此方法来访问它。
错误消息
error: request for member ‘data’ in ‘left<int>’, which is of non-class type ‘BTree<int>::LeafNode*()’
而此表单的其他错误通常意味着您正试图使用.
访问struct
的字段,而您应该使用->
。例如,如果你有
LeafNode* ptr = /* ... */;
ptr.data[0] = /* ... */;
您将在第二行得到一个错误,因为您使用的是.
而不是->
。
试着看看这是否是指示线上的错误,如果是,请将点改为箭头。
希望这能有所帮助!
相关文章:
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 没有从"树节点"到"树节点*"的可行转换
- 语言分析的二叉树实现 - 子节点:不起作用
- 对树节点使用 CString 时出现内存泄漏
- 预分配的节点向量中的无锁树节点分配
- 接受来自键盘的树节点以确定其高度
- C++ Eclipse:二叉搜索树节点>数据=变量似乎不起作用
- C++错误(从不兼容的类型"void"分配给树节点*)
- UWP:在树节点中将文本设置为粗体
- 树节点之间的最大距离中的运行时错误
- 想要以最新的添加节点完整返回树节点的根
- 如何在 C++ 与 CPLEX 中实现宏节点回调
- 使用深度第一次搜索的无向图中的图形调查无法实现的节点
- 从GCC功能树节点中检索函数参数
- 如何映射数组元素以选择二进制树节点
- Boost::p tree - 访问列表中包含的属性树节点
- 我应该如何根据 Liskov 原则实现树节点类
- B+树节点实现
- AVL树实现有节点或没有节点
- 如何在树访问器中实现每个节点的缓存