Почему я не могу использовать «баланс» в моем коде? - PullRequest
0 голосов
/ 02 мая 2020

Я не знаю, что не так, система показывает:

Использование необъявленного идентификатора 'balance'.

Но это должно сработать, потому что "l" и "р" может работать. ??? Почему то ??? Я использовал правильный способ использовать баланс, но он все еще не может использовать? Кстати, это дерево AVL, и «информация об университете» является частью запроса, но это не важная часть.


struct uninfo ; // university information
struct uninfo {
    string all;
    string schoolName;
    string subjectName;
    string day;
    string grade;
    int nStudent;
    int nTeacher;
    int nGraduate;
} ;

struct tree
{
   int key ;
   vector<uninfo> list ;
   tree *left ;
   tree *right ;
   //tree *root ;
   int height ; // tree height
}; // related to the data

class data {

    int total ;
    int size ;
    string fileNum ;
    ifstream rFile ;
    ofstream wFile ;

    public:

        int getheight( tree *N )
        {
            if ( N == NULL)
                return 0;
            return N -> height ;
        }
        //data(){ root = NULL ; }
        //tree avltree ;
        tree *root ;
        vector<uninfo> InputList ;
        bool ReadFile( ) ;
        void clear() ;
        void list() ;
        bool SaveData() ;


}; // for data sorting


tree* r( tree *parent ) {  // this is the right spin

    tree *z = new tree() ; // create a new node
    tree *x = new tree() ; // create a new node
    tree tmp ;

    z  = parent -> left ; // set z as the parent's left child

    x = z -> right ; // set x as z's right child

    parent -> left = x ; // set the parent's left child as x

    z -> right = parent ; // set z's right child as the parent

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

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

    return z ; // return z
} // end of right right spin

tree* l( tree *parent ) { // this is the leftspin

    parent = new tree() ;
    tree *z = new tree() ; // create a new node
    tree *x = new tree() ; // create a new node

    z = parent -> right ; // set the z as the current parent's right child

    x = z -> left ; // set x as z's left child

    parent -> right = z ; // set parent's right child as z

    z -> left = parent; // set z's left child as the parent

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

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

    return z ; // return z
}// end of the left spin

tree *insert( tree* v, int y )
{
    data tmp ;
    // data tmp ; in order to access the struct in class, i have to create a new data

    if ( tmp.root == nullptr ) // if root == null
    {
        return ( createnode( v -> list.at( y ).nGraduate ) ); // return a int value
    } // if()

    if ( v -> list.at( y ).nGraduate > ( tmp.root -> list.at( y ).nGraduate ) ) // if the num is bigger then the num in root
    {
        tmp.root -> right = insert( tmp.root -> right, v -> list.at( y ).nGraduate ) ; //set the roo's right child as ( put into function createavltree again ) value



        tmp = balance( v , y ) ;
        // this is the code that went wrong
 //error issue : Use of undeclared identifier 'balance'



    } // if()

    else if ( v -> list.at( y ).nGraduate == ( tmp.root -> list.at( y ).nGraduate ) ) // if the num == num in root
    {
        return tmp.root ; // return root
    } // else if(), this condition is not allowed

    else
    {
        tmp.root -> left = insert( tmp.root -> left, tmp.InputList.at( y ).nGraduate ) ; // set root's left child as ( put into funtion createavltree again ) value
    } // else

    tmp.root -> height = 1 + max( tmp.getheight( tmp.root -> left ), tmp.getheight( tmp.root -> right ) ) ; // update the height

    return  v ;

} // end of insert

tree *balance( tree *tmp, int y )
{
    int f = 0 ;
    data t ;

    f = getheight( t.root -> left ) - getheight( t.root -> right ) ; 
    if ( f > 1 ) // if the factor > 1
    {
        if ( ( t.root -> left -> list.at( y ).nGraduate ) > t.InputList.at( y ).nGraduate ) // if the data currently put in is smaller than the current node's left child
        {
            return r( t.root ) ; // do the ll spin
        } // if(), this is the ll spin

        else if ( t.InputList.at( y ).nGraduate > ( t.root -> left -> list.at( y ).nGraduate ) ) // if the data currently put in is bigger than the current node's left child

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

    } // if()

    else if ( -1 > f ) // if -1 > the facor
    {
        if ( t.InputList.at( y ).nGraduate > ( t.root -> right ->  list.at( y ).nGraduate ) ) // if the data currently put in is bigger than the current node's right child
        {
            return r( t.root ); // do the rr spin
        } // if(), this is thr rr spin

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

    } // else if()

    return tmp ;
} // end of balance

1 Ответ

1 голос
/ 02 мая 2020

Вы должны объявить что-либо, прежде чем использовать это, это базовое c правило C ++. Вы используете balance перед тем, как объявить его, так что это не разрешено.

Чтобы объявить его, добавьте прототип функции

tree *balance( tree *tmp, int y );

Поместите это после объявления tree но до первого использования balance. После объявления data, но перед функцией r может показаться хорошим местом.

l и r в порядке, поскольку вы определяете эти функции перед их использованием, определение считается как декларация Но было бы неплохо добавить прототипы для l и r.

...