- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我只想说我对这一切都不陌生,所以请多多包涵。因此,对于我们的任务,我们必须实现我们自己的二叉搜索树。我们将使用此 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/
过去两天我遇到了一个奇怪的问题,但我还无法解决它。我正在尝试从 2 个文本文件中获取单词并将这些单词添加到树中。我选择的获取单词的方法引用这里: Splitting a text file into
例如,像这样的树: 5 / \ 3 6 / \ 7 2 print(tree.branchLenSum()) 将是1+1+2+2=6 树类: class BinaryTre
我正在学习 JavaScript。我是文档对象模型 (DOM) 主题的新手。 我对节点在 DOM 树中的布局方式感到困惑? 我使用了这个 HTML 文件。 List Buy groceries
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
var nodeEnter = node.enter().append("g") .attr("class", "node") .attr("transform", function (d) {
我正在使用数据表来表示我的应用程序中的数据。我有一个功能,可以在单击克隆图标时克隆任何行并动态添加它下面的行。 数据表具有搜索功能,可以搜索表中存在的所有行,但是它只考虑初始页面加载时加载到表中的行。
我已经用 Python 编写了一个 Tree 类,但是我在为它创建迭代器时遇到了问题我希望能够做到 phonebook = MyTree() # Build up tree for node in p
我需要为 FreeBSD 检查这个 libunwind 端口, http://people.freebsd.org/~kib/git/libunwind.git/ 我以前和当我尝试使用命令 check
我已经实现了自己的 AVL 树,并将其用作字典。我想知道,计算以某个字符串开头的所有单词的最快方法是什么。 例如: string prefix = "fa"; output: 4 我已经在 O(n)
我目前正在尝试编写一个使用 2-3-4 树的程序,但我遇到了插入函数的问题。这是相关代码.. int main () { tree234 myTree; myTree.insert("
我的同事问了我这个问题,但我无法想出任何最佳解决方案。 给定一棵具有n 个节点、n-1 条边和 q 个查询的无向加权树。 每个查询都有输入 u v k ,输出位于路径 u to v 的奇数且小于 k
如果我有一个 app.html 模板如下: ${message} test 使用 MyComponent.ts : export class MyComponent { my
我继承了一个 git 存储库,其中树中的提交条目为空 sha1,阻止 FishEye 为存储库编制索引。 $ git fsck Checking object directoriies: 100%(2
这是一个例子。我也许可以在点击函数中弄清楚它,但我不确定从点击函数外部从根开始检查所有子项的最佳方法。谢谢 .node circle { fill: #fff; stroke: stee
我正在使用 Python 批量编辑许多当前看起来像这样的 musicXML 文件: ... -5 -9
假设有一个 8 阶 B 树。这意味着它可以有 8 个指针和 7 个元素。假设字母 A 到 G 存储在这棵 B 树中。所以这棵 B 树只是一个包含 7 个元素的节点。 然后你尝试将 J 插入到树中。没有
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试做一个简单的二维“物理”模拟,主要涉及圆形物体的碰撞,为了避免编程我自己的空间索引(四叉树/r-tree/等),我希望使用 Boost 的 R -树。 问题是我在 Boost 文档中找不到任
我正在从 python 字典中读取数据并尝试在下面的树中添加更多书籍元素。下面只是一个示例,我需要复制一个元素及其子元素但替换内容,在这种情况下我需要复制 book 元素但替换标题和作者。
假设我有一个变量,它包含一个 nltk 树类的树。是否有类似 parent() 或返回节点父节点的函数? 最佳答案 您需要一种不同的数据结构:一棵树,其节点包含指向其父节点的指针。 NLTK 现在提供
我是一名优秀的程序员,十分优秀!