Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ALLOC (TNo*)malloc(sizeof(TNo));
- typedef struct no{
- struct no *esq, *dir;
- int inf, sup;
- } TNo;
- void inicializa(TNo **ptr){
- (*ptr) = NULL;
- }
- /*void insere(TNo **ptr, int chave){
- if((*ptr) == NULL){
- (*ptr) = ALLOC;
- (*ptr)->esq = NULL;
- (*ptr)->dir = NULL;
- (*ptr)->chave = chave;
- } else{
- if(chave < (*ptr)->chave)
- insere(&(*ptr)->esq, chave);
- else if(chave > (*ptr)->chave)
- insere(&(*ptr)->dir, chave);
- else
- printf("\nChave #%d ja existente!", chave);
- }
- }
- remove somente se for exatamente o intervalo ou estiver dentro
- void antecessor(TNo *q, TNo **r){
- if((*r)->dir != NULL)
- antecessor(q, &(*r)->dir);
- else{
- q->chave = (*r)->chave;
- q = (*r);
- (*r) = (*r)->esq;
- free(q);
- }
- }
- */
- TNo convertToTNo(struct no *r){
- TNo n;
- n.dir = r->dir;
- n.esq = r->esq;
- n.inf = r->inf;
- n.sup = r->sup;
- return n;
- }
- void insere (TNo **ptr, TNo no){
- printf("\n%d-%d\n", no.inf, no.sup);
- if((*ptr) == NULL){
- (*ptr) = ALLOC;
- (*ptr)->esq = NULL;
- (*ptr)->dir = NULL;
- (*ptr)->inf = no.inf;
- (*ptr)->sup = no.sup;
- printf("no1\t");
- }
- // maior que o inferior E menor que o superior (entre inf e sup)
- else if((*ptr)->inf <= no.inf && (*ptr)->sup >= no.sup){
- //noJaInserido
- printf("\n%d %d %d %d\n", (*ptr)->inf, (*ptr)->sup, no.inf, no.sup);
- printf("\nConjunto ja existente!");
- }
- // INFERIOR maior que o superior em 1 OU igual ao superior OU entre o inferior e o superior
- else if(((*ptr)->sup == no.inf - 1) || ((*ptr)->sup == no.inf) || ((*ptr)->inf <= no.inf && (*ptr)->sup >= no.inf)){
- if((*ptr)->dir){
- if((*ptr)->dir->inf - 1 <= no.sup){ // maior que o inferior do próximo nó em pelo menos 1
- printf("22->dir\t");
- //junta-se a ptr, alterando o superior para o atual
- (*ptr)->dir->inf = no.inf;
- struct no *aux = (*ptr)->dir;
- TNo a = convertToTNo(aux);
- free((*ptr)->dir);//REMOVE
- (*ptr)->dir = NULL;//REMOVE
- insere(&(*ptr), a);
- }
- else if((*ptr)->dir->sup < no.sup){ //menor que o inferior do próximo nó a esquerda
- TNo **aux = ptr;
- *aux = (*aux)->dir;
- insere(aux, no);
- printf("a2->dir");
- }
- }
- else{
- (*ptr)->sup = no.sup;
- printf("b1\t");
- }
- }
- // SUPERIOR menor que o inferior em 1 OU igual ao inferior OU entre o inferior e o superior
- else if(((*ptr)->inf == no.sup + 1) || ((*ptr)->inf == no.sup) || ((*ptr)->inf <= no.sup && (*ptr)->sup >= no.sup)){
- if((*ptr)->esq){
- if((*ptr)->esq->inf - 1 <= no.inf){ //maior que o inferior do próximo nó a esquerda em pelo menos 1
- printf("22->esq\t");
- //junta-se a ptr, alterando o superior para o atual
- (*ptr)->esq->sup = no.sup;
- struct no *aux = (*ptr)->esq;
- TNo a = convertToTNo(aux);
- free((*ptr)->esq);//REMOVE
- (*ptr)->esq = NULL;//REMOVE
- insere(&(*ptr), a);
- //remover o no da esquerda e alocá-lo ao principal
- }
- else if((*ptr)->esq->inf > no.inf){ //menor que o inferior do próximo nó a esquerda
- TNo **aux = ptr;
- *aux = (*aux)->esq;
- insere(aux, no);
- printf("a2->esq\t");
- }
- //insere(&(*ptr)->esq, no);
- }
- else{
- (*ptr)->inf = no.inf;
- //insere(&(*ptr), )
- printf("b2\t");
- }
- }
- else if((*ptr)->inf > no.sup){
- insere(&(*ptr)->esq, no);
- printf("c1\t");
- }
- else if((*ptr)->sup < no.sup){
- insere(&(*ptr)->dir, no);
- printf("c2\t");
- }
- printf("\n\n");
- //else if()
- /*TNo *aux = *ptr->dir;
- (*ptr)->sup = (*ptr)->dir->sup;
- (*ptr)->dir = (*ptr)->dir->dir;
- free(aux);*/
- }
- /*
- void inOrdem(TNo **ptr){
- if((*ptr) != NULL){
- free((*ptr));
- inOrdem((*ptr)->esq);
- inOrdem((*ptr)->dir);
- }
- }
- */
- /*
- void remover(TNo **ptr, int chave){
- if((*ptr) == NULL)
- printf("\nA chave #%d nao esta na arvore!", chave);
- else if(chave < (*ptr)->chave)
- remover(&(*ptr)->esq, chave);
- else if(chave > (*ptr)->chave)
- remover(&(*ptr)->dir, chave);
- else{
- TNo *aux = *ptr;
- if((*ptr)->dir == NULL){
- (*ptr) = (*ptr)->esq;
- free(aux);
- } else if((*ptr)->esq == NULL){
- (*ptr) = (*ptr)->dir;
- free(aux);
- } else antecessor((*ptr), &(*ptr)->esq);
- }
- }
- void inOrdem(TNo *ptr){
- if(ptr != NULL){
- inOrdem(ptr->esq);
- printf("%d\n", ptr->chave);
- inOrdem(ptr->dir);
- }
- }
- void preOrdem(TNo *ptr){
- if(ptr != NULL){
- printf("%d\n", ptr->chave);
- preOrdem(ptr->esq);
- preOrdem(ptr->dir);
- }
- }
- void posOrdem(TNo *ptr){
- if(ptr != NULL){
- posOrdem(ptr->esq);
- posOrdem(ptr->dir);
- printf("%d\n", ptr->chave);
- }
- }
- void filhoEsq(TNo *ptr, int chave){
- if(ptr->chave == chave){
- if(ptr->esq != NULL){
- printf("%d", ptr->esq->chave);
- }
- else printf("Nao tem filho a esquerda");
- } else{
- while(ptr->chave != chave){
- if(ptr->chave > chave){
- ptr = ptr->esq;
- } else{
- ptr = ptr->dir;
- }
- }
- if(ptr == NULL) return;
- }
- }
- */
- void main(){
- TNo* raiz = NULL;
- int i;
- //for (i = 0; i < 10; i++)
- inicializa(&raiz);
- TNo n;
- n.dir = NULL;
- n.esq = NULL;
- n.inf = 20;
- n.sup = 25;
- insere(&(raiz), n);
- n.inf = 15;
- n.sup = 18;
- insere(&(raiz), n);
- n.inf = 27;
- n.sup = 35;
- insere(&(raiz), n);
- n.inf = 19;
- n.sup = 24;
- insere(&(raiz), n);
- n.inf = 26;
- n.sup = 33;
- insere(&(raiz), n);
- n.inf = 37;
- n.sup = 40;
- insere(&(raiz), n);
- n.inf = 14;
- n.sup = 36;
- insere(&(raiz), n);
- printf("\n\t%d-%d\n", (raiz)->inf, (raiz)->sup);
- if((raiz)->esq)
- printf("\n%d-%d", (raiz)->esq->inf, (raiz)->esq->sup);
- if((raiz)->dir)
- printf("\t\t%d-%d", (raiz)->dir->inf, (raiz)->dir->sup);
- //void insere (TNo **ptr, TNo no){
- // if((*ptr) == NULL)
- // alocaNo
- // else
- // if((*ptr)->inf <= no.inf && (*ptr)->sup >= no.sup)
- // noJaInserido
- // else
- // if(((*ptr)->sup == no.inf - 1) && ((*ptr)->dir->inf == no.sup + 1)){
- // TNo *aux = *ptr->dir;
- // (*ptr)->sup = (*ptr)->dir->sup;
- // (*ptr)->dir = (*ptr)->dir->dir;
- // free(aux);
- // }
- // }
- // else
- // if()
- /*
- int opt = -1;
- while(opt != 0){
- system("cls");
- printf("1- Inserir Chave\n");
- printf("2- Remover Chave\n");
- printf("3- Pesquisar Filho da Esquerda\n");
- printf("4- Pesquisar Pai\n");
- printf("5- Exibir em Ordem\n");
- printf("6- Exibir em Pre Ordem\n");
- printf("7- Exibir em Pos Ordem\n");
- printf("0- Sair\n");
- scanf("%d", &opt);
- system("cls");
- switch (opt){
- case 1: {
- int chave;
- printf("Digite a Chave a ser inserida: ");
- scanf("%d", &chave);
- insere(&(raiz), chave);
- break;
- }
- case 2: {
- int chave;
- printf("Digite a Chave a ser removida: ");
- scanf("%d", &chave);
- remover(&(raiz), chave);
- break;
- }
- case 3: {
- int chave;
- printf("Digite a Chave a ser pesquisada: ");
- scanf("%d", &chave);
- filhoEsq(raiz, chave);
- system("pause");
- break;
- }
- case 4: {
- int chave;
- printf("Digite a Chave a ser pesquisada: ");
- scanf("%d", &chave);
- system("pause");
- break;
- }
- case 5: {
- inOrdem(raiz);
- system("pause");
- break;
- }
- case 6: {
- preOrdem(raiz);
- system("pause");
- break;
- }
- case 7: {
- posOrdem(raiz);
- system("pause");
- break;
- }
- }
- } */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement