我正在尝试为2-3树构建构造函数。但我不知道如何处理这个问题。我们创建的节点只有一个值,两个指针(左和中)从一开始就与该节点相关联,或者一个节点有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;
}
答案 0 :(得分:0)
我认为您应该首先以这种方式声明树:
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;
}
然后,BNode()
应该包含叶子上的一个或两个元素,BTree()
应该包含一个列表或/和一个初始化列表,其中包含对象列表(2或3个对象?)每个叶子如果使用C ++ 11。您应该根据此图形构建从根到最右下叶的树:http://en.wikipedia.org/wiki/2%E2%80%933_tree