简体   繁体   中英

Array Based Binary Search Tree C++

Im trying to build an array based, "Binary Search Tree" by following the algorithm at:

http://highered.mcgraw-hill.com/olcweb/cgi/pluginpop.cgi?it=gif%3A:600%3A:388%3A%3A/sites/dl/free/0070131511/25327/tree%5Finsert.gif%3A%3ATREE-INSERT

Up until I need to realloacte, my tree resembles:

     R
    / 
   A
    \
     F
      \
       L
      /
     B
      \
       C
        \
         T  

Recursively. However, im notice that I need to get back to the root, "R"....Trying to do that now..

void BST::insert(const data& aData)
{
item *y = NULL;   // Algorithm calls for NULL assignment..
item *x = new item();       

     // How do i Init LEFT and RIGHT?
     // With no nested copy ctor for struct item?
if ( items->empty ) 
{
    items = new item();
    items->empty = false;
    items->theData = aData; // Get the data.
    ++size;
} 
else if ( size == maxSize ) this->reallocate();
else  
{   
        if ( aData < items->theData )
        {
            x[x->LEFT].theData = aData;
            x->LEFT = x->LEFT + 1;
            this->insert(items->theData);
        }
        else if ( items->theData < aData )
        {
            x[x->RIGHT].theData = aData;
            x->RIGHT = x->RIGHT + 1;
            this->insert(items->theData);
        } 
                       else this->insert(items->theData);
}

Here is my struct for the items array in the private section of the BST class object file: ...

 private:
    int size;  // size of the ever growing/expanding tree :)
    int maxSize;
    struct item
    {
              bool empty;
         int  LEFT;
              int  RIGHT;
              data theData;     
         };

    item *items;    // The tree array
    item *oldRoot;
         int root_index;   // index for the root(s)

Hmm. Im also overloading the assignment operator...I dont know what to say. Its hard. I've looked at so many examples and lectures online; as well as algorithms....

The relloaction method as requested:

void BST::reallocate()
{
    item *new_array = new item[size*2];
    for ( int array_index = 0; array_index < size; array_index++ ) 
    {
        new_array[array_index].theData = items[array_index].theData;
        new_array[array_index].empty = false;
    }
    size *= 2;
    delete [] items;

    items = NULL;
    items = new_array;
}

There is well established way to implement binary tree as array, which says that root is sitting at index 0, LEFT of element at index i will be found at 2i + 1 and RIGHT will be found at 2i + 2. You do not need to have LEFT and RIGHT as part of your structure. it has to be

struct item
{
    int index;
    data theData;
};

Do NOT store left index and right index in your structure. Also you don't need to keep track of root index. It is always 0. Check out wiki article on binary trees. Search for "Methods for storing binary trees" string. This implementation allows easy traversal down and up the tree if needed.

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