【问题标题】:Right-Rotate of AVL is giving problems?AVL 的右旋有问题吗?
【发布时间】:2016-05-22 12:37:43
【问题描述】:

我正在为 AVL 树编写右旋转。但它给出了运行时错误。目前我忽略了 AVL 树的高度部分。我稍后会处理它。但只是在 5 处应用右旋转会出现问题。请帮助。 我使用的 AVL 树是:

                                  9(Root)

                5(Left Child)                          13(Right Child)

            3                 7                  11                       17
      2
1

C 代码:

#include <stdio.h>
#include <malloc.h>


struct AVLTree
{
  int data;
  struct AVLTree *left;
  struct AVLTree *right;
};



struct AVLTree *RightRotate(struct AVLTree *rootop)
{
    struct AVLTree *A = rootop->left;
    rootop->left = A->right;
    A->right = rootop;

    return A;
}

int main()
{
    struct AVLTree * root = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    root-> data = 9;
    struct AVLTree * l = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    l -> data = 5;
    struct AVLTree * ll = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    ll -> data = 3;
    ll -> left = ll -> right = NULL;
    struct AVLTree * lll = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    lll -> data = 2;
    lll -> left = lll -> right = NULL;
    ll->left = lll;
    struct AVLTree * llll = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    llll -> data = 1;
    llll -> left = llll -> right = NULL;
    lll->left = llll;
    struct AVLTree * lr = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    lr -> data = 7;
    lr -> left = lr -> right = NULL;
    l -> left = ll;
    l -> right = lr;
    struct AVLTree * r = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    r -> data = 13;
    struct AVLTree * rl = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    rl -> data = 11;
    rl -> left = rl -> right = NULL;
    struct AVLTree * rr = (struct AVLTree *)malloc(sizeof(struct AVLTree));
    rr -> data = 17;
    rr -> left = rr -> right = NULL;
    r -> left = rl;
    r -> right = rr;
    root -> left = l;
    root -> right = r;




    RightRotate(l);
    printf("Root is %d\n",root->data);

    printf("Left is %d\n",l->data);
    printf("Left left is %d\n",l->left->data);
    printf("Left right is %d\n",l->right->data);

    printf("Left Ka Left is %d\n",ll->data);
    printf("LL left is %d\n",ll->left->data);

    printf("Left Ka Left Ka Left is %d\n",lll->data);
    printf("LLL left is %d\n",lll->left->data);


    return 0;
}

【问题讨论】:

  • 什么问题?你可以说得更详细点吗?它有段错误吗?你试过调试吗?
  • 只是运行时错误。你认为调用函数是错误的吗?如果我把 l = RightRotate(l);它有效,但给出了错误的值。

标签: c data-structures tree avl-tree


【解决方案1】:

我认为你应该写l = RightRotate(l) 之类的东西,因为你的RightRotate 函数返回struct AVLTree *

【讨论】:

  • 首先,这种情况出现Runtime error是正常的,因为RightRotate(l)之后,你设置了l-&gt;left=NULL,所以l-&gt;left-&gt;data会报错,其实这是一个segmentation fault,因为您想从空指针获取数据。其次,您可以尝试ll=RightRotate(ll),这对 AVL 树很有意义。最后,您确定包含malloc.h 而不是stdlib.h
  • 是的,它应该是stdlib.hnot malloc.h。但是l-&gt;left = NULLl = RightRotate(l) 之后怎么样?新的l = 3 和'l->left = 2'。是的ll=RightRotate(ll)也可以做。但这也是给出错误的值吗?
  • 他们不是错误的价值观,他们有理由。因为你先设置了l-&gt;left= an address(e.g.23ef, it's ll's address),然后我们将ll的地址重置为lll的地址,但是l-&gt;left仍然是23ef。因此,如果您希望 left left is 2 只需重置 l-&gt;left=ll
  • 我正在设置 l-&gt;left = NULLl 本身正在从 5 变为 3。所以 l-&gt;left 不再是 Null。但 ll 仍然是 3 而它应该是 2?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多