gpt4 book ai didi

c++ - 在二叉搜索树中检索对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:32 28 4
gpt4 key购买 nike

我只想说我对这一切都不陌生,所以请多多包涵。因此,对于我们的任务,我们必须实现我们自己的二叉搜索树。我们将使用此 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;
}
}
}

地址簿.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);
}

地址簿.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;
}

如您所见,我不知道自己在做什么。再次感谢您!

error C2451: conditional expression of type 'ExtPersonType' is illegal No user-defined-conversion operator available that can perform this conversion, >or the operator cannot be called documents\visual studio 2010\projects\programming4\binarysearchtree.h(234): >while compiling class template member function 'void >BinarySearchTree::displayInfo(T)' with [ T=ExtPersonType ]

最佳答案

在你的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 的方式,以这种方式进行比较可以减少以后出错的机会。对象相互关联。

关于c++ - 在二叉搜索树中检索对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931650/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com