I am trying to read in a bunch of integers into an array, then use a function that takes that array as an input element by element and creates a binary search tree. the bst is in the form of an array and will have smaller elements at the (2n+1) array spot and larger elements at the 2n+2 array spot. I think I have most of it working but for some reason, the loop in my function wont work. Any solutions?? I think I am simply just not seeing something.
Code::
#include <iostream>
#include <fstream>
using namespace std;
void sortArray(int arr[], int size)
{
int bst[50];
bst[0] = arr[0];
for(int b = 1; b < size - 1; b++)
{
int c = 0;
do{
if(arr[b] < bst[c])
{
c = ((2 * c) + 1);
}
if(arr[b] > bst[c])
{
c = ((2 * c) + 2);
}
}while(bst[c] != 0);
bst[c] = arr[b];
}
for(int p = 0; p < 25; p++){
cout << bst[p];
}
}
int main(int argc, char** argv) {
int myArray[50];
int i = 0;
ifstream myfile ("tree_nodes.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
myfile >> myArray[i];
i++;
}
myfile.close();
}
else cout << "Unable to open file";
for(int p = 0; p < (i); p++){
cout << myArray[p];
}
sortArray(myArray,i);
return 0;
}