gpt4 book ai didi

c++ - 从二叉搜索树 C++ 返回引用

转载 作者:行者123 更新时间:2023-11-28 00:15:20 25 4
gpt4 key购买 nike

<分区>

考虑我的二叉搜索树中的以下搜索函数。

template <class elemType>
elemType& BSTree<elemType>::search(const elemType & searchItem) const
{
std::cout << "in 1st teir search" << std::endl;
if (root == NULL)
{
std::cout << "Tree is empty, and there for no data will be in this tree." << std::endl;
}
else
{
std::cout << "Entering 2nd teir search" << std::endl;
return search(root, searchItem);
} //End else
} //End search(1param)

template <class elemType>
elemType& BSTree<elemType>::search(nodeType<elemType>* node, const elemType& dataToFind) const
{
elemType found;

if (node == NULL)
{
std::cout << "Not found. Node is null." << std::endl;
}
else
{
if (node->data == dataToFind)
{
std::cout << "Data found" << std::endl;
found = node->data;
}
else if (node->data < dataToFind)
{
std::cout << "Data not found, searching to the RIGHT" << std::endl;
found = search(node->rLink, dataToFind);
}
else
{
std::cout << "Data not found, searching to the LEFT" << std::endl;
found = search(node->lLink, dataToFind);
}
} //End else
return found;
} //End search(2param)

每当我访问/搜索非根数据时,我的程序在分配该数据时崩溃。

我错过了什么?

注意:请理解,也许我可以在我的遍历中使用一个函数指针来返回值,但出于我使用我的树进行搜索的目的,将返回对该对象的引用。

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