SlideShare uma empresa Scribd logo
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
´Arvore rubro-negra
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral
Instituto Federal de Ciˆencia e Tecnologia do Cear´a
25 de Maio de 2018
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Introduc¸˜ao
Uma ´arvore rubro-negra, ou vermelho-preta, ´e uma ´arvore bin´aria de pesquisa com
um bit extra de armazenamento por n´o indicando sua cor: vermelho ou preto.
Restringindo o modo como os n´os podem ser coloridos em qualquer caminho desde
a raiz at´e uma folha, as ´arvores rubro-negra asseguram que nenhum desses caminhos
ser´a maior que duas vezes o comprimento de qualquer outro, de maneira que a ´arvore ´e
aproximadamente balanceada.
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Propriedades
Uma ´arvore de pesquisa bin´aria ´e uma ´arvore rubro-negra se satisfaz as seguintes
propriedades:
1 Todo n´o ´e vermelho ou preto;
2 A raiz ´e preta;
3 Se um n´o ´e vermelho, ent˜ao ambos os seus filhos s˜ao pretos;
4 Para cada n´o, todos os caminhos desde um n´o at´e as folhas descendentes cont´em o
mesmo n´umero de n´os pretos;
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Operac¸˜oes
As operac¸˜oes de inserc¸˜ao e remoc¸˜ao quando executadas sobre uma ´arvore rubro-
negra com n chaves demoram tempo O(log n). Considerando-se que elas modificam a
´arvore o resultado pode violar as propriedades e, para restabelecˆe-las, mudamos as cores
de alguns n´os ou efetuamos rotac¸˜oes.
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Rotac¸˜ao
Rota¸c˜ao a esquerda
Consideremos um n´o ”x”, se seu filho da direita ”y” n˜ao for nulo procedemos da
seguinte forma:
• ”y” torna-se a nova raiz da sub´arvore considerada;
• ”x” torna-se filho esquerdo de y;
• o filho esquerdo de ”y” torna-se o filho direito de x;
Rota¸c˜ao a direita
Analogamente, se ”y”, o filho da esquerda de ”x”, n˜ao for nulo:
• ”y” torna-se a nova raiz da sub´arvore considerada;
• ”x” torna-se filho direito de y;
• o filho direito de ”y” torna-se o filho esquerdo de x;
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Inserc¸˜ao
• Por padr˜ao, todo n´o inserido ´e vermelho;
• A inserc¸˜ao ´e realizada da mesma maneira que em ´arvores bin´arias comuns ou
´arvores AVL, em tempo proporcional a altura da ´arvore (O(log n));
• Em cada inserc¸˜ao precisamos verificar se alguma das propriedades foi violada;
• Se alguma das prorpiedades for violada s˜ao efetuadas rotac¸˜oes ou colorac¸˜oes de
maneira a tratar os casos a seguir;
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Caso 1: Tio ”s” do n´o ”x” ´e vermelho
z
y
A x
B C
s
D E
z
y
A x
B C
s
D E
Sub´arvore considerada antes e ap´os a aplicac¸˜ao da contramedida
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Contramedida para o caso 1:
• O pai ”y” e o tio ”s” tornam-se pretos e o avˆo vermelho;
• Se o avˆo for raiz permanece preto;
• Posteriormente verificamos se alguma propriedade ´e violada fazendo x = x.avo;
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Caso 2: Tio ”s” ´e preto e x ´e filho da direita.
Contramedida: Efetuamos uma rotac¸˜ao a esquerda sobre o pai de ”x”, ”y”.
z
y
A x
B C
s
D E
z
x
y
A B
C
s
D E
Sub´arvore considerada antes e ap´os a aplicac¸˜ao da contramedida
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Caso 3: Tio ”s” ´e preto e x ´e filho da esquerda.
Contramedida: Efetuamos uma rotac¸˜ao a direita sobre o pai de ”x”, ”y”.
z
s
A B
y
x
C D
E
z
s
A B
x
C y
D E
Sub´arvore considerada antes e ap´os a aplicac¸˜ao da contramedida
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Caso 4: Tio ”s” de x ´e preto e x ´e filho da direita.
Contramedida: Efetuamos uma rotac¸˜ao a esquerda sobre o avˆo de ”x”, ”z”.
Posteriormente fazemos ”z” e ”x” vermelhos e ”y” preto.
z
s
A B
y
C x
D E
y
z
s
A B
C
x
D E
Sub´arvore considerada antes e ap´os a aplicac¸˜ao da contramedida
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Rotac¸˜ao
Inserc¸˜ao
Tratando violac¸˜oes
Caso 5: Tio ”s” de x ´e preto e x ´e filho da esquerda.
Contramedida: Efetuamos uma rotac¸˜ao a direita sobre o avˆo de ”x”, ”z”.
Posteriormente fazemos ”z” e ”x” vermelhos e ”y” preto.
z
y
x
A B
C
s
D E
y
x
A B
z
C s
D E
Sub´arvore considerada antes e ap´os a aplicac¸˜ao da contramedida
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra
Introduc¸˜ao
Propriedades
Operac¸˜oes
Bibliografia
Bibliografia I
Thomas H. Cormen...[et al.]
Algoritmos: teoria e pr´atica, Edicao 2.
Elsevier,2002.
Leo Guibas e Robert Sedgewick
A dichromatic framework for balanced trees
Proceedings of the 19th Annual Symposium on Foundations of Computer Science,
IEEE Computer Society, 1978.
Dener Jamill, Raimundo Azevedo Neto, Samuel Cabral ´Arvores rubro-negra

Mais conteúdo relacionado

Destaque

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
Albert 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 Insights
Kurio // 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 2024
Search 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 summary
SpeakerHub
 
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 Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit 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 management
MindGenius
 
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
 
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...
Applitools
 
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
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Destaque (20)

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
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Arvore Rubro Negra