INF 1010
29/08/16 1
Estruturas de Dados Avançadas
Informações do curso
Ø www.inf.puc-rio.br/~noemi/eda-16.2
• só para 3wb!
29/08/16 2
Sobre o curso
Ø Estruturas de dados• Estratégias básicas para organização de
dados na construção de algoritmos
Ø Objetivo:• Apresentar estruturas de dados de uso
comum
29/08/16 3
ProgramaØ Parte 1
• árvores
• árvores binárias de busca
• custo computacional
• filas de prioridades (heaps)
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 4
Ø Parte 3:
• partições dinâmicas
• árvores B
• grafos
Ø Parte 2
• mapas de bits
• tabelas hash
• árvores AVL e rubro-negras
Critério de Avaliação
29/08/16 5
Critério 4NF = ( G1 + G2 + G3) / 3
se G1, G2 e G3 >= 3,0 e NF >= 5,0então MÉDIA = NF
senão o aluno faz G4;
se G4 >= 3,0então MÉDIA = (Gm + Gn + G4) / 3 onde Gm e Gn são as maiores notas de G1, G2 e G3
senão (i.e., G4 < 3,0)então MÉDIA = ( (G1 + G2 + G3 + (G4*3) ) / 6
Os graus G1, G2 e G3 serão compostos por nota de prova e trabalho.Gn = (Pn2.Tn)1/3
Bibliografia
Celes, W.; Cerqueira, R.; Rangel, J.L. (2016) Introdução a Estruturas de Dados – com técnicas de programação em C, Editora Campus.
29/08/16 6
Revisão – estrutura de dados e tipo abstratode dados
29/08/16 7
inte
rface
: mod
ulo.
h
programaque usa esse
modulo
#include “modulo.h"
modulo.cimplementação
aqui aparecem as estruturas de dados variadas!
29/08/16 8
interface: conjunto.hjprogramaque usa conjunto
#include “conjunto.h”
...int main (void) {tconjunto C1, C2;...C1 = criaConj();...
conjunto.cimplementaçãolistaencadeada?array?
typedef structsconjunto *tconjunto;
tconjunto criaConj (void);int insereConj (tconjunto c,
int e);...int estaNoConj (tconjunto c,
int e);...
Módulo Conjunto - interface
29/08/16 9
typedef structsconjunto *tconjunto;
tconjunto criaConj (void);
int eConjValido (tconjunto c);
tconjunto insereConj (tconjunto c, int e);
tconjunto retiraConj (tconjunto c, int e);
int estaNoConj (tconjunto c, int e);
/* ... outras */
Módulo Conjunto - 1
29/08/16 10
#include "conjunto.h"#include <stdlib.h>#include <stdio.h>
struct sconjunto {int info;struct sconjunto *prox;
};
tconjunto criaConj (void) {return NULL;
}
...
Módulo Conjunto - 1
29/08/16 11
#include "conjunto.h” /* fundamental */#include <stdlib.h>#include <stdio.h>
struct sconjunto {int info;struct sconjunto *prox;
};
tconjunto criaConj (void) {return NULL;
}
...
Módulo Conjunto – 2
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 12
...tconjunto insereConj (tconjunto c, int e) {
struct sconjunto *novo = (struct sconjunto *) malloc (sizeof(struct sconjunto));
novo->info = e;novo->prox = c;return novo;
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Módulo Conjunto – 2
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 13
...tconjunto insereConj (tconjunto c, int e) {
struct sconjunto *novo = (struct sconjunto *) malloc (sizeof(struct sconjunto));
novo->info = e; /* e se não conseguiu alocar??? seg fault! */novo->prox = c;return novo;
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Módulo Conjunto – 2
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 14
...tconjunto insereConj (tconjunto c, int e) {
struct sconjunto *novo = (struct sconjunto *) malloc (sizeof(struct sconjunto));
if (novo!=NULL) {novo->info = e;novo->prox = c;
}return novo;
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Módulo Conjunto – 2
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 15
...tconjunto insereConj (tconjunto c, int e) {struct sconjunto *novo =
(struct sconjunto *) malloc (sizeof(struct sconjunto));if (novo!=NULL) {novo->info = e;novo->prox = c;
}return novo; /* se não conseguiu inserir vai retornar NULL */
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Módulo Conjunto – 2
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 16
...tconjunto insereConj (tconjunto c, int e) {struct sconjunto *novo =
(struct sconjunto *) malloc (sizeof(struct sconjunto));if (novo!=NULL) {novo->info = e;novo->prox = c;
}return novo; /* se não conseguiu inserir vai retornar NULL */
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Problemas dessa implementação
Ø para eliminar...
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 17
Módulo Conjunto – 2.1
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 18
...tconjunto insereConj (tconjunto c, int e) {
if (estaNoConj (c, e)) return c;struct sconjunto *novo =
(struct sconjunto *) malloc (sizeof(struct sconjunto));if (novo!=NULL) {novo->info = e;novo->prox = c;
}return novo;
}
int estaNoConj (tconjunto c, int e) {while (c!=NULL)
if (c->info == e) return 1;else c = c->prox;
return 0;}
Problemas dessa implementação
Ø Para saber que está no conjunto...
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 19
Módulo Conjunto – outra implementação
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 20
tconjunto insereConj (tconjunto c, int e) { /* lista ordenada */
tconjunto novo;
if (estaNoConj (c, e)) return c;
novo = (struct sconjunto *) malloc (sizeof(struct sconjunto));if (novo!=NULL) novo->info = eelse return NULL;
/* tratar o caso qdo ainda não tem elemento algum: */
/* caso geral: *//* percorrer lista até encontrar posição correta */
}
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 21
tconjunto insereConj (tconjunto c, int e) {
…/* percorrer até achar a posição certa! */
aux1 = c;aux2 = c->prox;while (1)if ((aux2==NULL) || (aux2->info > e)) {
/* achou posicao */novo->prox = aux2;aux1->prox = novo;return c;
}else {
aux1 = aux2;aux2 = aux2->prox;
}}
0
Módulo Conjunto – outra implementação
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 22
int estaNoConj (tconjunto c, int e) {
while ((c!=NULL) && (c->info <= e))if (c->info == e) return 1;else c = c->prox;
return 0;
}
... e se não é um conjunto, mas sim um MAPA?
mapeamento (chave, valor)
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 23
Módulo Mapa- interface
29/08/16 24
typedef struct smapa *tmapa;
tmapa criaMapa (void);
int eMapa (tmapa c);
tmapa insereMapa (tmapa c, int chave, int valor);
tmapa retiraMapa (tmapa c, int chave);
int estaNoMapa (tmapa c, int chave);
/* ... outras */
... e se o elemento é mais complexo queum inteiro?
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 25
Módulo Mapa de objetos - interface
29/08/16 26
#include "meuobjeto.h"
typedef struct smapa *tmapa;
tmapa criaMapa (void);
int eMapa (tmapa c);
tmapa insereMapa (tmapa c, int chave, tobjeto *);
tmapa retiraMapa (tmapa c, int chave);
int estaNoMapa (tmapa c, int chave);
/* ... outras */
Módulo Mapa – Implementação
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 27
#include "mapaObj.h"#include <stdlib.h>#include <stdio.h>
struct smapa {int chave;tobjeto* obj;struct smapa *prox;
};
tmapa criaMapa (void) {return NULL;
}
…
laboratórios às 4as
• sala 546L
• ambiente linux• igual ao de inf1018• mas não usaremos características específicas
• entrega via ead
• conferir conta labgrad, espaço em disco e acesso ead (senha puc online)
29/08/16 © 2012 DI, PUC-Rio - Estruturas de Dados Avançadas – 2012.2 28