All pastes #2075146 Raw Edit

RN

public c v1 · immutable
#2075146 ·published 2011-06-05 17:41 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>//Definindo enumeração para facilitar a identificação das cores;typedef enum {NEGRO, RUBRO} Cores;//Definindo a estrutura a ser utilizada na solução do problematypedef struct rubro_negra{	//informações derivadas do problema	int id;	char modelo[20];	int ano;	char corVeiculo[15];	//informações da estrutura da arvore rubro negra	Cores cor;	struct rubro_negra *esq;	struct rubro_negra *dir;} TArvore;TArvore *RAIZ = NULL; //ponteiro para a raiz da arvoreTArvore *EXTERNO = NULL; //Nó externo//PrototiposTArvore *aloca(int id, int ano, char modelo[20], char cor[15]);void insere(int id, int ano, char modelo[], char corVeiculo[], TArvore **no, TArvore **pai, TArvore **avo, int *flag);void rota(TArvore **no, TArvore **pai, TArvore **avo, TArvore **bavo, int *flag);void imprime(TArvore *arv, TArvore *pai);//Função principalint main(){	int op; //variavel para "operação", para manter o controle do laço de menu    int flag = 0;    int id,ano;    char modelo[20];    char corVeiculo[15];	//TArvore *novo = (TArvore*) malloc(sizeof(TArvore)); //alocação de nó para guardar os valores inseridos pelo usuario e em seguida ser inserido na arvore    EXTERNO = (TArvore*) malloc(sizeof(TArvore));    EXTERNO->cor = NEGRO;    EXTERNO->id = -1;      RAIZ = EXTERNO;    TArvore *Arvore = EXTERNO;	do	{		printf("Escolha uma operação:\n 1 - Cadastrar\n 2 - Imprimir\n 3 - Buscar\n 4 - Remover\n 5 - Limpar Tela\n 0 - Sair\n");		scanf("%i",&op);		switch (op)		{			case 0: //sair				op = 0;				break;			case 1: //inserção								printf("Insira os dados do veiculo:\n");				printf("Identificador: ");				scanf("%d",&id);				/*printf("Ano: ");				scanf("%d",&ano);				printf("Modelo: ");				scanf("%s",modelo);				printf("Cor: ");				scanf("%s",corVeiculo);*/                ano = 1999;                strcpy(modelo, "modelo");                strcpy(corVeiculo, "cor");				insere(id,ano,modelo,corVeiculo, &RAIZ, &EXTERNO, &EXTERNO, &flag);                printf("flag:%d\n",flag);                if(flag == 2)                    flag = 0;                printf("flag:%d\n",flag);				break;			case 2: //impressão                    imprime(RAIZ, NULL);				break;			case 3: //busca				break;			case 4: //remoção				break;			default:				system("clear");		}	}	while(op != 0);	return 0;}//Função p/ alocar novo nóTArvore *aloca(int id, int ano, char modelo[20], char cor[15]){    TArvore *novo = (TArvore*) malloc(sizeof(TArvore));    novo->id = id;    novo->ano = ano;    strcpy(novo->modelo, modelo);    strcpy(novo->corVeiculo, cor);	novo->cor = RUBRO;	novo->dir = EXTERNO;	novo->esq = EXTERNO;    return novo;}//função de inserçãovoid insere(int id, int ano, char modelo[] ,char corVeiculo[], TArvore **no, TArvore **pai, TArvore **avo, int *flag){    TArvore *aux = (TArvore*) malloc(sizeof(TArvore*));    TArvore *novo = aloca(id,ano,modelo,corVeiculo);	if((*no) == EXTERNO)	{    		if(RAIZ == EXTERNO)		{            (*no) = novo;			(*no)->cor = NEGRO;			RAIZ = (*no);            		}		else		{            (*no) = novo;			if(novo->id < (*pai)->id)			{				(*pai)->esq = (*no);			}			else			{				(*pai)->dir = (*no);			}		}	}    	else	{   		if(novo->id != (*no)->id)		{			if(novo->id < (*no)->id)			{                aux = (*no)->esq;			}			else if(novo->id > (*no)->id)			{				aux = (*no)->dir;			}            insere(id,ano,modelo,corVeiculo,&aux,no,pai,flag);            			if(*flag == 1)			{                   printf("Chamou a rota\n");				rota(&aux,&(*no),&(*pai),&(*avo),flag);			}			else			{				if(*flag == 0)				{					*flag = 1;				}			}		}		else		{			printf("Nao é possivel inserir\n");		}	}	}void rota(TArvore **no, TArvore **pai, TArvore **avo, TArvore **bavo, int *flag){	TArvore *tio = (TArvore*) malloc(sizeof(TArvore*));	TArvore *aux = (TArvore*) malloc(sizeof(TArvore*));	    	*flag = 2;          	if((*pai)->cor == RUBRO)	{		if((*pai) == (*avo)->esq)		{			tio = (*avo)->dir;		}		else		{			tio = (*avo)->esq;		}		if(tio->cor == RUBRO) //caso 2.1		{printf("caso2.1\n");			(*pai)->cor = NEGRO;            tio->cor = NEGRO;			(*avo)->cor = RUBRO;						*flag = 0;		}		else //2.2		{						if((*no) ==  (*pai)->esq)			{                				if((*pai) == (*avo)->esq)				{printf("caso2.2.1\n"); //2.2.1 					aux = (*pai); 					aux->cor = NEGRO;					(*avo)->esq = (*pai)->dir;					(*pai)->dir = (*avo);                    (*avo)->cor = RUBRO;				}				else				{  printf("caso2.2.2\n");//2.2.2					aux = (*no);					aux->cor = NEGRO;                    (*pai)->esq = (*no)->dir;					(*avo)->dir = (*no)->esq;										aux->esq = (*avo);					aux->dir = (*pai);                    (*avo)->cor = RUBRO;                    *flag = 0;				}			}			else			{                				if((*pai) == (*avo)->dir)				{                     printf("caso2.2.3\n");//2.2.3					aux = (*pai);					aux->cor = NEGRO;					(*avo)->dir = (*pai)->esq;					aux->esq = (*avo);                    (*avo)->cor = RUBRO;                    				}				else				{ printf("caso2.2.4\n");//2.2.4					aux = (*no);					aux->cor = NEGRO;                    (*pai)->dir = (*no)->esq;					(*avo)->esq = (*no)->dir;										aux->esq = (*pai);					aux->dir = (*avo);                    (*avo)->cor = RUBRO;                    *flag = 0;                                                     				}			}			if((*bavo) != EXTERNO)			{                printf("bavo\n");				if((*no)->id < (*bavo)->id)				{					(*bavo)->esq = aux;				}				else				{					(*bavo)->dir = aux;				}			}			else			{				RAIZ = aux;			}		}		RAIZ->cor = NEGRO;     	}}//Fução de Impressãovoid imprime(TArvore *arv, TArvore *pai){    if(arv != EXTERNO)    {        if(pai != NULL)        {            printf("Id: %d\tAno: %d\t Modelo: %s Cor do Veic.: %s [Cor: %d\tPai:%d]\n",arv->id, arv->ano, arv->modelo, arv->corVeiculo, arv->cor,pai->id);            imprime(arv->esq,arv);            imprime(arv->dir,arv);        }        else        {            printf("Id: %d\tAno: %d\t Modelo: %s Cor do Veic.: %s [Cor: %d RAIZ]\n",arv->id, arv->ano, arv->modelo, arv->corVeiculo, arv->cor);            imprime(arv->esq,arv);            imprime(arv->dir,arv);        }    }}