AVL树旋转问题

时间:2016-09-26 16:25:41

标签: c avl-tree tree-rotation

我一直面临着AVL树实现的一个非常奇怪的问题。鉴于下面的代码,我只能在没有正确轮换的情况下运行它,因为如果我这样做,我就会崩溃。我已经尝试过调试,删除文件和重新构建项目,然后重建,这些都没有用。

事先我很抱歉,如果代码有点难以理解,我是巴西人并且变量名称大部分都是葡萄牙语,如果这证明是帮助解决这个问题的问题,我会说将变量名称更改为英语。

avl.h档案:

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

typedef struct avl{
    int elemento;
    struct avl *esq;
    struct avl *dir;
    int altura;
}avl;

avl *novo_no(int);
avl *rotacao_dir(avl *);
avl *rotacao_esq(avl *);
avl *insercao_avl(avl *, int);
int calcula_fb(avl *);
int max(int, int);
int get_altura(avl *);
void in_order(avl *);

avl.c档案:

#include "avl_tree.h"

//creates a new node with the given integer
avl *novo_no(int elemento){
    avl *novo = (avl *)malloc(sizeof(avl));
    if(novo){
        novo->elemento = elemento;
        novo->dir = NULL;
        novo->esq = NULL;
        novo->altura = 1;
        return novo;
    }else
        exit(1);
}

//right rotation
avl *rotacao_dir(avl *arv){

    avl *filho_esq = arv->esq;
    avl *subarvore_dir = filho_esq->dir;

    filho_esq->dir = arv;
    arv->esq = subarvore_dir;

    //updates the height

    arv->altura = max(get_altura(arv->esq), get_altura(arv->dir)) + 1;
    filho_esq->altura = max(get_altura(filho_esq->esq), get_altura(filho_esq->dir)) + 1;
    //printf("Altura de [%d]: %d\n\n", filho_esq->elemento, filho_esq->altura); //debug print of the node and it's height

    return filho_esq;
}

//left rotation
avl *rotacao_esq(avl *arv){
    avl *filho_dir = arv->dir;
    avl *subarvore_esq = filho_dir->esq;

    filho_dir->esq = arv;
    arv->dir = subarvore_esq;

    //updates the height

    arv->altura = max(get_altura(arv->esq), get_altura(arv->dir)) + 1;
    filho_dir->altura = max(get_altura(filho_dir->esq), get_altura(filho_dir->dir)) + 1;
    printf("Altura de [%d]: %d", filho_dir->elemento, filho_dir->altura);

    return filho_dir;
}

//insertion
avl *insercao_avl(avl *arv, int elemento){

    /*1. BST normal insertion*/

    if(arv == NULL) return novo_no(elemento);

    avl *aux = arv;
    if(elemento <= aux->elemento)
        aux->esq = insercao_avl(aux->esq, elemento);
    else
        aux->dir = insercao_avl(aux->dir, elemento);

    //Inicia os testes para as rotacoes

    /*2. updates height */
    arv->altura = max(get_altura(arv->esq), get_altura(arv->dir)) + 1;

    /*3. get the balance*/
    int fb = calcula_fb(arv);
    printf("FB do no[%d] = %d\n",arv->elemento, fb);

    //If unbalanced node, 1 of the 4 following cases will apply:

    //3.1 esq->esq
    if (fb > 1 && elemento < arv->esq->elemento)
        return rotacao_dir(arv);

    //3.2 dir->dir
    else if (fb < -1 && elemento > arv->dir->elemento)
        return rotacao_dir(arv);

    //3.3 esq->dir
    else if (fb > 1 && elemento > arv->esq->elemento){
        arv->esq =  rotacao_dir(arv->esq);
        return rotacao_dir(arv);
    }

    //3.4 dir->esq
    else if (fb < -1 && elemento < arv->dir->elemento){
        arv->dir = rotacao_dir(arv->dir);
        return rotacao_dir(arv);
    }

    /*4. Returns node */
    return arv;
}

//gets the balancing factor of a node
int calcula_fb(avl *arv){
    if(arv == NULL) return 0;
    else return(get_altura(arv->esq) - get_altura(arv->dir));
}


//get max between 2 values
int max(int a, int b){
    return((a > b) ? a : b);
}

//get a node's height
int get_altura(avl *arv){
    if(arv == NULL) return 0;
    else return arv->altura;
}

// in-order travel of the tree
void in_order(avl *arv){
    if(arv == NULL) // se arvore vazia então finaliza
        return;

    in_order(arv->esq); // chamada recursiva para a subarvore esquerda
    printf("[ %2d ] ", arv->elemento); // visita a raiz
    in_order(arv->dir); // chamada recursiva para a subarvore direita
}

main.c档案:

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

int main(){

    avl *raiz;
    int i;
    for(i = 0; i < 10; i++)
        raiz = insercao_avl(raiz, i);

    in_order(raiz);


    return 0;
}

正如您所看到的,我的左右旋转功能(分别为rotaciona_dirrotaciona_esq)基本上都是镜像,所以我觉得正确的功能不起作用。我尝试过不同的电话,现在我只是使用for循环进行测试。如果我的输入正在减少,一切正常,但只要我需要调用rotaciona_dir函数,程序就会崩溃。

关于IDE /编译器的注意事项,我在Ubuntu 16.04 LTS系统中使用Code-Blocks IDE 13.12。

此外,欢迎任何代码改进或一般编程技巧。我自己并不是一个C程序员(Python是我的主要语言),所以答案可能非常简单,我只是没有得到它。

1 个答案:

答案 0 :(得分:1)

所以看起来我使用auto-complete有点太多了。我的所有案例都没有调用, as required. The case where,我的左旋转功能,这使代码出现故障。学过的知识。将此问题标记为已解决。感谢所有帮助过的人,特别感谢@IanAbbott指出新手的错误。