मैं इसे recursively..The माता पिता पूर्णांक varaible होगा मैं की तरह है करने के लिए, सूत्र के अनुरूप, की कोशिश की है 2*i +1के लिए leftChildकी और 2*i +2सही के लिए।
void BST::insert(const data &aData)
{
if ( items[Parent].empty )
{
items[Parent].theData = aData;
items[Parent].empty = false;
}
else if ( aData < items[Parent].theData )
{
Parent = 2 * Parent + 1;
if ( Parent >= maxSize ) this->reallocate();
this->insert(aData);
}
else
{
Parent = (2 * rightChild++)+ 2;
if ( Parent >= maxSize ) this->reallocate();
this->insert(aData);
}
}
यह ठीक काम करता है जब आइटम है कि मूल माता पिता की तुलना में कम कर रहे हैं डालने ... लेकिन जब मैं कुछ पाते हैं कि इसे बड़े सभी शिकंजा है: एक्स
void BST::reallocate()
{
item *new_array = new item[maxSize*2];
for ( int array_index = 0; array_index < maxSize; array_index++ )
{
if ( ! items[array_index].empty )
{
new_array[array_index].theData = items[array_index].theData;
}
}
maxSize *= 2;
delete [] items;
items = NULL;
items = new_array;
}
यहाँ मेरी ctor है ताकि कोई भी अब और भ्रमित हो जाता है तो मैं हूँ:
BST::BST(int capacity) : items(new item[capacity]), size(0), Parent(0),
leftChild(0), rightChild(0)
{
items->empty = true;
maxSize = capacity;
}
private:
int size; // size of the ever growing/expanding tree :)
int Parent;
int maxSize;
int leftChild;
int rightChild;
struct item
{
bool empty;
data theData;
};
item *items; // The tree array
ऊपर प्रविष्टि समारोह वास्तव में सबसे अच्छा मैं प्राप्त कर सकते हैं ..
R
/ \
/ \
/ \
L X
/ \ / \
J V K T <--The only out of place node.
/ \ \
/ NULL \
G /
P
जब डालने: R, L, J, G, X, K, V, P, Tइसी क्रम में













