SlideShare a Scribd company logo
1 of 13
Download to read offline
Estruturas de Dados
´
´
Inserindo Nós
nó pai
nulo?
nó raiz
nulo?
nó raiz = novo nó
criar novo nó
fim
incrementa tamanho
da árvore
valor >
valor do
nó pai?
nó pai = nó raiz
sim
não
início
não
sim
direita
do nó pai
nula?
direita do nó pai =
novo nó
esquerda
do nó pai
nula?
esquerda do nó pai =
novo nó
sim
não
recursão passando valor
e direita do nó pai
recursão passando valor
e esquerda do nó pai
sim sim
não não
insert (value, parent) {
const inserted = new Node(value)
if (parent == null) {
if (this.root == null){
this.root = inserted
this.length++
return
} else {
parent = this.root
}
}
// Continua...
}
JavaScript
Inserindo Nós
// Continuação:
if (value > parent.value) {
if (parent.right == null) {
parent.right = inserted
this.length++
} else {
this.insert(value, parent.right)
}
} else if (value < parent.value) {
if (parent.left == null) {
parent.left = inserted
this.length++
} else {
this.insert(value, parent.left)
}
}
JavaScript
Inserindo Nós
visitapré-ordem
root
2
41
6
8
7
1. Pegue um nó
(na dúvida, a raiz)
2. Visite o nó
3. Visite a esquerda
(recursivamente)
4. Visite a direita
(recursivamente)
1
2
3
4
Resultado:
[6, 1, 2, 4, 7, 8]
5
6
preOrder (node = this.root) {
let result = []
result.push(node.value)
if (node.left) {
result.push(...this.preOrder(node.left))
}
if (node.right) {
result.push(...this.preOrder(node.right))
}
return result
}
JavaScript
Função preOrder
Leia mais sobre o Operador Spread para entender como o Array retornado pela recursão é inserido:
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Spread_operator
visitapós-ordem
root
2
41
6
8
7
1. Pegue um nó
(na dúvida, a raiz)
2. Visite a esquerda
(recursivamente)
3. Visite a direita
(recursivamente)
4. Visite o nó 1
2
3
Resultado:
[1, 2, 4, 7, 8, 6]
4
5
6
postOrder (node = this.root) {
let result = []
if (node.left) {
result.push(...this.postOrder(node.left))
}
if (node.right) {
result.push(...this.postOrder(node.right))
}
result.push(node.value)
return result
}
JavaScript
Função postOrder
visitaem-ordem
root
2
41
6
8
7
1. Pegue um nó
(na dúvida, a raiz)
2. Visite a esquerda
(recursivamente)
3. Visite o nó
4. Visite a direita
(recursivamente) 1
2
3
4
5
6
Resultado:
[1, 2, 4, 6, 7, 8]
inOrder (node = this.root) {
let result = []
if (node.left) {
result.push(...this.inOrder(node.left))
}
result.push(node.value)
if (node.right) {
result.push(...this.inOrder(node.right))
}
return result
}
JavaScript
Função inOrder
buscandonós
root
2
41
6
8
7
1. Pegue um nó alvo
(na dúvida, a raiz)
2. Se o valor procurado é maior
que o do nó, visite a direita
3. Se o valor procurado é menor
que o do nó, visite a esquerda
4. Retorne true
quando achar o valor
5. Retorne false
quando não tiver mais
para onde ir
Exemplo: 4
Buscando Nós
fim
valor >
valor do
nó?
não
início
recursão passando o
valor e a direita do nó
recebe valor
procurado
recebe nó alvo
(ou raiz)
valor <
valor do
nó?sim
direita
do nó
nula?
sim
retorne false
retorne o resultado
da recursão
não
não
recursão passando o
valor e a esquerda do nó
esquerda
do nó
nula?sim
retorne false
retorne o resultado
da recursão
sim
valor =
valor do
nó?
retorne true
retorne false
sim
não
não
Estruturas de Dados - Árvores Binárias (Binary Tree) - Parte 2

More Related Content

Recently uploaded

GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.doc
GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.docGUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.doc
GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.docPauloHenriqueGarciaM
 
Slides 9º ano 2024.pptx- Geografia - exercicios
Slides 9º ano 2024.pptx- Geografia - exerciciosSlides 9º ano 2024.pptx- Geografia - exercicios
Slides 9º ano 2024.pptx- Geografia - exerciciosGentil Eronides
 
Sopa de letras | Dia da Europa 2024 (nível 1)
Sopa de letras | Dia da Europa 2024 (nível 1)Sopa de letras | Dia da Europa 2024 (nível 1)
Sopa de letras | Dia da Europa 2024 (nível 1)Centro Jacques Delors
 
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdf
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdfApresentação ISBET Jovem Aprendiz e Estágio 2023.pdf
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdfcomercial400681
 
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...azulassessoria9
 
