C++ C++;二叉搜索树递归搜索函数 模板 布尔BST::搜索(常量T&x、int&len)常量 { 返回搜索(BT::root,x); } 模板 boolbst::search(结构节点*&根,常量T&x) { if(root==NULL) 返回false; 其他的 { 如果(根->数据==x) 返回true; else if(根->数据左,x); 其他的 搜索(根->右,x); } }
这是我用T节点搜索BST类的函数。x是在树中搜索的数据,len是它必须经过的节点数量,以找到匹配的节点(如果存在)。我还没有实现这一点,我只是在逐步发展我的作业。我这样称呼它:C++ C++;二叉搜索树递归搜索函数 模板 布尔BST::搜索(常量T&x、int&len)常量 { 返回搜索(BT::root,x); } 模板 boolbst::search(结构节点*&根,常量T&x) { if(root==NULL) 返回false; 其他的 { 如果(根->数据==x) 返回true; else if(根->数据左,x); 其他的 搜索(根->右,x); } },c++,binary-search-tree,C++,Binary Search Tree,这是我用T节点搜索BST类的函数。x是在树中搜索的数据,len是它必须经过的节点数量,以找到匹配的节点(如果存在)。我还没有实现这一点,我只是在逐步发展我的作业。我这样称呼它: template <class T> bool BST<T>::search(const T& x, int& len) const { return search(BT<T>::root, x); } template <class T> bo
template <class T>
bool BST<T>::search(const T& x, int& len) const
{
return search(BT<T>::root, x);
}
template <class T>
bool BST<T>::search(struct Node<T>*& root, const T& x)
{
if (root == NULL)
return false;
else
{
if (root->data == x)
return true;
else if(root->data < x)
search(root->left, x);
else
search(root->right, x);
}
}
if(t.search(v[1],len)==true)
cout好的,boolbst::search(struct Node*&root,const T&x)
后面应该有const,就像这样:boolbst::search(struct Node*&root,const T&x)const
。基本上,你从一个常量函数调用了一个非常量函数,这是一个禁忌
顺便说一句,这在我看来是可疑的“struct Node*&
”。。。我可能会放弃&并使用节点*
。。。但也许你需要它是因为结构
也是,这是C++,没有理由把节点作为一个结构来保留。在参数定义中需要struct看起来很糟糕,IMHO。为什么不将节点设为类?
算法:
获取节点值数据李>
重复步骤3到步骤5,直到找到值或超出树
如果数据等于根节点值,则搜索成功并终止算法
如果数据小于根节点值,则必须搜索左子树
如果数据小于根节点值,则必须搜索左子树
输出打印消息“已找到”或“未找到”
C++实现
node*搜索(node*根,int数据)
{
if(root==NULL | | root->data==data)返回root;
如果(根->数据<数据)返回搜索(根->右,数据);
返回搜索(根->左,数据);
}
您的搜索代码中存在多个问题:
- 排序顺序是向后的,如果节点数据小于搜索的数据,则应在右分支中搜索,而不是在左分支中搜索
- 您应该返回递归调用的结果
- 还不清楚为什么要通过引用传递
root
。它应该作为const
限定指针传递,方法体也应该是const
限定指针
这里有一个替代方案:
node* search(node* root, int data)
{
if (root==NULL || root->data==data) return root;
if (root->data < data) return search(root->right, data);
return search(root->left, data);
}
模板
boolbst::search(const struct Node*root,const T&x)const{
if(root==NULL)
返回false;
其他的
如果(根->数据==x)
返回true;
其他的
如果(根->数据右,x);
其他的
返回搜索(根->左,x);
}
下面是一个更简单的非递归实现:
template <class T>
bool BST<T>::search(const struct Node<T> *root, const T& x) const {
if (root == NULL)
return false;
else
if (root->data == x)
return true;
else
if (root->data < x)
return search(root->right, x);
else
return search(root->left, x);
}
模板
boolbst::search(const struct Node*root,const T&x)const{
while(root!=NULL){
如果(根->数据==x)
返回true;
如果(根->数据右;
其他的
根=根->左;
}
返回false;
}
我粘贴了完整的消息。但是它以前的格式不太好,所以我把它删减了,但我把它重新放回了。它是一个节点类,但出于某种原因,为了能够以这种方式访问它,我不得不在插入和搜索函数中将它变成一个结构。这是通过维基百科提供给我的,而我丢失了大量信息。什么是BT::root?BST和BT是一回事吗?节点和节点是一回事吗?请剪切并粘贴,不要键入。这是派生函数的一部分,因此对于我传递根节点,我必须明确地告诉它BT::root,否则我会出错。如果我不这样做,我会在这个范围内声明我的根。你能添加一些上下文吗?Tnq来查找我的错误。Marcus MüllerThank你的非递归版本。
node* search(node* root, int data)
{
if (root==NULL || root->data==data) return root;
if (root->data < data) return search(root->right, data);
return search(root->left, data);
}
template <class T>
bool BST<T>::search(const struct Node<T> *root, const T& x) const {
if (root == NULL)
return false;
else
if (root->data == x)
return true;
else
if (root->data < x)
return search(root->right, x);
else
return search(root->left, x);
}
template <class T>
bool BST<T>::search(const struct Node<T> *root, const T& x) const {
while (root != NULL) {
if (root->data == x)
return true;
if (root->data < x)
root = root->right;
else
root = root->left;
}
return false;
}