问题描述
我已经实现了几个函数来旋转展开树中的节点,但是它通过将相同的节点发送到不同的地方以某种方式破坏了我的树。他们将儿子旋转到爸爸节点。爸爸指针可能过多,但我想尝试这种方式。我在函数中使用这些将节点调整为根。
struct cliente *rot_left(struct cliente *a){
if(a==root){
return a;
}
struct cliente *tmp=NULL,*tmp1=NULL;
tmp = a->dad;
tmp1 = tmp->dad;
a->dad= tmp->dad; //removes dad,point to grandpa
tmp->dad = a; //tmp point a
tmp->right = a->left; //a left to tmp right
a->left = tmp; //a picks son tmp
if(tmp1!=NULL){
if(tmp1->right==tmp)
tmp1->right=a;
else
tmp1->left=a;}
return a;}
struct cliente *rot_right(struct cliente *a){
if(a==root){
return a;
}
struct cliente *tmp=NULL,*tmp1=NULL;
tmp = a->dad;
tmp1 = tmp->dad;
a->dad = tmp->dad;
tmp->dad= a;
tmp->left = a->right;
a->right= tmp;
if(tmp1!=NULL){
if(tmp1->right==tmp)
tmp1->right=a;
else
tmp1->left=a;}
return a;
}
void adjust(struct cliente *a){
while(a->dad !=NULL) {
if(a==(a->dad->right)){
a=rot_left(a);
} else
a=rot_right(a);
}
root=a;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)