Os editoriais, reportagens e entrevistas.pptx
Os editoriais, reportagens e entrevistas.pptxOs editoriais, reportagens e entrevistas.pptx
Os editoriais, reportagens e entrevistas.pptxTailsonSantos1
 
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024azulassessoria9
 
3 2 - termos-integrantes-da-oracao-.pptx
3 2 - termos-integrantes-da-oracao-.pptx3 2 - termos-integrantes-da-oracao-.pptx
3 2 - termos-integrantes-da-oracao-.pptxMarlene Cunhada
 
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...marcelafinkler
 
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024azulassessoria9
 
LENDA DA MANDIOCA - leitura e interpretação
LENDA DA MANDIOCA - leitura e interpretaçãoLENDA DA MANDIOCA - leitura e interpretação
LENDA DA MANDIOCA - leitura e interpretaçãoLidianePaulaValezi
 
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...PatriciaCaetano18
 
apostila filosofia 1 ano 1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...
apostila filosofia 1 ano  1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...apostila filosofia 1 ano  1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...
apostila filosofia 1 ano 1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...SileideDaSilvaNascim
 
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...AnaAugustaLagesZuqui
 
Caderno de exercícios Revisão para o ENEM (1).pdf
Caderno de exercícios Revisão para o ENEM (1).pdfCaderno de exercícios Revisão para o ENEM (1).pdf
Caderno de exercícios Revisão para o ENEM (1).pdfJuliana Barbosa
 
AULÃO de Língua Portuguesa para o Saepe 2022
AULÃO de Língua Portuguesa para o Saepe 2022AULÃO de Língua Portuguesa para o Saepe 2022
AULÃO de Língua Portuguesa para o Saepe 2022LeandroSilva126216
 
aprendizagem significatica, teórico David Ausubel
aprendizagem significatica, teórico David Ausubelaprendizagem significatica, teórico David Ausubel
aprendizagem significatica, teórico David Ausubeladrianaguedesbatista
 
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdf
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdfMESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdf
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdfprofesfrancleite
 
O que é arte. Definição de arte. História da arte.
O que é arte. Definição de arte. História da arte.O que é arte. Definição de arte. História da arte.
O que é arte. Definição de arte. História da arte.denisecompasso2
 
Falando de Física Quântica apresentação introd
Falando de Física Quântica apresentação introdFalando de Física Quântica apresentação introd
Falando de Física Quântica apresentação introdLeonardoDeOliveiraLu2
 

Recently uploaded (20)

GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.doc
GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.docGUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.doc
GUIA DE APRENDIZAGEM 2024 9º A - História 1 BI.doc
 
Slides 9º ano 2024.pptx- Geografia - exercicios
Slides 9º ano 2024.pptx- Geografia - exerciciosSlides 9º ano 2024.pptx- Geografia - exercicios
Slides 9º ano 2024.pptx- Geografia - exercicios
 
Sopa de letras | Dia da Europa 2024 (nível 1)
Sopa de letras | Dia da Europa 2024 (nível 1)Sopa de letras | Dia da Europa 2024 (nível 1)
Sopa de letras | Dia da Europa 2024 (nível 1)
 
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdf
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdfApresentação ISBET Jovem Aprendiz e Estágio 2023.pdf
Apresentação ISBET Jovem Aprendiz e Estágio 2023.pdf
 
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...
Considerando as pesquisas de Gallahue, Ozmun e Goodway (2013) os bebês até an...
 
Os editoriais, reportagens e entrevistas.pptx
Os editoriais, reportagens e entrevistas.pptxOs editoriais, reportagens e entrevistas.pptx
Os editoriais, reportagens e entrevistas.pptx
 
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 3 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
 
3 2 - termos-integrantes-da-oracao-.pptx
3 2 - termos-integrantes-da-oracao-.pptx3 2 - termos-integrantes-da-oracao-.pptx
3 2 - termos-integrantes-da-oracao-.pptx
 
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...
Introdução às Funções 9º ano: Diagrama de flexas, Valor numérico de uma funçã...
 
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
ATIVIDADE 2 - DESENVOLVIMENTO E APRENDIZAGEM MOTORA - 52_2024
 
LENDA DA MANDIOCA - leitura e interpretação
LENDA DA MANDIOCA - leitura e interpretaçãoLENDA DA MANDIOCA - leitura e interpretação
LENDA DA MANDIOCA - leitura e interpretação
 
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...
A EDUCAÇÃO FÍSICA NO NOVO ENSINO MÉDIO: IMPLICAÇÕES E TENDÊNCIAS PROMOVIDAS P...
 
apostila filosofia 1 ano 1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...
apostila filosofia 1 ano  1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...apostila filosofia 1 ano  1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...
apostila filosofia 1 ano 1s (1).pdf 1 ANO DO ENSINO MEDIO . CONCEITOSE CARAC...
 
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...
Tema de redação - As dificuldades para barrar o casamento infantil no Brasil ...
 
