Red-black tree properties

Slides:



Advertisements
Similar presentations
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Advertisements

Definitions and Bottom-Up Insertion
Introduction to Algorithms Red-Black Trees
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
Red Black Trees Top-Down Insertion. Review of Bottom-Up Insertion In B-Up insertion, “ordinary” BST insertion was used, followed by correction of the.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red Black Trees Top-Down Deletion. Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
AA Trees.
File Organization and Processing Week 3
SNS COLLEGE OF TECHNOLOGY (Autonomous ) COIMBATORE-35
G64ADS Advanced Data Structures
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Lecture 17 Red-Black Trees
Red-Black Trees.
AVL Tree.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Motivations
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Advanced Associative Structures
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Red-Black Trees Bottom-Up Deletion.
AVL-Trees.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red Black Trees Top-Down Deletion.
CO4301 – Advanced Games Development Week 5 Walkthrough of Red-Black Tree Insertion Gareth Bellaby.
Presentation transcript:

Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two consecutive red nodes -- i.e. the children of a red node must be black Every path from a node, x, to a descendant leaf contains the same number of black nodes -- the “black height” of node x. In these slides we don’t force the root to be black

Insertion: The First Step Given a new node with a key value, the first step is to determine where to insert the new node. Since a Red-Black Tree is a Binary Search Tree (BST), use the BST Insert procedure to: determine the location where the new node’s key value belongs in the Red-Black Tree. insert the new node at that location. Color the new node red. Use the cases described in the following slides to restore any Red-Black Tree properties that are violated by the new node.

Red-black tree insertion: Case 0: New node’s parent is black. newly inserted node No property is violated, so there’s nothing to do.

(x could also be a right child) Red-black tree insertion: Case 1: New node’s parent and uncle are both red. This node is now red; possibly propagating a r-b violation upward that must also be fixed… newly inserted node x x (x could also be a right child) compare w/ Figure 13.5 color flip: x’s parent, uncle, and grandparent all change color

Red-black tree insertion: Case 3: New red node’s parent is red and uncle is black ; x is a left child & p(x) is a left child [a left/left situation] a newly red node (caused by either direct insertion or violation propagation from below) b See Fig. 13.6 a b x C A B B x = A Change color : p(x) to black - p(p(x)) to red Rotate right: grab p(x) and shake; p(p(x)) drops down to the right; right subtree of p(x) slides down to become a left subtree C

newly red node (caused by propagation from below) Red-black tree insertion: Case 2: New red node’s parent is red and uncle is black; x is a right child & p(x) is a left child [a right/left situation] Reduce to Case 3! newly red node (caused by propagation from below) b b a x a x D D C A A B B C Rotate left: grab x and shake; p(x) drops down to the left; left subtree of x slides down to become a right subtree Note that this rotation grabs tree at x, whereas Case 3 grabs tree up one level at p(x).

Red-black tree insertion: Case 2: continued was x; now is p(a ) b now acts as x a b D a C A B A B C D Now apply Case 3 at a

Other Cases for red x, red parent, black uncle If x is right child and p(x) is right child Change colors: p(x) to black; p(p(x)) to red Rotate left (a right/right form of Case 3) If x is left child and p(x) is right child Rotate right Apply case above (a left/right form of Case 2) b a C B x A b a C B x A

Example: Inserting the sequence 1 9 2 8 3 7 4 6 No property is violated, so there’s nothing to do.

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Insert 9 and flip color of root

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Insert 2 as left child of 9. This causes a red/red violation! Plan: Apply a left/right Case 2: - Rotate right (grab 2) - Apply right/right form of Case 3

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Apply a left/right Case 2: - Rotate right (grab 2) - Now we have a right/right form of Case 3

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Applying right/right form of Case 3 - change color of 1 and 2 - rotate left (grab 2) Red-black property restored!

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Red-black property violated by inserting 8. An application of case 1 (color change) will suffice here because uncle is red.

Example: Inserting the sequence 1 9 2 8 3 7 4 6 A color flip restores the red-black property.

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 3 violates the red-black property

Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is a left/left form of case 3, so change colors and rotate right to restore (grab 8).

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 7 causes a red/red violation; both 7’s parent and uncle are red, so a color flip suffices …

Example: Inserting the sequence 1 9 2 8 3 7 4 6 The color flip performed: 3, 9, and 8 all flip. This forms a red/red violation above…

Example: Inserting the sequence 1 9 2 8 3 7 4 6 To fix the red/red violation above, recolor the root to black.

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 4 causes a red/red violation; this is a left/right form of Case 2

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Apply left/right form of Case 2; perform a right rotation (grab 4) to produce a right/right form of Case 3

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Apply right/right form of Case 3; change colors and rotate left (grab 4)

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 6 causes a red/red violation … which can be fixed by a color flip (it’s a case 1 violation)

Example: Inserting the sequence 1 9 2 8 3 7 4 6 But flipping the colors of 6’s parent, uncle, and grandparent causes another tree violation at 4 ...

Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is left/right Case 2: first grab at 4 and rotate right; this reduces the situation to right/right Case 3

Example: Inserting the sequence 1 9 2 8 3 7 4 6 Now apply right/right Case 3: change colors and rotate left (grab 4)

Inserting 1 9 2 8 3 7 4 6 into a binary search tree without balancing!