为什么我不能在代码中使用'balance'?

问题描述 投票:0回答:1

我不知道怎么了,系统显示:使用了未声明的标识符'balance'。但是它应该起作用,因为“ l”和“ r”可以起作用。 ???为什么你???我使用正确的方式使用余额,但仍然无法使用?顺便说一句,这是一棵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

c++ avl-tree
1个回答
1
投票

使用前必须声明任何东西,这是C ++的基本规则。在声明之前使用balance,因此不允许这样做。

要声明,请添加功能原型

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

[在声明tree之后但在首次使用balance之前放置。在data的声明之后但在r的函数之前似乎是个好地方。

lr可以,因为您在使用这些功能之前先定义了这些功能,因此定义被视为声明。但是也为lr添加原型也没有什么坏处。

© www.soinside.com 2019 - 2024. All rights reserved.