Red-Black Trees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or black.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
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.
Comp 122, Spring 2004 Red-Black Trees. redblack - 2 Comp 122, Spring 2004 Red-black trees: Overview  Red-black trees are a variation of binary search.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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
Red-Black Trees Comp 550.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Red-Black Trees Definitions and Bottom-Up Insertion.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
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
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
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.
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.
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
File Organization and Processing Week 3
Balanced Search Trees Modified from authors’ slides.
Red Black Trees
FLIPPED CLASSROOM ACTIVITY CONSTRUCTOR – USING EXISTING CONTENT
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Analysis of Algorithms CS 477/677
Red-black tree properties
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red Black Trees Top-Down Deletion.
Red-Black Trees CS302 Data Structures
Presentation transcript:

Red-Black Trees

Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree is balanced. Height is at most 2lg(n+1), O(lg n), where n is the number of nodes. Operations take O(lg n) time in the worst case.

Red-black Tree Binary search tree + 1 bit per node: the attribute color, which is either red or black. All other attributes of BSTs are inherited. All empty trees (leaves) are colored black. We use a single sentinel, NIL, for all the leaves of red-black tree T, with color(NIL)= black. The root’s parent (if parents are stored) is also NIL.

Red-black Tree – Example Every internal node has two children, even though nil leaves are not usually shown. 26 17 41 NIL 30 47 38 50

Red-black Properties Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes.

Height of a Red-black Tree Height of a node: Number of edges in a longest path to a leaf. Black-height of a node x, bh(x): bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x. Black-height of a red-black tree is the black-height of its root. By Property 5, black height is well defined.

Height of a Red-black Tree Example: Height of a node: h(x) = # of edges in a longest path to a leaf. Black-height of a node bh(x) = # of black nodes on path from x to leaf, not counting x. How are they related? bh(x) ≤ h(x) ≤ 2 bh(x) h=4 bh=2 26 h=3 bh=2 h=1 bh=1 17 41 h=2 bh=1 h=2 bh=1 30 47 h=1 bh=1 38 h=1 bh=1 50 NIL

Operations on RB Trees All operations can be performed in O(lg n) time. The query operations, which don’t modify the tree, are performed in exactly the same way as they are in BSTs. Insertion and Deletion are not straightforward.

Rotations Rotations are the basic tree-restructuring operation for almost all balanced search trees. Rotation takes a red-black-tree and a node, Changes pointers to change the local structure, and Won’t violate the binary-search-tree property. Left rotation and right rotation are inverses. y x    Left-Rotate(T, x) Right-Rotate(T, y)

Insertion in RB Trees Insertion must preserve all red-black properties. Should an inserted node be colored Red? Black? Basic steps: Use Tree-Insert from BST (slightly modified) to insert a node x into T: Procedure RB-Insert(x). Color the node x red. Fix the modified tree by re-coloring nodes and performing rotation(s) to preserve RB tree property: Procedure RB-Insert-Fixup.

Insertion Possibilities x is the root node, i.e., first node of red–black tree x's parent (P) is black x's parent (P) and uncle (U) are red x is added to right of left child, or x is added to left of right child (P is red and U is black) x is added to left of left child, or x is added to right of right child (P is red and U is black)

Insertion Possibilities x is the root node, i.e., first node of red–black tree C C The current node is the root of the tree. In this case, it is repainted black to satisfy property 2 (the root is black).

Insertion Possibilities x's parent (P) is black C A No RB Tree properties are invalidated. In this case, the tree is still valid and no changes are necessary.

Insertion Possibilities x's parent (P) and uncle (U) are red Insert B C C C A D A D       B B Parent and uncle are repainted black and the grandparent becomes red. However, the grandparent may now violate properties 2 (The root is black) or 4 (Both children of every red node are black) To fix this, the entire procedure is recursively performed on grandparent. In this case make the root black – now we are good.

Insertion Possibilities P is red and U is black: x is added to right of left child, or x is added to left of right child C C A D B D  A B  A left rotation on the parent switches the roles of the current node and its parent; then, the former parent node is dealt with using case 5 (next slide).

Insertion Possibilities P is red and U is black: x is added to left of left child, or x is added to right of right child C B B A C D D A   A right rotation on the grandparent; then, the colors of parent and grandparent are switched. (or in symmetric case: a left rotation on grandparent…)

RB-Insert Example 8 11 10 18 26 22 7 3 Example: Insert x =15 15

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 8 11 10 18 26 22 7 3 Example: Insert x =15 Recolor, move the violation up the tree. 15

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 8 11 10 18 26 22 7 3 Example: Insert x =15 Recolor, move the violation up the tree. 15

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 Insert x =15 Recolor, move the violation up the tree. 10 22 8 11 26 RIGHT-ROTATE(18) 15

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 Insert x =15 Recolor, move the violation up the tree. 8 18 11 22 RIGHT-ROTATE(18) 15 26

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 Insert x =15 Recolor, move the violation up the tree. 8 18 11 22 RIGHT-ROTATE(18) 15 26 LEFT-ROTATE(7)

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7)

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7) and recolor.

Recolor, move the violation up the tree. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 10 Example: Insert x =15 7 18 Recolor, move the violation up the tree. 3 8 11 22 15 26 RIGHT-ROTATE(18) LEFT-ROTATE(7) and recolor. Done!

Deletion Deletion, like insertion, should preserve all the RB properties. The properties that may be violated depends on the color of the deleted node. Red – OK. Why? Black? Steps: Do regular BST deletion. Fix any violations of RB properties that may result.

Analysis O(lg n) time to get through RB-Delete up to the call of RB-Delete-Fixup. Within RB-Delete-Fixup: O(lg n) time.

videos https://www.youtube.com/watch?v=vDHFF4wjWYU https://www.youtube.com/watch?v=PhY56LpCtP4