因此,这是具有左,右,父级和数据的二叉搜索树的基类。

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的成员。我知道它应该识别它们,因为它应该继承此信息。任何帮助都适用。

最佳答案

好的,我在Visual Studio中对此进行了编译...

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.
};

template<class Data>
class RSTNode : public BSTNode<Data>
{
public:
   int priority;

   RSTNode(Data const & d)
      : priority(rand()),
        BSTNode<Data>(d)
   {
      left = 0; //Accessible because public
      right = 0;
      parent = 0;
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   RSTNode<std::string> node(std::string("test"));
    return 0;
}

它编译后没有访问问题。像上面的其他海报一样,在我看来,您要么未发布问题的详细信息,要么您不了解基本的知识。

>现在的问题是我不确定如何为RSTNode实现构造函数,因为由于某种原因它不识别BSTNode的成员。我知道它应该识别它们,因为它应该继承此信息。任何帮助都适用。

上面的代码实现了一个构造函数,或者如果您想专门设置左,右和父集,则需要:
BSTNode(const Data & d, BSTNode* l, BSTNode* r, BSTNode* p)
       : data(d),
       left(l),
       right(r),
       parent(p)
    {
    }

然后在RSTNode中使用它,或者为传递给该RSTNode的RSTNode使用类似的...。
RSTNode(Data const & d, BSTNode* l, BSTNode* r, BSTNode* p)
   : priority(rand()),
     BSTNode<Data>(d,l,r,p)
{
}

希望对您有所帮助,请注意,您应该更喜欢初始化程序列表来直接访问ctor中的成员。但是,如果您不能更改基类,则需要...

更正的错字-数据->数据

关于c++ - 构造函数C++的Super vs Subclass继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13082676/

10-14 16:47
查看更多