简体   繁体   中英

Binary Search Tree In Basic Array Format

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;
}

There are quite a few problems with your code.

  1. You are creating an unbalanced BST, potentially requiring 2^N space to store the tree. Ie to "sort" 32 elements you might need 4GB of memory. Your 50 element array can only reliably sort 4 integers.
  2. You are relying on the integer 0 to indicate "no value". 0 can't be a part of the initial data.
  3. int bst[50] is not initialized. The contents are unknown, not zero. int bst[50] = {}; , see eg http://www.cplusplus.com/doc/tutorial/arrays/
  4. Your method can't handle two integers with the same value.
  5. I'm under the impression you expect the last for -statment to output the integers sorted. This is also not the case.

In short: This approach won't work. Creating a BST is tricky. I would suggest you study an already existing balanced BST implementation.

To solve 5. you can use something like:

void visitAndPrintOddBST(int bst[], int index = 0){
    if (bst[index] == 0 ) return;
    visitAndPrintOddBST(bst, index*2 + 1);
    std::cout << bst[index] << std::endl;
    visitAndPrintOddBST(bst, index*2 + 2);
}

You need some sort of stack to keep track of the movement in the tree. In this approach this is the recursive call stack. Also the size of the array is unspecified and is quite likely to go out of bounds. This is not good code. Only use it for this experiment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM