簡體   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