I've been facing a very weird problem with an AVL tree implementation. Given the code below, I can only run it with no right rotations, since that if I do, I have a crash. I already tried debugging, deleting files and remaking the project, and rebuild, none of this worked.
In advance, I apologize if the code is a little hard to understand, I'm brazilian and the variable names are mostly in Portuguese, if that proves to be a problem in helping the problem, I'll change the variable names to english.
avl.h
file:
#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
file:
#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
file:
#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;
}
As you can see, my right and left rotation functions(rotaciona_dir
and rotaciona_esq
respectively) are basically mirrors, so bugs me that the right one doesn't work. I've trying different calls, and right now I"m just using a for
loop for testing. If I make my input be decreasing, all works fine, but as soon as I need to call the rotaciona_dir
function, the program crashes.
On note about the IDE/compiler, I'm using Code-Blocks IDE 13.12, in a Ubuntu 16.04 LTS system.
Also, any code improvement or general programming tips are welcome. I'm not much a C programmer myself(Python is my main language), so the answer for this may be very simple and I'm just not getting it.
So Looks like I used auto-complete a little bit too much. None of my cases were calling rotacao_esq
, my left rotation function, and that was making the code malfunction. Lesson learned. Marking this questions as solved. Thanks to all who helped and special thanks to @IanAbbott for point that newbie mistake.