aula 20 – laboratório: Árvores...

17
1 Aula 20 – Laboratório: Árvores AVL Algoritmos e Estruturas de Dados I Prof. Jesús P. Mena-Chalco [email protected] Q1-2017

Upload: dinhliem

Post on 05-Dec-2018

224 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

1

Aula 20 – Laboratório:Árvores AVL

Algoritmos e Estruturas de Dados I

Prof. Jesús P. [email protected]

Q1-2017

Page 2: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

2

AVL (Adelson-Velskii e Landis)

Page 3: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

3

AVL

G.M. Adelson-Velskii y E.M. Landis“An algorithm for the organization of information”. Proceedings of the USSR Academy of Sciences, vol. 146, pp. 263–266, 1962

AVL foi a primeira estrutura (conhecida) de árvore de altura balanceada/equilibrada.

Page 4: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

4

Rotação: Esquerda & Direita

X

YT1

T3T2

Y

T3X

T2T1

Varredura e-r-d: T1, X, T2, Y, T3 Varredura e-r-d: T1, X, T2, Y, T3

R. a direita

R. a esquerda

Page 5: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

5

Rotação: LL

p

T3f

T2T1

a

T4 p

f

T2T1

a

T4T3

R. a direita(a)

Page 6: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

6

p

T3

Rotação: RR

a

T1

f

T4

p

a

T2T1

f

T4T3

R. a esquerda(a)T2

Page 7: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

7

p

T2T1

Rotação: LR

p

fT1

a

T4f

p

T2T1

a

T4T3

R. a esquerda(p)

T3T2

f

a

T4

T3

R. a direita(a)

Page 8: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

8

Rotação: RL

T1

f

a

pf

a

T2T1

p

T4T3

R. a direita(p)

T3T2

R. a esquerda(a)

T4

T1

T2

a

f

p

T4T3

Page 9: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

9

Atividade prática: AVL

Baixar o arquivo avl.tgz

Descomprimir os arquivos:tar xvfz avl.tgz 

O diretório avl contem o código fonte avl.c e 4 arquivos com vetores de teste:

vetor1.dat (20 elementos, ordem crescente)

vetor2.dat (20 elementos, ordem decrescente)

vetor3.dat (20 elementos, aleatório)

vetor4.dat (40000 elementos, aleatório)

Preencher a planilha (aberto até 01/maio, 23h50): https://docs.google.com/spreadsheets/d/1CfWyhAT6UpKHmV-N1p33np5o_E37eLqUpyM5kBLy-_8/edit?usp=sharing

(O bônus, de 0.5 na média das listas, é válido para preenchimento da planilha até 01/maio, 23h50)

Page 10: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

10

Atividade prática: AVL

Page 11: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

11

Atividade prática: AVL

X

YT1

T3T2

Y

T3X

T2T1

r. a direitar. a esquerda

Page 12: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

12

Atividade prática: AVL

Page 13: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

13

Atividade prática: AVL

Page 14: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

14

Atividade prática: AVL

TO-DO: Você deve implementar essa parte

TO-DO:Você deve implementar essa parte

Veja dicas no código fonteavl.c

Page 15: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

15

avl.c

Page 16: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

16

Testes empíricos

$ gcc avl.c ­o avl.exe

$ ./avl.exe < vetor1.dat

$ ./avl.exe < vetor2.dat

$ ./avl.exe < vetor3.dat

$ ./avl.exe < vetor4.dat

Para cada teste serão apresentadas várias informações:

- Veja como o fator de balanceamento está sendo calculado.

- Como os valores são inseridos na ABB?

- Qual é a altura final da árvore? (este valor deverá ser inserido na planilha no GoogleDocs).

- Qual é o número total de rotações que foram realizadas para inserir todos os elementos? (este valor deverá ser inserido na planilha no GoogleDocs).

Para pensar:O teste para o vetor4.dat (com 40mil elementos) tem uma altura inferior a 23?

Page 17: Aula 20 – Laboratório: Árvores AVLprofessor.ufabc.edu.br/~jesus.mena/courses/mcta001-1q-2017/AED1-20.pdf · AVL G.M. Adelson-Velskii y E.M. Landis “An algorithm for the organization

17

Comentários finais...

Assista o seguinte vídeo:https://www.youtube.com/watch?v=Pbdo-ozuOug

Use software livre (use Linux!)

Existe uma plataforma, similar ao HackerRank muito utilizada para contratações de estágio em empresas internacionais (Google, Uber, Facebook). Está com um tempinho… faça os testes: https://www.interviewbit.com/problems/loop_cmpl/

Bom recesso!