Arvore rubro negra em c

803 palavras 4 páginas
#include <stdio.h>
#include <stdlib.h>

typedef struct tipono{ int item; enum {rubro , negro} cor; struct tipono *esq, *dir, *pai; }tipono;

void inicializa(tipono **arv){ (*arv)= NULL; } void rotesq(tipono **t, tipono *x){ tipono *y; y = x->dir; x->dir = y->esq;

if (y->esq != NULL) y->esq->pai = x;

y->pai = x->pai;

if (x->pai == NULL) (*t) = y; else if (x == x->pai->esq) { x->pai->esq=y;

}else{ x->pai->dir=y; } y->esq=x; x->pai=y; } void rotdir(tipono **t, tipono *x){ tipono *y; y = x->esq;

x->esq = y->dir; if (y->dir != NULL) y->dir->pai = x;

y->pai = x->pai;

if (x->pai == NULL) (*t) = y; else if (x == x->pai->esq) { x->pai->esq=y;

}else{ x->pai->dir=y; } y->dir=x; x->pai=y; } void insere(tipono **t, tipono *z){ tipono *y, *x;

y = NULL; x = (*t); while (x!= NULL){ y = x; if (z->item == x->item){ printf("Elemento ja existe\n"); return; } else if (z->item < x->item){ x = x->esq;

}else x = x->dir; } z->pai = y;

if (y==NULL) (*t) = z; else { if (z->item < y->item) y->esq = z; else y ->dir = z; }

z ->dir = z->esq = NULL; z->cor = rubro;

inserefix (t,z);

} void insereA(tipono **t, int valor){ tipono *novo = (tipono *) malloc (sizeof(tipono)); novo->item = valor; novo->cor = rubro; novo->dir = novo->esq = novo->pai = NULL;

insere(t, novo); } void enordem(tipono *t){ if (t != NULL){ enordem(t->esq); printf("%d ", t->item); enordem(t->dir); } } void inserefix (tipono **t, tipono *z){ tipono *y;

while (z->pai != NULL && z->pai->cor == rubro) { if(z->pai == z->pai->pai->esq) { y = z->pai->pai->dir; if (y != NULL &&y->cor == rubro) { z->pai->cor = negro; y->cor = negro; z->pai->pai->cor = rubro; z = z->pai->pai; }

Relacionados

  • Árvore rubro negra
    4598 palavras | 19 páginas
  • Arvores Rubro Negras
    1542 palavras | 7 páginas
  • Arvores Rubro Negras
    635 palavras | 3 páginas
  • Arvores Red- Black
    1062 palavras | 5 páginas
  • Abordagem de arvores rubro-negra
    2304 palavras | 10 páginas
  • Arvore RN
    3044 palavras | 13 páginas
  • Arvores binarias
    1706 palavras | 7 páginas
  • TrabalhoEstrutura De Dados
    3201 palavras | 13 páginas
  • Arvore rubro negra (redblack tree)
    1225 palavras | 5 páginas
  • Metodo de pesquisa e ordenacao
    1447 palavras | 6 páginas