检索二进制搜索树中的对象

时间:2015-04-28 23:23:29

标签: c++

我只想说这是我对这一切都不熟悉的事情,所以请耐心等待。因此,对于我们的任务,我们必须实现自己的二进制搜索树。我们将使用此BST来创建ExtPersonType类型的对象的地址簿(其具有不同的成员等)。

在大多数情况下,一切看起来都很好并且有效,但对于我的生活,我无法弄清楚这三个功能。我必须通过在树中搜索姓氏,月份和状态来显示该对象的所有信息。

它们都是非常相似的功能,所以我赢得了所有这些功能。如果有人可以指出我正确的方向,我相信我可以搞清楚。感谢您的帮助!

我已经删除了很多代码来达到高点。如果您希望我发布更多

,请与我们联系
BinarySearchTree
template<class T>
class BinarySearchTree
{
private:
     Node<T>* root; 
public:
    BinarySearchTree() { root = NULL; }
    void displayInfo(T value);
    bool search(T value);

template <class T>
bool BinarySearchTree<T>::search(T value)
 {
    Node<T>* tree = root;

while (tree)
{
    if (tree->value)
    {
        return true;
    }
    else if (tree->value > value)
    {
        tree = tree->left;
    }
    else
    {
        tree = tree->right;
    }
}
return false;
}

template <class T>
void BinarySearchTree<T>::displayInfo(T value)
{
Node<T>* tree = root;

while (tree)
{
    if (tree->value)
    {
        cout << tree->value;//I have overloaded << here to display objects (works btw)
    }
    else if (tree->value > value)
    {
        tree = tree->left;
    }
    else
    {
        tree = tree->right;
    }
    }
}

AddressBook.cpp。

AddressBook<ExtPersonType> addressBook;
ExtPersonType person;

int main()
{
   //reads input from file
        person.setInfo(firstName, lastName, 
                             month, day, year, 
                             street, city, state, zipCode, 
                             phoneNumber, status);

        addressBook.insert(person);
      }
}

void optionThree()  //The crux of my problem
{
 string lastName;
 cout << "Enter the last name of the person: ";
 cin >> lastName;
 addressBook.printInfoOf(lastName);
}

AddressBook.h

template <class elemType>
class AddressBook : public BinarySearchTree<elemType>
{
public:
 AddressBook();
 void printInfoOf(string);
 void printNameInTheMonth(int);
 void printNamesWithStatus(string);
};


// Print - Info Of
template <class elemType>
void AddressBook<elemType>::printInfoOf(string lastName)
{
  if(person.getLastName() == (last))
    BinarySearchTree::displayInfo(person);
  else
    cout << "Not found" << endl;
}

正如你所看到的,我不知道我在做什么。再次感谢你!

  

错误C2451:类型&#39; ExtPersonType&#39;的条件表达式是非法的   没有可用于执行此转换的用户定义转换运算符,>或者无法调用运算符   documents \ visual studio 2010 \ projects \ programming4 \ binarysearchtree.h(234):&gt;同时编译类模板成员函数&#39; void&gt; BinarySearchTree :: displayInfo(T)&#39;            同            [                T = ExtPersonType            ]

2 个答案:

答案 0 :(得分:1)

BinarySearchTree::displayInfo方法中,您有以下一行:

else if (tree->value > value)

这一行在ExtPersonType类型的两个对象之间进行了大于比较,但是对于自定义类没有默认的operator>

如果您还没有实现类似

的内容
bool operator>(const ExtPersonType & other) const { ... }

ExtPersonType课程中,那就是你的问题。

不要忘记,如果您实施operator>,您还应该实施operator<operator>=operator<=

通常,应将operator>operator<=视为相关对,将operator<operator>=视为另一个相关对,并将每对中的一个视为否定另一个,例如:

bool operator>(const ExtPersonType & other) const {
    return !(*this <= other);
}

bool operator<=(const ExtPersonType & other) const {
    // Do your actual comparison here
}

如果您需要更改两个ExtPersonType对象彼此之间的关系,那么以这种方式实施比较可以减少出错的可能性。

答案 1 :(得分:0)

感谢所有回复的人。内森,你在问题的一部分是对的,我没有比较价值(树 - >值==值)。另一部分是关于我如何传递参数的问题,但我现在已经完成了整个工作。谢谢!