как использовать правильную вставку дерева AVL - PullRequest
0 голосов
/ 04 мая 2020

Это часть вставки дерева AVL, я тестирую код и обнаруживаю, что он застрял в первой строке, то есть if (v == nullptr), но я не знаю почему. Если я предоставлю моему проекту данные с 500 числами в нем, все это покажет ab c de, а не все остальное, что должно выйти из строя. Я положил всю часть вставки ниже. Часть хранения данных в порядке, когда я выхожу из сохраненных данных, данные показываются совершенно идеально.


tree* insert( tree* v, int y )
{
    if ( v == nullptr ) 
    {
        cout << " a " << endl ;
        v = new tree() ;
        cout << " b " << endl ;
        v -> key = y ;
        cout<< " c " << endl ;
        v -> left = nullptr ;
        cout << " d " << endl ;
        v -> right = nullptr ;
        cout << " e " << endl ;
        return( v ) ; // return a int value
    } // if()

    cout << "   2    " << endl ;

    if (  y > ( v -> key ) ) 
    {
        v -> right = insert( v -> right, y ) ; 
    } // if()

    else if ( ( v -> key ) == y ) 
    {
        cout << " 3 " << endl ;
        return v ; // return v
    } // else if(), this condition is not allowed

    else
    {
        cout << " 4 " << endl ;
        v -> left = insert( v -> left, y ) ; 
    } // else

    cout << " 5 " << endl ;

    v -> height = 1 + max( height( v -> left ), height( v -> right ) ) ; // update the height

    int f = 0 ;

    f = thebalance( v ) ;

     if ( f > 1 ) // if the factor > 1
     {
         if ( ( v -> left -> key ) > y ) // if the data currently put in is smaller than the current node's left child
         {
             return r( v ) ; // do the rr spin
         } // if(), this is the ll spin

         else if ( y > ( v -> left -> key ) ) // if the data currently put in is bigger than the current node's left child

         {
             v -> left = l( v -> left ) ; // do a left spin first
             return r( v ) ; // then do a right spin
         } // else if(), this is the lr spin

     } // if()

     else if ( -1 > f ) // if -1 > the facor
     {
         if ( y > ( v -> right ->  key ) ) // if the data currently put in is bigger than the current node's right child
         {
             return l( v ); // do the ll spin
         } // if(), this is thr rr spin

         else if ( ( v -> right -> key ) > y ) // if the data currently put in is smaller than the current node's right child
         {
             v -> right = r( v -> right ) ; // do the right spin first
             return l( v ) ; // then do a left spin
         } // else if(), this is the rl spin

     } // else if()

     cout <<  "    end    "  << endl ;



     return v ;
} // end of insert
...