Hello,
Im trying to build an array based, "Binary Search Tree" by following the algorithm at:
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;
}