C++中的递归二叉搜索
Recursive Binary Search in C++
我正在尝试实现递归二叉搜索。但是,我的函数总是迭代 3 次,然后返回我正在搜索的列表为空。相关代码如下。full() 和 insertAfter() 都可以正常工作。
列表.cpp
#include <iostream>
#include "listA.h"
using namespace std;
List::List(int a)
{
size = a;
actual = 0;
data = new char[a];
cursor = -1;
}
int List::binarySearch(List l, int value, int imin, int imax)
{
//check if list is empty
if(imax < imin)
{
//return -1 if list is empty
return -1;
}
cout << "made it here" << endl;
//calculate midpoint of set
// problem line: int imid = imin + ((imin+imax)/2);
int imid = (imin+imax)/2;
//search for value
if(l.data[imid] > value)
{
cout << "less than"<< endl;
//value is in lower half
return binarySearch(l,value,imin,imid-1);
}
if(l.data[imid] < value)
{
cout <<" greater " << endl;
//value is in upper half
return binarySearch(l,value,imid+1,imax);
}
else
{
//value has been found
return imid;
}
}
bool List::insertAfter(char c)
{
if(!full())
{
if(actual >= cursor)
{
//shuffle values up so as to not overwrite
for(int i=cursor+1;i<=actual;i++)
{
data[i]+data[i+1];
}
}
//increment actual element count
actual++;
//add c to data after cursor
data[cursor+1]=c;
//increment cursor to new element
cursor++;
return true;
}
else
{
return false;
}
}
bool List::full()const
{
return (actual == size);
}
这是我的清单A.h
class List{
public:
List(int = 10);
int binarySearch(List l, int, int, int);
bool insertAfter(char);
bool full() const;
private:
int size;
int actual;
int cursor;
char *data;
};
我的主要.cpp是
int main() {
int size = 7;
char c;
List l(size);
for(int i = 0; i < size; i++) {
if(!l.insertAfter(i + '0')) {
cout << "inserting " << (char)(i + '0');
cout << "Error in insertAfter" << endl;
}
}
cout << "TEST FOR BINARYSEARCH" << endl;
//search for 2 in the list from 0->6
cout << l.binarySearch(l,2,0,6) << endl;
}
我的输出返回开始<<:0123456结束<<测试二进制搜索在这里做到了小于在这里做到了小于在这里做到了小于-1//
所以我循环了三次,然后返回-1,为什么会这样?我在二进制搜索中哪里犯了错误?
你的代码有两个问题:
- 在
binarySearch()
方法中,您比较的是值,而不是键。这可能是你想要的,但你的列表中没有2
存储的价值(见第 2 点)。 - 您
insertAfter()
方法插入char
打印时给出0,1,2,3,4,5,6
但实际上它们的int
值(基于进行比较)如下:48,49,50,51,52,53,54
.
我不确定这是否是您在这里尝试执行的操作,但这是binarySearch()
方法的工作版本(请注意int
转换char
):
int List::binarySearch(List l, int value, int imin, int imax)
{
//check if list is empty
if(imax < imin)
{
//return -1 if list is empty
return -1;
}
cout << "made it here" << endl;
//calculate midpoint of set
int imid = ((imin+imax)/2);
//search for value
if((int)(l.data[imid]-'0') > value) // <-- see the cast here
{
cout << "less than"<< endl;
//value is in lower half
return binarySearch(l,value,imin,imid-1);
}
if((int)(l.data[imid]-'0') < value) // <-- see the cast here
{
cout <<" greater " << endl;
//value is in upper half
return binarySearch(l,value,imid+1,imax);
}
if((int)(l.data[imid]-'0') == value) // <-- see the cast here (this part of your program could be simplified by removing this condition and leaving only the return)
{
//value has been found
return l.data[imid]; // <-- see the modified return here
}
}
这是创建列表的代码:
for(int i = 0; i < size; i++) {
cout << "inserting " << (int)(i + '0');
if(!l.insertAfter(i + '0')) { // <-- note the conversion from int to char
cout << "Error in insertAfter" << endl;
}
}
我认为您可以从这里开始适当地修改您的代码。
相关文章:
- 按边长度递归搜索图中所有可行路径
- 在递归二叉搜索树中搜索
- 递归二进制搜索与字符串数组
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 如何改进搜索二项式系数的递归算法
- 使用后序遍历递归的深度优先搜索会产生意外输出
- 字符串的递归二进制搜索-C++
- 递归搜索n元树中的元素
- 这是河内算法的递归塔是一种不知情的搜索
- 二叉搜索树按级别递归C++打印节点
- 了解一种神秘地起作用的递归二进制搜索算法
- 在 c++ 中递归搜索注册表
- 我在DFS搜索(树)代码中遇到麻烦.我如何打破递归
- 简单的二进制搜索树非递归添加功能
- 双链接列表通过递归搜索C
- 递归获取二进制搜索树的高度
- 使用 Bool 函数在 C++ 中进行递归二叉搜索
- VPATH 中的递归搜索
- 为什么这个计算二进制搜索树中节点的递归函数总是返回比预期更大的结果
- 使用递归在二进制搜索树中查找固定后继