繁体   English   中英

将值插入排序不同的两个 BST

[英]Inserting values into two BSTs sorted differently

对于我在学校工作的一个项目,我必须将一个指向一个对象的指针插入到两个 BST 中。 一个 BST 按 APN(唯一键)排序,另一个按价格排序(非唯一)。 我们正在使用模板,所以我问我的教授如何做到这一点,她说使用函数指针。 当我尝试这样做时,我遇到了一些我不知道如何解决的错误。

对象定义为

class House
{
    private:
    string APN; // Unique key
    int price;  // Non-unique key

    string address;
    int bedrooms;
    double bathrooms;
    int sqFt;
}

在 main 中,在创建对象后,我尝试运行。

uniqueTree->insert(newHouse, comparePrimaryKey);
nonUniqueTree->insert(newHouse, compareSecondaryKey);

其中每个函数定义为

int comparePrimaryKey(const House* &left, const House* &right)
{
    if(left->getAPN() < right->getAPN())
        return -1;
    else
        return 1;
}

int compareSecondaryKey(const House* &left, const House* &right)
{
    if(left->getPrice() < right->getPrice())         // right > left
       return -1;
    else                                            // right < left
       return 1;
}

但我收到一个错误说

"Cannot initialize a parameter of type 'int (*)(House *const &, House *const &) 
with an lvalue of type 'int (const House *&, const House *&)'"

二叉树文件中有一个名为rootPtr的BinaryNode对象指针,insert被定义为纯虚函数。

BinaryNode<ItemType>* rootPtr;
virtual bool insert(const ItemType & newData, int compare(const 
ItemType&, const ItemType&)) = 0;

二进制节点类:

template<class T>
class BinaryNode
{   
private:
    T              item;         // Data portion
    BinaryNode<T>* leftPtr;     // Pointer to left child
    BinaryNode<T>* rightPtr;        // Pointer to right child

public:
    // constructors
    BinaryNode(const T & anItem)    {item = anItem; leftPtr = 0; rightPtr = 0;}
    BinaryNode(const T & anItem, 
           BinaryNode<T>* left, 
           BinaryNode<T>* right) {item = anItem; leftPtr = left; rightPtr = right;}
    // mutators
    void setItem(const T & anItem) {item = anItem;}
    void setLeftPtr(BinaryNode<T>* left) {leftPtr = left;}
    void setRightPtr(BinaryNode<T>* right) {rightPtr = right;}
    // accessors
    T getItem() const    {return item;}
    BinaryNode<T>* getLeftPtr() const  {return leftPtr;}
    BinaryNode<T>* getRightPtr() const {return rightPtr;}

    bool isLeaf() const {return (leftPtr == 0 && rightPtr == 0);}
}; 

在 BST 文件中,insert 定义为

template<class ItemType>
bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry, int 
compare(const ItemType &, const ItemType &))
{
    BinaryNode<ItemType>* newNodePtr = new BinaryNode<ItemType>(newEntry);
    BinaryTree<ItemType>::rootPtr = _insert(BinaryTree<ItemType>::rootPtr, 
newNodePtr, compare(newNodePtr->getItem(), BinaryTree<ItemType>::rootPtr()->getItem()));
    return true;
}

我也在 BinaryTree::rootPtr 行收到一个错误说

Called object type 'BinaryNode<House *> *' is not a function or function pointer

这是一个解决您的第一个问题的简单程序,我添加了一些函数的定义,以便我可以测试您的类和函数。

#include <iostream>
#include <string>


class House
{
    private:
    std::string APN; // Unique key
    int price;  // Non-unique key

    std::string address;
    int bedrooms;
    double bathrooms;
    int sqFt;

public:

    House(std::string apn, int prix)

    {
    APN = apn;
    price = prix;

    }
    std::string getAPN(void)

    {
    return APN;

    }


        int getPrice()
        {

        return price;

        }

};

    int comparePrimaryKey( House *const  &left,  House *const  &right)
{
    if(left->getAPN() < right->getAPN())
        return -1;
    else
        return 1;
}

int compareSecondaryKey( House *const  &left,  House *const  &right)
{
    if(left->getPrice() < right->getPrice())         // right > left
       return -1;
    else                                            // right < left
       return 1;
}

// Main function for the program
int main( )
{

    int PrimaryKey =0;
    int SecondaryKey=0;

    House right("droite",2050);
    House left("gauche",2000);



    PrimaryKey =  comparePrimaryKey(&left, &right);
    SecondaryKey =  compareSecondaryKey(&left, &right);

std::cout<< PrimaryKey << std::endl;
std::cout << SecondaryKey << std::endl;

    std::system("PAUSE");
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM