在具有完整可复制示例C的AVL树上旋转 [英] Rotation on AVL tree with full reproducible example C

查看:63
本文介绍了在具有完整可复制示例C的AVL树上旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此功能上停留了2天,所以我想弄清楚.下面的代码是我的尝试(感谢user3386109),以便在通用节点上进行rightRotate.我认为这很接近,但问题是当我运行代码时旋转发生了,但是当我实际打印值时,似乎什么也没发生.

I've been stuck on this function for 2 days, so I'm trying to figure it out. The code below is my try (thanks to user3386109 ) to make a rightRotate on a generic node. I think to be close but the problem is that when I run the code the rotation goes but when I print values actually it seems like nothing happened.

#include <stdio.h>
#include <stdlib.h>

typedef struct tree mynode;  
    struct tree{                    // node struct
    int value;
    int key;
    char color;
    struct tree *left;
    struct tree *right;
};
//allocate memory, set color, key, value and NULL sons 
mynode* red_node_create(int Key, int Value) {
    mynode* parent=(mynode*) calloc(1, sizeof(mynode));
    parent->color= 1;
    parent->value=Value;
    parent->key=Key;
    parent->left=NULL;
    parent->right=NULL;
    return parent;
}

void rightRotate(mynode **parentPtr, mynode *child)
{
    // make sure the arguments are valid for a right rotation
    if (parentPtr == NULL || child == NULL || child->left == NULL)
    {
        printf("Error while rotating right\n");
        return;
    }
    printf("GOing to right rotate\n");
    // save the three node addresses involved in the rotation
    mynode *F = child;
    mynode *D = F->left;
    mynode *E = D->right;

    // perform the rotation
    *parentPtr = D;
    D->right = F;
    F->left = E;
    return;
}

int main(){

mynode* root=red_node_create(0,0);
mynode* F;
F=red_node_create(3,3);
mynode* D=red_node_create(2,2);;
mynode* E=red_node_create(1,1);;
F->left=D;
D->left=E;
root->right= F;
rightRotate(&root,F);
printf(" %d %d %d \n\n",root->right->value,root->right->right->value,
root->right->left->value);
free(F);
free(D);
free(E);
free(root);
return 0;
}

在此示例中,程序最终以分段错误结束,显然 rightRotate 运行了,但是它没有执行任务.

In this example the program ends with a segmentation fault obviously the rightRotate runs but it doesn't do its job.

推荐答案

您显然没有适当的算法来进行右旋转...

You obviously didn't have the proper algorithm for a right rotate...

SEG_FAULT是由于您的测试树未正确初始化.

And the SEG_FAULT was due to your test tree not being initialized correctly.

#include <stdio.h>
#include <stdlib.h>


struct tree
{               // node struct
  int value;
  int key;
  char color;
  struct tree *left;
  struct tree *right;
};

typedef struct tree mynode;   // move this below declaration for struct tree.
                              // keeping declarations in the right order does
                              // a great deal in making code easier to navigate. 

//allocate memory, set color, key, value and NULL sons 
mynode* red_node_create (int Key, int Value)
{
  mynode *p = (mynode *) calloc (1, sizeof (mynode));
  p->color = 1;
  p->value = Value;
  p->key = Key;
  p->left = NULL;
  p->right = NULL;
  return p;
}

// having a proper way to free memory is very important when dealing 
// with trees and lists.  It would be better if this was not recursive.
// I'll leave this exercise to you.  I'm sure you can find examples on 
// the Net.
void free_node(mynode* p)
{
    if (!p)
        return;
    
    free_node(p->left);
    free_node(p->right);

    free(p);   
}

// Rotation only needs 1 parameter: the root node around which
// rotation occurs.
void rightRotate (mynode ** root)
{
    if (!root || !*root || !(*root)->left)
    {
        printf ("bad arguments in myRightRotate()\n");
        return;
    }

    // take left node, make it parent,  make old parent the right node
    // of new parent, and make right node of old left node the left node
    // of old_parent
    
    // using letters as in graphics on this page: 
    // https://en.wikipedia.org/wiki/Tree_rotation
    
    // these non leaves nodes cannot be NULL
    mynode* Q = *root;
    mynode* P = Q->left;
    
    // the leaf nodes could be NULL.  Only B is needed.
    // but A and C are checked in unit test look-alike below.
    mynode* A = P->left;
    mynode* B = P->right;
    mynode* C = Q->right;
    
    // rotate
    *root = P;
    P->right = Q;
    Q->left = B;
    
#define CHECK_RIGHT_ROTATE   // undef as needed.
#ifdef CHECK_RIGHT_ROTATE

    // make sure the nodes are in place.    
    if (P != *root)
        printf("RR error. root is not P\n");

    if (Q != (*root)->right)
        printf("RR error. root->right is not Q\n");
    
    if (A != P->left)
        printf("RR error. A is not at P->left\n");
    if (B != Q->left)
        printf("RR error. B is not at Q->left\n");
    if (C != Q->right)
        printf("RR error. C is not at Q->right\n");
#endif
}

int main ()
{ 
    // make minimal tree for a proper rotate.  root has both left and right node.
    // - left node has both left and right leaf nodes, values and 1 and 2 
    // - right node is a leaf node. value 3
    mynode *root      = red_node_create (0, 0);
    root->left        = red_node_create (0, 0);
    root->left->left  = red_node_create(1, 1);
    root->left->right = red_node_create(2, 2);
    root->right       = red_node_create(3, 3);

    // before rotate, we should have
    // root->left->left:   leaf, value 1
    // root->left->right:  leaf, value 2
    // root->right: leaf, value 3

    printf ("before:  %d %d %d \n", root->left->left->value, root->left->right->value, root->right->value);

    rightRotate (&root);
    
    // after rotate, we should have
    // root->left:         leaf, value 1
    // root->right->left:  leaf, value 2
    // root->right->right: leaf, value 3
    
    printf ("after:   %d %d %d \n", root->left->value, root->right->left->value, root->right->right->value);
    
    free_node(root);
    
    return 0;
}

您可以在此处运行程序: https://onlinegdb.com/HyQAfPFtP

You can run the program here: https://onlinegdb.com/HyQAfPFtP

这篇关于在具有完整可复制示例C的AVL树上旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