Caderno de exercícios Revisão para o ENEM (1).pdf
Caderno de exercícios Revisão para o ENEM (1).pdfCaderno de exercícios Revisão para o ENEM (1).pdf
Caderno de exercícios Revisão para o ENEM (1).pdf
 
AULÃO de Língua Portuguesa para o Saepe 2022
AULÃO de Língua Portuguesa para o Saepe 2022AULÃO de Língua Portuguesa para o Saepe 2022
AULÃO de Língua Portuguesa para o Saepe 2022
 
aprendizagem significatica, teórico David Ausubel
aprendizagem significatica, teórico David Ausubelaprendizagem significatica, teórico David Ausubel
aprendizagem significatica, teórico David Ausubel
 
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdf
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdfMESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdf
MESTRES DA CULTURA DE ASSARÉ Prof. Francisco Leite.pdf
 
O que é arte. Definição de arte. História da arte.
O que é arte. Definição de arte. História da arte.O que é arte. Definição de arte. História da arte.
O que é arte. Definição de arte. História da arte.
 
Falando de Física Quântica apresentação introd
Falando de Física Quântica apresentação introdFalando de Física Quântica apresentação introd
Falando de Física Quântica apresentação introd
 

Featured

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
 

Featured (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...
 

Estruturas de Dados - Árvores Binárias (Binary Tree) - Parte 2

  • 2. Inserindo Nós nó pai nulo? nó raiz nulo? nó raiz = novo nó criar novo nó fim incrementa tamanho da árvore valor > valor do nó pai? nó pai = nó raiz sim não início não sim direita do nó pai nula? direita do nó pai = novo nó esquerda do nó pai nula? esquerda do nó pai = novo nó sim não recursão passando valor e direita do nó pai recursão passando valor e esquerda do nó pai sim sim não não
  • 3. insert (value, parent) { const inserted = new Node(value) if (parent == null) { if (this.root == null){ this.root = inserted this.length++ return } else { parent = this.root } } // Continua... } JavaScript Inserindo Nós
  • 4. // Continuação: if (value > parent.value) { if (parent.right == null) { parent.right = inserted this.length++ } else { this.insert(value, parent.right) } } else if (value < parent.value) { if (parent.left == null) { parent.left = inserted this.length++ } else { this.insert(value, parent.left) } } JavaScript Inserindo Nós
  • 5. visitapré-ordem root 2 41 6 8 7 1. Pegue um nó (na dúvida, a raiz) 2. Visite o nó 3. Visite a esquerda (recursivamente) 4. Visite a direita (recursivamente) 1 2 3 4 Resultado: [6, 1, 2, 4, 7, 8] 5 6
  • 6. preOrder (node = this.root) { let result = [] result.push(node.value) if (node.left) { result.push(...this.preOrder(node.left)) } if (node.right) { result.push(...this.preOrder(node.right)) } return result } JavaScript Função preOrder Leia mais sobre o Operador Spread para entender como o Array retornado pela recursão é inserido: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Spread_operator
  • 7. visitapós-ordem root 2 41 6 8 7 1. Pegue um nó (na dúvida, a raiz) 2. Visite a esquerda (recursivamente) 3. Visite a direita (recursivamente) 4. Visite o nó 1 2 3 Resultado: [1, 2, 4, 7, 8, 6] 4 5 6
  • 8. postOrder (node = this.root) { let result = [] if (node.left) { result.push(...this.postOrder(node.left)) } if (node.right) { result.push(...this.postOrder(node.right)) } result.push(node.value) return result } JavaScript Função postOrder
  • 9. visitaem-ordem root 2 41 6 8 7 1. Pegue um nó (na dúvida, a raiz) 2. Visite a esquerda (recursivamente) 3. Visite o nó 4. Visite a direita (recursivamente) 1 2 3 4 5 6 Resultado: [1, 2, 4, 6, 7, 8]
  • 10. inOrder (node = this.root) { let result = [] if (node.left) { result.push(...this.inOrder(node.left)) } result.push(node.value) if (node.right) { result.push(...this.inOrder(node.right)) } return result } JavaScript Função inOrder
  • 11. buscandonós root 2 41 6 8 7 1. Pegue um nó alvo (na dúvida, a raiz) 2. Se o valor procurado é maior que o do nó, visite a direita 3. Se o valor procurado é menor que o do nó, visite a esquerda 4. Retorne true quando achar o valor 5. Retorne false quando não tiver mais para onde ir Exemplo: 4
  • 12. Buscando Nós fim valor > valor do nó? não início recursão passando o valor e a direita do nó recebe valor procurado recebe nó alvo (ou raiz) valor < valor do nó?sim direita do nó nula? sim retorne false retorne o resultado da recursão não não recursão passando o valor e a esquerda do nó esquerda do nó nula?sim retorne false retorne o resultado da recursão sim valor = valor do nó? retorne true retorne false sim não não