C++中的递归二叉搜索

Recursive Binary Search in C++

本文关键字:搜索 递归 C++      更新时间:2023-10-16

我正在尝试实现递归二叉搜索。但是,我的函数总是迭代 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,为什么会这样?我在二进制搜索中哪里犯了错误?

你的代码有两个问题:

  1. binarySearch()方法中,您比较的是,而不是。这可能是你想要的,但你的列表中没有2存储的价值(见第 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;
    }
}

我认为您可以从这里开始适当地修改您的代码。