1

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;
}
2
  • Can you post your reallocation method as well ? Why don't you use a std::vector ? Commented Nov 15, 2009 at 8:07
  • I posted the reallocation method as u requested. Commented Nov 15, 2009 at 8:32

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a reason why item contains an index? If it is implemented using an array then we know exactly where it is... Or am I missing something?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.