SlideShare uma empresa Scribd logo
1 de 20
Alberi binari e ricorsione
un albero binario: radice figlio sinistro figlio destro ogni nodo ha al più 2 figli ogni figlio è destro o sinistro foglie nodo interno
0 00 01 111 101 cammini = sequenze di nodi = sequenze di 0 e 1 profondità di un nodo altezza dell’albero=prof. max delle foglie X   0 X  1 X  2
albero binario completo ogni livello è completo, se h= altezza l’albero contiene 2 h+1  -1  nodi
[object Object],[object Object],[object Object],[object Object],nodo sinistro destro
i s o s f n i ,[object Object],[object Object],[object Object],[object Object],depth-first traversal
r p s s e f i ,[object Object],[object Object],[object Object],[object Object],o
t o s i s p o ,[object Object],[object Object],[object Object],[object Object],s f
altezza di un albero = profondità massima dei suoi nodi = distanza massima tra 2 nodi dell’albero  altezza 0 altezza 1 albero vuoto? per convenzione -1
un  cammino  di un albero = sequenza di 0 e 1 0=sinistra 1= destra array int C[]  e il valore -1 indica la fine della sequenza: r x cammino per x:  C=[010-1] cammino di r = [-1]
inserimento di un nuovo nodo in un albero: il nuovo va inserito come figlio di un nodo già esistente diventa quindi una foglia o l’unico nodo se l’albero era vuoto inseriamo sempre nel sotto albero che contiene meno nodi L R cioè conto i nodi di L e di R e inserisco il nodo nel + piccolo dei 2
dove metteremmo n? n ha meno nodi
è simile ad aggiungere un nuovo nodo in fondo ad una lista
binary  search  trees (BST): i h a g f r t s ogni nodo è maggiore dei nodo nel sottoalbero sinistro e minore di quelli del destro
i h a g f r t s cerchiamo h: h<i andiamo a sinistra h>f destra h>g destra trovato !!! se h non ci fosse 0
n i a g f r t s se h non ci fosse dove andrebbe inserito? h
n i a g f r t s h percorso infisso a f g i n r s t sono in ordine !!
inseriamo  e s e m p i o  nell’albero vuoto root=0 root e e s root e s e root e e s m root
p m e s e o i p m e s e i p m e s e p o i
a c b f e x d come un’espressione prefissa: a(b(f(_,_),e(x(_,_),_)),c(_,d(_,_))) useremo un percorso prefisso

Mais conteúdo relacionado

Destaque

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
ThinkNow
 
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
Kurio // The Social Media Age(ncy)
 

Destaque (20)

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...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Alberi Binari

  • 1. Alberi binari e ricorsione
  • 2. un albero binario: radice figlio sinistro figlio destro ogni nodo ha al più 2 figli ogni figlio è destro o sinistro foglie nodo interno
  • 3. 0 00 01 111 101 cammini = sequenze di nodi = sequenze di 0 e 1 profondità di un nodo altezza dell’albero=prof. max delle foglie X  0 X  1 X  2
  • 4. albero binario completo ogni livello è completo, se h= altezza l’albero contiene 2 h+1 -1 nodi
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. altezza di un albero = profondità massima dei suoi nodi = distanza massima tra 2 nodi dell’albero altezza 0 altezza 1 albero vuoto? per convenzione -1
  • 10. un cammino di un albero = sequenza di 0 e 1 0=sinistra 1= destra array int C[] e il valore -1 indica la fine della sequenza: r x cammino per x: C=[010-1] cammino di r = [-1]
  • 11. inserimento di un nuovo nodo in un albero: il nuovo va inserito come figlio di un nodo già esistente diventa quindi una foglia o l’unico nodo se l’albero era vuoto inseriamo sempre nel sotto albero che contiene meno nodi L R cioè conto i nodi di L e di R e inserisco il nodo nel + piccolo dei 2
  • 12. dove metteremmo n? n ha meno nodi
  • 13. è simile ad aggiungere un nuovo nodo in fondo ad una lista
  • 14. binary search trees (BST): i h a g f r t s ogni nodo è maggiore dei nodo nel sottoalbero sinistro e minore di quelli del destro
  • 15. i h a g f r t s cerchiamo h: h<i andiamo a sinistra h>f destra h>g destra trovato !!! se h non ci fosse 0
  • 16. n i a g f r t s se h non ci fosse dove andrebbe inserito? h
  • 17. n i a g f r t s h percorso infisso a f g i n r s t sono in ordine !!
  • 18. inseriamo e s e m p i o nell’albero vuoto root=0 root e e s root e s e root e e s m root
  • 19. p m e s e o i p m e s e i p m e s e p o i
  • 20. a c b f e x d come un’espressione prefissa: a(b(f(_,_),e(x(_,_),_)),c(_,d(_,_))) useremo un percorso prefisso