C++ 为什么可以';t我使用';余额';在我的代码里?
我不知道出了什么问题,系统显示: 使用未声明的标识符“余额” 但它应该会起作用,因为“l”和“r”可以起作用???为什么呢???我用了正确的方法使用平衡,但它仍然不能使用? 顺便说一句,这是一个AVL树,“大学信息”是请求的一部分,但不是重要部分C++ 为什么可以';t我使用';余额';在我的代码里?,c++,avl-tree,C++,Avl Tree,我不知道出了什么问题,系统显示: 使用未声明的标识符“余额” 但它应该会起作用,因为“l”和“r”可以起作用???为什么呢???我用了正确的方法使用平衡,但它仍然不能使用? 顺便说一句,这是一个AVL树,“大学信息”是请求的一部分,但不是重要部分 struct uninfo ; // university information struct uninfo { string all; string schoolName; string subjectName;
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
结构uninfo;//大学信息
结构uninfo{
串全部;
字符串学名;
字符串subjectName;
弦日;
串级;
智力型学生;
国际教师;
内倾的;
} ;
结构树
{
int键;
向量表;
树*左;
树*对;
//树根;
int height;//树高
}; // 与数据相关
类数据{
整数合计;
整数大小;
字符串fileNum;
ifrfile;
流文件;
公众:
int getheight(树*N)
{
如果(N==NULL)
返回0;
返回N->高度;
}
//data(){root=NULL;}
//树木;
树根;
向量输入表;
bool ReadFile();
无效清除();
作废清单();
bool SaveData();
}; // 用于数据排序
tree*r(tree*parent){//这是正确的旋转
tree*z=new tree();//创建一个新节点
tree*x=new tree();//创建一个新节点
树tmp;
z=parent->left;//将z设置为父级的左子级
x=z->right;//将x设置为z的右子级
父->左=x;//将父对象的左子对象设置为x
z->right=parent;//将z的右子级设置为父级
父级->高度=最大值(getheight(父级->左),getheight(父级->右))+1;//更新高度
z->height=max(getheight(z->left),getheight(z->right))+1;//更新高度
返回z;//返回z
}//右旋转结束
tree*l(tree*parent){//这是leftspin
父=新树();
tree*z=new tree();//创建一个新节点
tree*x=new tree();//创建一个新节点
z=parent->right;//将z设置为当前父级的右子级
x=z->left;//将x设置为z的左子级
父->右=z;//将父对象的右子对象设置为z
z->left=parent;//将z的左子级设置为父级
z->height=max(getheight(z->left),getheight(z->right))+1;//更新高度
父级->高度=最大值(getheight(父级->左),getheight(父级->右))+1;//更新高度
返回z;//返回z
}//左旋结束
树*插入(树*v,整数y)
{
数据tmp;
//数据tmp;为了访问类中的结构,我必须创建一个新的数据
if(tmp.root==null ptr)//if root==null
{
return(createnode(v->list.at(y).ngradude));//返回一个int值
}//如果()
if(v->list.at(y).ngradude>(tmp.root->list.at(y).ngradude))//如果num大于root中的num
{
tmp.root->right=insert(tmp.root->right,v->list.at(y).ngradude);//将roo的右子级设置为(再次放入函数createavltree)值
tmp=平衡(v,y);
//这就是出错的代码
//错误问题:使用未声明的标识符“余额”
}//如果()
else if(v->list.at(y).ngradude==(tmp.root->list.at(y).ngradude))//如果num==根中的num
{
return tmp.root;//返回root
}//否则如果为(),则不允许此条件
其他的
{
tmp.root->left=insert(tmp.root->left,tmp.InputList.at(y).ngradude);//将root的左子级设置为(再次放入函数createavltree)值
}//否则
tmp.root->height=1+max(tmp.getheight(tmp.root->left),tmp.getheight(tmp.root->right));//更新高度
返回v;
}//插入结束
树*平衡(树*tmp,整数y)
{
int f=0;
数据t;
f=getheight(t.root->left)-getheight(t.root->right);
if(f>1)//如果因子>1
{
if((t.root->left->list.at(y).ngradude)>t.InputList.at(y).ngradude)//如果当前放入的数据小于当前节点的左子节点
{
返回r(t.root);//执行ll旋转
}//if(),这是ll自旋
else if(t.InputList.at(y).ngradude>(t.root->left->list.at(y).ngradude))//如果当前放入的数据大于当前节点的左子节点
{
t、 root->left=l(t.root->left);//先向左旋转
返回r(t.root);//然后向右旋转
}//否则如果为(),则这是lr自旋
}//如果()
else if(-1>f)//if-1>facor
{
if(t.InputList.at(y).ngradude>(t.root->right->list.at(y).ngradude))//如果当前放入的数据大于当前节点的右子节点
{
返回r(t.root);//执行rr旋转
}//if(),这是thr-rr自旋
else if((t.root->right->list.at(y).ngradude)>t.InputList.at(y).ngradude)//如果当前放入的数据小于当前节点的右子节点
{
t、 root->right=r(t.root->right);//先向右旋转
返回l(t.root);//然后向左旋转
}//否则如果(),这是rl自旋
}//else if()
返回tmp;
}//结余
> p>在使用之前,必须声明任何东西,这是C++的基本规则。您在声明余额之前使用余额
,因此这是不允许的
要声明它,请添加一个函数原型
将其放在树
声明之后,但在第一次使用余额
之前。在声明data
之后,但在函数r
之前,似乎是个好地方
l
和r
是可以的,因为您定义了这些乐趣
tree *balance( tree *tmp, int y );