为什么我不能在我的代码中使用“balance”?
Why can't I use the ' balance ' in my code?
不知道怎么回事,系统显示:
Use of undeclared identifier 'balance'.
但它应该可以工作,因为“l”和“r”可以工作。 ???为什么???我用了正确的balance方法还是不能用?
顺便说一句,这是一棵 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++的基本规则。你在声明它之前使用 balance
,所以这是不允许的。
要声明它,请添加一个函数原型
tree *balance( tree *tmp, int y );
将其放在 tree
的声明之后但在首次使用 balance
之前。在 data
声明之后但在函数之前 r
似乎是个好地方。
l
和 r
可以,因为您在使用它们之前定义了这些函数,定义算作声明。但是也可以为 l
和 r
添加原型。
不知道怎么回事,系统显示:
Use of undeclared identifier 'balance'.
但它应该可以工作,因为“l”和“r”可以工作。 ???为什么???我用了正确的balance方法还是不能用? 顺便说一句,这是一棵 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++的基本规则。你在声明它之前使用 balance
,所以这是不允许的。
要声明它,请添加一个函数原型
tree *balance( tree *tmp, int y );
将其放在 tree
的声明之后但在首次使用 balance
之前。在 data
声明之后但在函数之前 r
似乎是个好地方。
l
和 r
可以,因为您在使用它们之前定义了这些函数,定义算作声明。但是也可以为 l
和 r
添加原型。