1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
/*
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the subtree.
* lineNum is the line number occurrence to be recorded
*/
void insert( const Comparable & x, const int & lineNum, AvlNode * & t)
{
if( t == NULL )
{
if(t->parent == NULL) //insertion at the root, new tree
{
t = new AvlNode(x, NULL, NULL, NULL);
}
t = new AvlNode( x, NULL, NULL, ?); //set value, parent, lt and rt to NULL
t->lines.push_back(lineNum); //add line number occurrence
}
else if( x < t->element )
{
insert( x, lineNum, t->left);
if( height( t->left ) - height( t->right ) == 2 )
if( x < t->left->element )
{
rotateWithLeftChild( t );
}
else
{
doubleWithLeftChild( t );
}
}
else if( t->element < x )
{
insert( x, lineNum, t->right);
if( height( t->right ) - height( t->left ) == 2 )
if( t->right->element < x )
{
rotateWithRightChild( t );
}
else
{
doubleWithRightChild( t );
}
}
else
{
if(t->flag == false)
t->flag = true; //change flag to true if node was previously "deleted"
t->lines.push_back(lineNum); // Duplicate; add new line occurrence
}
t->height = max( height( t->left ), height( t->right ) ) + 1; //recalculate height
}
|