所以这是具有左、右、父和数据的二叉搜索树的基类。
template<class Data>
class BSTNode
{
public:
/** Constructor. Initialize a BSTNode with the given Data item,
* no parent, and no children.
*/
BSTNode(const Data & d) : data(d)
{
left = right = parent = 0;
}
BSTNode<Data>* left;
BSTNode<Data>* right;
BSTNode<Data>* parent;
Data const data; // the const Data in this node.
/** Return the successor of this BSTNode in a BST, or 0 if none.
** PRECONDITION: this BSTNode is a node in a BST.
** POSTCONDITION: the BST is unchanged.
** RETURNS: the BSTNode that is the successor of this BSTNode,
** or 0 if there is none.
*/
BSTNode<Data>* successor()
{
BSTNode<Data>* cursor;
BSTNode<Data>* par;
cursor = this->right;
par = this->parent;
if (this->right != NULL)
{
while (cursor->left != NULL) {
cursor = cursor->left;
}
return cursor;
}
if ((this->right == NULL) && (this == par->left))
return this->parent;
if ((this->right == NULL) && (this == par->right))
{
do
{
cursor = par;
par = par->parent;
if (par == NULL)
{return cursor;}
} while(cursor != par->left);
return par;
}
if (this->right == NULL && this->parent == NULL)
return NULL;
return NULL;
}
};
子类是 RSTNode ,它应该使用 BSTNode 的所有成员并在其之上添加优先级:
template<class Data>
class RSTNode: public BSTNode<Data>
{
public:
int priority;
RSTNode(Data const & d)
: BSTNode<Data>(d)
{
//call a random number generator to generate a random priority
priority = rand();
}
};
现在的问题是我不确定如何实现 RSTNode 的构造函数,因为它由于某种原因无法识别 BSTNode 的成员。我知道它应该识别它们,因为它应该继承这些信息。任何帮助都会得到帮助。