二叉搜索数组溢出 C++

binary search array overflow c++

本文关键字:溢出 C++ 数组 搜索      更新时间:2023-10-16

我是计算机科学专业的学生。这是我为数据结构和算法类完成的一些代码。它编译良好,运行正常,但其中有一个错误,我用创可贴纠正了。我希望得到一个关于如何以正确的方式修复它的答案,以便将来我知道如何正确地做到这一点。

作业的目的是创建一个二叉搜索。我采用了我创建的使用堆排序的程序,并添加了二进制搜索。我使用Visual Studio作为我的编译器。

我的问题是我选择将我的值从文本文件读入数组。文本文件中的每个整数都由制表符空格分隔。在第 98 行中,文件正确读取,但是当我到达文件中的最后一个项目时,计数器 (n) 计数太多一次,并为数组中的该索引分配一个大的负数(因为数组溢出),然后导致我的堆排序以我不需要的非常大的负数开头。我通过分配数组中的最后一个位置来为其添加创可贴,将数组中的第一个位置分配给数组中的第一个位置。我将读出的数字与我的文件进行了比较,每个数字都在那里,但是大数字消失了,所以我知道它有效。这对我来说不是一个合适的修复程序,即使程序确实运行正常。我想知道是否有人知道一个正确的解决方案,它会遍历我的文件,将每个整数分配给数组中的一个位置,但不会溢出数组。

这是整个程序:

#include "stdafx.h"
#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
#define MAXSIZE 100
void heapify(int heapList[], int i, int n)  //i shows the index of array and n is the counter
{
int listSize;
listSize=n;
int j, temp;//j is a temporary index for array
temp = heapList[i];//temporary storage for an element of the array
j = 2 * i;//end of list
while (j <= listSize)
{
if (j < listSize && heapList[j + 1] > heapList[j])//if the value in the next spot is greater than the value in the current spot
j = j + 1;//moves value if greater than value beneath it
if (temp > heapList[j])//if the value in i in greater than the value in j
break;
else if (temp <= heapList[j])//if the value in i is less than the value in j
{
heapList[j / 2] = heapList[j];//assigns the value in j/2 to the current value in j--creates parent node
j = 2 * j;//recreates end of list
}
}
heapList[j / 2] = temp;//assigns to value in j/2 to i
return;
}
//This method is simply to iterate through the list of elements to heapify each one
void buildHeap(int heapList[], int n) {//n is the counter--total list size
int listSize;
listSize = n;
for (int i = listSize / 2; i >= 1; i--)//for loop to create heap
{
heapify(heapList, i, n);
}
}
//This sort function will take the values that have been made into a heap and arrange them in order so that they are least to greatest
void sort(int heapList[], int n)//heapsort
{
buildHeap(heapList, n);
for (int i = n; i >= 2; i--)//for loop to sort heap--i is >= 2 because the last two nodes will not have anything less than them
{
int temp = heapList[i];
heapList[i] = heapList[1];
heapList[1] = temp;
heapify(heapList, 1, i - 1);
}
}
//Binary search
void binarySearch(int heapList[], int first, int last) {//first=the beginning of the list, last=end of the list
int mid = first + last / 2;//to find middle for search
int searchKey;//number to search
cout << "Enter a number to search for: ";
cin >> searchKey;
while ((heapList[mid] != searchKey) && (first <= last)) {//while we still have a list to search through
if (searchKey < heapList[mid]) {
last = mid - 1;//shorten list by half
}
else {
first = mid + 1;//shorten list by half
}
mid = (first + last) / 2;//find new middle
}
if (first <= last) {//found number
cout << "Your number is " << mid << "th in line."<< endl;
}
else {//no number in list
cout << "Could not find the number.";
}
}
int main()
{
int j = 0;
int n = 0;//counter
int first = 0;
int key;//to prevent the program from closing
int heapList[MAXSIZE];//initialized heapList to the maximum size, currently 100
ifstream fin;
fin.open("Heapsort.txt");//in the same directory as the program
while (fin >> heapList[n]) {//read in
n++;
}
heapList[n] = heapList[0];
int last = n;
sort(heapList, n);
cout << "Sorted heapList" << endl;
for (int i = 1; i <= n; i++)//for loop for printing sorted heap
{
cout << heapList[i] << endl;
}
binarySearch(heapList, first, last);
cout << "Press Ctrl-N to exit." << endl;
cin >> key;
}
int heapList[MAXSIZE];//initialized heapList to the maximum size, currently 100

这个注释是错误的 -heapList数组被声明为未初始化,因此当您从文件中读取所有数据时,索引变量n将指向未初始化的单元格。任何使用它的尝试都将调用未定义的行为。你可以:在使用数组之前对其进行初始化,递减n值,因为它大于读取值的逐个,或者更好地使用std::vector而不是数组。

您填充索引的heapsort值,0n-1

然后,您可以访问从1nheaplist,这是越界的,因为没有在heapsort[n]中输入任何值。

for (int i = 0; i < n; i++) //instead of i=1 to n