SlideShare uma empresa Scribd logo
1 de 8
Diêgo João Costa Santiago
Índice
1. Escreva um algoritmo que inverta uma lista dada como entrada.
2. Escreva um algoritmo que verifique se uma árvore binária é AVL.
Obs1.: Cada elemento só pode ser visitado apenas uma vez;
Obs2.: Cada nó aponta apenas para seus filhos.
3. Escreva um algoritmo que transforme uma árvore binária de busca em uma lista
contendo as chaves da árvore em ordem crescente.
4. Escreva um algoritmo que concatene duas árvores AVL em uma árvore AVL
válida. O seu algoritmo deve executar em tempo O(h), onde h é a altura máxima
entre as duas árvores.
Definição: A concatenação intercala duas estruturas de dados onde todas as chaves de uma
são menores que todas as chaves da outra
5. Escreva um algoritmo que construa um heap que contém todos os elementos de
dois heaps de tamanho n. Os heaps dados são representados através de árvores
ligadas em vez de vetores (arrays), ou seja, cada nó tem ponteiro para os seus
dois filhos, pai e dois irmãos. O algoritmo deve executar em tempo O(log n) no
pior caso.
1. Escreva um algoritmo que inverta uma lista dada como
entrada.
ALGORITMO INVERTER-LISTA
ENTRADA elista que é uma PILHA
SAÍDA slista que é uma PILHA
TOPO(slista) = NIL //PILHA DE SAÍDA VAZIA
ENQUANTO TOPO(elista) ≠ NIL //ENQUANTO PILHA NÃO ESTÁ VAZIA
FAÇA chave = REMOVER(elista) //REMOVE O TOPO DA PILHA DE ENTRADA
INSERIR(chave, slista) //INSERE NO TOPO DA PILHA DE SAÍDA
FIM ENQUANTO
FIM ALGORITMO
1. Escreva um algoritmo que inverta uma lista dada como
entrada.
ALGORITMO INVERTER-LISTA
ENTRADA elista
SAÍDA slista
TOPO(pilha) = NIL //DEFINE UMA PILHA AUXILIAR VAZIA
x = CABEÇA(elista)
ENQUANTO x ≠ NIL
FAÇA INSERIR(CHAVE(x), pilha) //INSERE NO TOPO DE UMA PILHA AUXILIAR
x = PROXIMO(x)
FIM PARA
CABEÇA(slista) = TOPO(pilha)
//INSERE TODOS AS CHAVES DA PILHA AUXILIAR NA LISTA DE SAÍDA
FIM ALGORITMO
2. Escreva um algoritmo que verifique se uma árvore binária é
AVL.
ALGORITMO EH-AVL
ENTRADA nó = raiz da árvore
SAÍDA (alt = altura da árvore, ehAVL = se árvore é AVL)
SE nó = NIL
ENTÃO (alt, ehAVL) = (0, TRUE) //CASO BASE
SENÃO (altdir, ehAVLdir) = EH-AVL(ESQUERDA(nó))
//RECURSÃO NA SUB-ÁRVORE ESQUERDA
(altesq, ehAVLesq) = EH-AVL(DIREITA(nó))
//RECURSÃO NA SUB-ÁRVORE DIREITA
alt = máximo(altdir, altesq) + 1
ehAVL = ehAVLdir E ehAVLesq E (absoluto(altdir - altesq) ≤ 1)
FIM SE
FIM ALGORITMO
3. Escreva um algoritmo que transforme uma árvore binária de
busca em uma lista contendo as chaves da árvore em ordem
crescente.
ALGORITMO BUSCA-IN-ORDEM
ENTRADAS nó = raiz da árvore, lista vazia que é uma FILA
SAÍDA lista
SE nó ≠ NIL
ENTÃO lista = BUSCA-IN-ORDEM(ESQUERDA(nó), lista)
INSERIR(CHAVE(nó), lista) //INSERE A CHAVE NA (NO FINAL DA) FILA
lista = BUSCA-IN-ORDEM(DIREITA(nó), lista)
FIM SE
FIM ALGORITMO
4. Escreva um algoritmo que concatene duas árvores AVL em
uma árvore AVL válida.
ALGORITMO CONCATENAR-AVL
ENTRADA arv1, arv2
SAÍDA arv
SE CHAVE(RAIZ(arv1)) > CHAVE(RAIZ(arv2)) ENTÃO troca(arv1,arv2) FIM SE
nó = RAIZ(arv2)
ENQUANTO ESQUERDA(nó) ≠ NIL FAÇA nó = ESQUERDA(nó) FIM ENQUANTO
REMOVER(nó, arv2)
RAIZ(arv) = nó
ESQUERDA(nó) = RAIZ(arv1)
DIREITA(nó) = RAIZ(arv2)
BALANCE(nó) = ALTURA(ESQUERDA(nó)) – ALTURA(DIREITA(nó))
ENQUANTO absoluto(BALANCE(nó)) ≤ 1 FAÇA ROTACIONAR(nó) FIM ENQUANTO
FIM ALGORITMO
5. Escreva um algoritmo que construa um heap que contém
todos os elementos de dois heaps de tamanho n.
ALGORITMO UNIR-HEAPS
ENTRADA heap1, heap2
SAÍDA heap
nó = REMOVER(heap1)
RAIZ(heap) = nó
ESQUERDA(nó) = RAIZ(heap1), PAI(RAIZ(heap1)) = nó
DIREITA(nó) = RAIZ(heap2), PAI(RAIZ(heap2)) = nó
HEAPFY(nó)
x = RAIZ(heap1), y = RAIZ(heap2)
ENQUANTO x ≠ NIL E y ≠ NIL
FAÇA PROXIMO(x) = y, ANTERIOR(y) = x, x = DIREITA(x), y = ESQUERDA(y)
FIM ENQUANTO
FIM ALGORITMO

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Algoritmos para estruturas de dados

  • 2. Índice 1. Escreva um algoritmo que inverta uma lista dada como entrada. 2. Escreva um algoritmo que verifique se uma árvore binária é AVL. Obs1.: Cada elemento só pode ser visitado apenas uma vez; Obs2.: Cada nó aponta apenas para seus filhos. 3. Escreva um algoritmo que transforme uma árvore binária de busca em uma lista contendo as chaves da árvore em ordem crescente. 4. Escreva um algoritmo que concatene duas árvores AVL em uma árvore AVL válida. O seu algoritmo deve executar em tempo O(h), onde h é a altura máxima entre as duas árvores. Definição: A concatenação intercala duas estruturas de dados onde todas as chaves de uma são menores que todas as chaves da outra 5. Escreva um algoritmo que construa um heap que contém todos os elementos de dois heaps de tamanho n. Os heaps dados são representados através de árvores ligadas em vez de vetores (arrays), ou seja, cada nó tem ponteiro para os seus dois filhos, pai e dois irmãos. O algoritmo deve executar em tempo O(log n) no pior caso.
  • 3. 1. Escreva um algoritmo que inverta uma lista dada como entrada. ALGORITMO INVERTER-LISTA ENTRADA elista que é uma PILHA SAÍDA slista que é uma PILHA TOPO(slista) = NIL //PILHA DE SAÍDA VAZIA ENQUANTO TOPO(elista) ≠ NIL //ENQUANTO PILHA NÃO ESTÁ VAZIA FAÇA chave = REMOVER(elista) //REMOVE O TOPO DA PILHA DE ENTRADA INSERIR(chave, slista) //INSERE NO TOPO DA PILHA DE SAÍDA FIM ENQUANTO FIM ALGORITMO
  • 4. 1. Escreva um algoritmo que inverta uma lista dada como entrada. ALGORITMO INVERTER-LISTA ENTRADA elista SAÍDA slista TOPO(pilha) = NIL //DEFINE UMA PILHA AUXILIAR VAZIA x = CABEÇA(elista) ENQUANTO x ≠ NIL FAÇA INSERIR(CHAVE(x), pilha) //INSERE NO TOPO DE UMA PILHA AUXILIAR x = PROXIMO(x) FIM PARA CABEÇA(slista) = TOPO(pilha) //INSERE TODOS AS CHAVES DA PILHA AUXILIAR NA LISTA DE SAÍDA FIM ALGORITMO
  • 5. 2. Escreva um algoritmo que verifique se uma árvore binária é AVL. ALGORITMO EH-AVL ENTRADA nó = raiz da árvore SAÍDA (alt = altura da árvore, ehAVL = se árvore é AVL) SE nó = NIL ENTÃO (alt, ehAVL) = (0, TRUE) //CASO BASE SENÃO (altdir, ehAVLdir) = EH-AVL(ESQUERDA(nó)) //RECURSÃO NA SUB-ÁRVORE ESQUERDA (altesq, ehAVLesq) = EH-AVL(DIREITA(nó)) //RECURSÃO NA SUB-ÁRVORE DIREITA alt = máximo(altdir, altesq) + 1 ehAVL = ehAVLdir E ehAVLesq E (absoluto(altdir - altesq) ≤ 1) FIM SE FIM ALGORITMO
  • 6. 3. Escreva um algoritmo que transforme uma árvore binária de busca em uma lista contendo as chaves da árvore em ordem crescente. ALGORITMO BUSCA-IN-ORDEM ENTRADAS nó = raiz da árvore, lista vazia que é uma FILA SAÍDA lista SE nó ≠ NIL ENTÃO lista = BUSCA-IN-ORDEM(ESQUERDA(nó), lista) INSERIR(CHAVE(nó), lista) //INSERE A CHAVE NA (NO FINAL DA) FILA lista = BUSCA-IN-ORDEM(DIREITA(nó), lista) FIM SE FIM ALGORITMO
  • 7. 4. Escreva um algoritmo que concatene duas árvores AVL em uma árvore AVL válida. ALGORITMO CONCATENAR-AVL ENTRADA arv1, arv2 SAÍDA arv SE CHAVE(RAIZ(arv1)) > CHAVE(RAIZ(arv2)) ENTÃO troca(arv1,arv2) FIM SE nó = RAIZ(arv2) ENQUANTO ESQUERDA(nó) ≠ NIL FAÇA nó = ESQUERDA(nó) FIM ENQUANTO REMOVER(nó, arv2) RAIZ(arv) = nó ESQUERDA(nó) = RAIZ(arv1) DIREITA(nó) = RAIZ(arv2) BALANCE(nó) = ALTURA(ESQUERDA(nó)) – ALTURA(DIREITA(nó)) ENQUANTO absoluto(BALANCE(nó)) ≤ 1 FAÇA ROTACIONAR(nó) FIM ENQUANTO FIM ALGORITMO
  • 8. 5. Escreva um algoritmo que construa um heap que contém todos os elementos de dois heaps de tamanho n. ALGORITMO UNIR-HEAPS ENTRADA heap1, heap2 SAÍDA heap nó = REMOVER(heap1) RAIZ(heap) = nó ESQUERDA(nó) = RAIZ(heap1), PAI(RAIZ(heap1)) = nó DIREITA(nó) = RAIZ(heap2), PAI(RAIZ(heap2)) = nó HEAPFY(nó) x = RAIZ(heap1), y = RAIZ(heap2) ENQUANTO x ≠ NIL E y ≠ NIL FAÇA PROXIMO(x) = y, ANTERIOR(y) = x, x = DIREITA(x), y = ESQUERDA(y) FIM ENQUANTO FIM ALGORITMO