binary heap
TRANSCRIPT
![Page 1: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/1.jpg)
Binary HeapMarcos Castro
![Page 2: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/2.jpg)
2
Binary Heap• Uma Binary Heap é uma árvore binária completa ou quase completa.• Exemplo de árvore binária completa:
Todos os níveis (exceto possivelmente oúltimo) estão completamente preenchidos.
![Page 3: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/3.jpg)
3
Binary Heap• Tem-se a Min-Heap e Max-Heap.• Min-Heap:• O valor de cada nó é maior ou igual do que o valor do seu pai, o menor valor
está na raiz.
• Max-Heap:• O valor de cada nó é menor ou igual do que o valor do seu pai, o maior valor
está na raiz.
• Os elementos estão dispostos na heap de forma que o pai sempre tem prioridade maior ou igual do que a prioridade de seus filhos.
![Page 4: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/4.jpg)
4
Binary Heap• Cada posição do array é considerado pai de outras duas posições que
são os filhos.• A posição “i” passa a ser pai das posições:• 2i + 1 (filho à esquerda)• 2i + 2 (filho à direita)
![Page 5: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/5.jpg)
5
Binary Heap• Para inserir um novo elemento, basta inserir na primeira posição vaga
do array, ou seja, ao final do array.• Inserir ao final do array quer dizer que inicialmente o novo elemento
é uma folha.• Feito isso, precisa-se levar o elemento inserido para a sua respectiva
posição na heap de acordo com a sua prioridade (“subir” na árvore).
![Page 6: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/6.jpg)
6
Binary Heap• Vamos construir uma Min-Heap.• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 12...
12
Array: 12
![Page 7: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/7.jpg)
7
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 7...
12
7 Violou a propriedade da min-heap!
Array: 12, 7
![Page 8: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/8.jpg)
8
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Corrigindo:
12
7
7
12
Array: 7, 12
![Page 9: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/9.jpg)
9
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 6...
7
12 6
Violou a propriedade da min-heap, pois o 6é menor do que o seu pai: 7.
Array: 7, 12, 6
![Page 10: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/10.jpg)
10
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Corrigindo...
6
12 7
Array: 6, 12, 7
![Page 11: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/11.jpg)
11
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 10...
6
12 7
10 Violou a propriedade da min-heap, poiso 10 é menor que o seu pai: 12.
Array: 6, 12, 7, 10
![Page 12: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/12.jpg)
12
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Corrigindo...
6
10 7
12
Array: 6, 10, 7, 12
![Page 13: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/13.jpg)
13
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 8...
6
10 7
12 8Violou a propriedade da min-heap, poiso 8 é menor que o seu pai: 10.
Array: 6, 10, 7, 12, 8
![Page 14: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/14.jpg)
14
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Corrigindo...
6
8 7
12 10
Array: 6, 8, 7, 12, 10
![Page 15: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/15.jpg)
15
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20• Inserindo o 20...
6
8 7
12 10 20
Array: 6, 8, 7, 12, 10, 20
![Page 16: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/16.jpg)
16
Binary Heap• Exemplo: 12, 7, 6, 10, 8, 20
6
8 7
12 10 20
Array: 6, 8, 7, 12, 10, 20
![Page 17: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/17.jpg)
17
Binary Heap• Para remover, remove-se o elemento que está no topo da heap, ou
seja, no início do array.• Copia-se o elemento do final para o início do array.• Leva-se o elemento que foi colocado no topo da heap para a sua
respectiva posição de acordo com a sua prioridade.
![Page 18: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/18.jpg)
18
Binary Heap• Removendo o 6...
6
8 7
12 10 20
Array: 6, 8, 7, 12, 10, 20
![Page 19: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/19.jpg)
19
Binary Heap• Removendo o 6...
8 7
12 10 20 O 20 é o último elemento do array.
Array: 8, 7, 12, 10, 20
![Page 20: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/20.jpg)
20
Binary Heap• Removendo o 6...
8 7
12 10
20
Array: 20, 8, 7, 12, 10
![Page 21: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/21.jpg)
21
Binary Heap• Removendo o 6...
8 7
12 10
20Troca o 20 com o 7 para refazer a heap.
![Page 22: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/22.jpg)
22
Binary Heap• Heap refeita!
8 20
12 10
7
Array: 7, 8, 20, 12, 10
![Page 23: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/23.jpg)
23
Binary Heap - Custo• A inserção e remoção é O(logN).• Tanto na remoção como na inserção, precisa-se verificar e corrigir (se
necessário) violações das propriedades da heap.
![Page 24: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/24.jpg)
24
Binary Heap• Simulação da Binary Heap:• http://visualgo.net/heap.html (max heap)• https://www.cs.usfca.edu/~galles/visualization/Heap.html (min heap)
![Page 25: Binary heap](https://reader035.vdocuments.site/reader035/viewer/2022081414/587e4f781a28abeb1a8b5c07/html5/thumbnails/25.jpg)
25
Contato
[email protected]/mcastrosouza
www.geeksbr.comhttp://youtube.com/c/marcoscastrosouza
http://marcoscastro.mehttps://github.com/marcoscastro