如何找到二叉树的叶结点
How to find the leaf of a binary tree?
我正在编写一个具有搜索功能的二叉树。该函数应该接受一个参数x,该参数表示要搜索的值,一旦找到它,确定它是否是叶子。如果没有找到该值,则返回false。
这是我所拥有的,它给了我一个segfault,并且刚刚给了我从1到100的每个值的错误返回。二叉树初始化为100个值
bool search(Node<T>* ¤tNode, const T& x) const
{
//~ cout << "CURRENT NODE DATA: " << currentNode->data << " : ";
/* FUNCTION: Searches for variable that is passed in X and checks if this value is a leaf or not */
//Left Subtree Search
if (x < currentNode->data)
{
if ((leaf(currentNode)) == true)
{
return true;
}
else
{
search(currentNode->left, x);
}
}
//Right Subtree Search
else if (x >= currentNode->data)
{
//If node in right subtree is a node check
if ((leaf(currentNode)) == true)
{
return true;
}
else
{
search(currentNode->right, x);
}
}
//Return false if the node is not a leaf
return false;
} //END OF SEARCH FUNCTION
void remove(Node<T>* ¤tNode, const T& x)
{
}
bool leaf(Node<T>* currentNode) const
{
if (currentNode != nullptr)
{
return ((currentNode->left == nullptr && currentNode->right == nullptr) ? true : false);
}
else
{
return true;
}
}
另外,当currentNode
为nullptr
时,leaf
函数返回true,假设search
中nullptr
的检查已解决。这是你想要的指向不存在的叶子的指针的行为吗?
在您的搜索函数中,您求助于子节点而不首先查看它们是否为nullptr…然后尝试取消引用数据元素,但仍然不检查是否为null。
相关文章:
- OpenCV 傅里叶变换复杂输出问题
- 离散傅立叶变换C++
- 如何从2D数组为QHeightMapSurfaceDataProxy创建高度图以显示2D傅立叶变换结果
- 有没有办法访问C++ Boost rtree 中的非叶节点
- 函数已知时的傅里叶变换
- 输出二叉搜索树中的叶节点数
- DFT(离散傅里叶变换)与C++代码
- 具有最大不同节点的根到叶路径
- 贝叶斯和SVM分类器在OpenCV3中的实现
- 为什么我的Cooley-Tukey和蛮力(傅立叶)算法给出的结果非常不同
- 快速傅立叶变换:使用模板化派生类'undefined reference'即使标头和.cpp文件匹配也会出错
- 快速傅里叶变换:第一个元素是正确的,但其余元素不正确
- 应用傅里叶逆变换OpenCV后构建图像
- 是否有任何函数可以从 wxtreelistctrl 获取所有选定的叶节点
- 从C++中的二进制搜索树(递归)复制叶
- 傅里叶变换高斯滤波器误差
- 如何在列奥纳多堆中找到根的左右子结点
- 结点(b样条)到底是什么
- 列出并计算二叉树从根结点到叶结点的权重最大的路径
- 如何找到二叉树的叶结点