2-3树以及如何构建构造函数
2-3 tree and how to build the constructor
我正在尝试为2-3树构建构造函数。但我不知道该怎么做。我们是创建一个从一开始就只有一个值和两个指针(左和中)与该节点关联的节点,还是创建一个有两个值和三个指针(右、中和左)的节点?
这是我的2-3节点类
template<class E>
class BNode
{
public:
// I'm not sure if this is quite enough for the constructor
BNode() {_right == nullptr;};
typedef struct Entry
{
E value;
BNode<E>* _left;
// Constructor for the entry
Entry();
};
E fValue() {return _first.value;}
E sValue() {return _second.value;}
BNode<E>* fLeft() {return _first._left;}
BNode<E>* sLeft() {return _second._left;}
//check if the node is 2 or 3 pointers
bool IsThree()
{
if(_first._left != nullptr && _second._left != nullptr
&& _second._right != nullptr)
_three = true;
else
_three = false;
return _three;
}
Entry fEntry() {return _first;}
Entry sEntry() {return _second;}
BNode<E>* right() {return _right;}
private:
bool _three;
Entry _first, _second;
BNode<E>* _right;
};
template<class E>
BNode<E>::Entry::Entry()
{
_left = nullptr;
}
我认为您应该首先以这种方式声明树:
template<class E>
class BTree
{
public:
BTree();
~BTree();
class BNode
{
public:
// I'm not sure if this is quite enough for the constructor
BNode() {_right == nullptr;};
typedef struct Entry
{
E value;
BNode<E>* _left;
// Constructor for the entry
Entry();
};
E fValue() {return _first.value;}
E sValue() {return _second.value;}
BNode<E>* fLeft() {return _first._left;}
BNode<E>* sLeft() {return _second._left;}
//check if the node is 2 or 3 pointers
bool IsThree()
{
if(_first._left != nullptr && _second._left != nullptr
&& _second._right != nullptr)
_three = true;
else
_three = false;
return _three;
}
Entry fEntry() {return _first;}
Entry sEntry() {return _second;}
BNode<E>* right() {return _right;}
private:
bool _three;
Entry _first, _second;
BNode<E>* _right;
};
private:
BNode* root_;
};
template<class E>
BTree<E>::BNode<E>::Entry::Entry()
{
_left = nullptr;
}
然后,如果使用C++11,BNode()
应该包含叶上的一个或两个元素,BTree()
应该包含一个列表或/和一个初始化器列表,其中包含每个叶上的对象列表(2或3个对象?)。您应该根据以下图纸从根开始构建树,直到最右下的叶子:http://en.wikipedia.org/wiki/2%E2%80%933_tree
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- pair的两个构造函数几乎相同,为什么不生成构建错误?
- 2个模板构造函数的组合构建与变异模板.如何
- 为链接列表构建迭代器类(错误:无匹配的构造函数以初始化)
- 是否可以使用多个参数构建类的构造函数
- 在C 构建器中超载Mainform构造函数
- 找不到构造函数 - 如何将 CPP 文件添加到 Qt 构建过程
- 通过转发构造函数参数来构建基于可变参数模板的 mixin
- 我如何构建具有构造函数参数的对象的向量
- 使用子字符串构造函数构建字符串 - 调试 - 当自动跳过代码时
- 2-3树以及如何构建构造函数
- clang与gcc运行时的区别:用clang构建的c++类模板在没有复制构造函数的情况下崩溃,用复制构造函数构建的gcc
- 可变模板构造函数来构建父模板
- 如何构建一个合适的构造函数和析构函数