AVL树旋转的正确实现是什么?
当将 50,49,48 插入 AVL 树时,它会打印出来。
The root is: 50
50 Level: 0 Height: 0
49 Level: 1 Height: 0
50 Level: 0 Height: -1
50 Level: 0 Height: 0 -->> Rotation did not work?
这是我的功能。 向左旋转:
void AVLTree::rotateLeft(AVLNode* node)
{
AVLNode* otherNode = node;
otherNode = node->leftchild;
node->leftchild = otherNode->rightchild;
otherNode->rightchild = node;
node->height = max( height(node->leftchild), height(node->rightchild)) +1;
otherNode->height = max( height(otherNode->leftchild) , height(otherNode->rightchild))+1;
node = otherNode;
}
插入:
AVLTree::AVLNode* AVLTree::insert(int d,AVLNode *n){
if (n == NULL)
{
n = new AVLNode;
n->data = d;
n->leftchild = NULL;
n->rightchild = NULL;
n->height = 0;
} else if( d < n->data) {
n->leftchild = insert(d,n->leftchild);
if (height(n->leftchild) - height(n->rightchild) == 2) {
if (d < n->leftchild->data) {
rotateLeft(n);
} else {
rotateLeftTwice(n);
}
}
} else if (d > n->data) {
n->rightchild = insert(d,n->rightchild);
if (height(n->rightchild) - height(n->leftchild) == 2) {
if (d > n->rightchild->data) {
rotateRight(n);
} else {
rotateRightTwice(n);
}
}
} else {
;
}
n->height = max(height(n->leftchild), height(n->rightchild))+1;
return n;}
When inserting 50,49,48 into an AVL Tree, it prints out.
The root is: 50
50 Level: 0 Height: 0
49 Level: 1 Height: 0
50 Level: 0 Height: -1
50 Level: 0 Height: 0 -->> Rotation did not work?
Here are my functions.
Rotate Left:
void AVLTree::rotateLeft(AVLNode* node)
{
AVLNode* otherNode = node;
otherNode = node->leftchild;
node->leftchild = otherNode->rightchild;
otherNode->rightchild = node;
node->height = max( height(node->leftchild), height(node->rightchild)) +1;
otherNode->height = max( height(otherNode->leftchild) , height(otherNode->rightchild))+1;
node = otherNode;
}
insert:
AVLTree::AVLNode* AVLTree::insert(int d,AVLNode *n){
if (n == NULL)
{
n = new AVLNode;
n->data = d;
n->leftchild = NULL;
n->rightchild = NULL;
n->height = 0;
} else if( d < n->data) {
n->leftchild = insert(d,n->leftchild);
if (height(n->leftchild) - height(n->rightchild) == 2) {
if (d < n->leftchild->data) {
rotateLeft(n);
} else {
rotateLeftTwice(n);
}
}
} else if (d > n->data) {
n->rightchild = insert(d,n->rightchild);
if (height(n->rightchild) - height(n->leftchild) == 2) {
if (d > n->rightchild->data) {
rotateRight(n);
} else {
rotateRightTwice(n);
}
}
} else {
;
}
n->height = max(height(n->leftchild), height(n->rightchild))+1;
return n;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rotateLeft
函数的node
参数是rotateLeft
中的局部变量。换句话说,当您在
rotateLeft
中为该变量赋值时,insert
中的n
变量不会被修改。您需要通过指针或引用将n
传递给rotateLeft
,即同样
的原理也
适用于
insert
的n
参数 - 如果您希望函数修改变量的值,则需要向其传递一个指针或引用该变量而不是它的值。The
node
parameter to yourrotateLeft
function is a local variable inrotateLeft
.In other words, when you assign a value to that variable in
rotateLeft
, then
variable ininsert
is not modified. You need to passn
torotateLeft
either through a pointer or through a reference, i.e.either
or
The same principle applies to
insert
'sn
parameter - if you want a function to modify the value of a variable, you need to pass it a pointer or reference to that variable rather than its value.