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.

Slides:



Advertisements
Similar presentations
Definitions and Bottom-Up Insertion
Advertisements

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.
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.
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.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
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.
Balanced Trees Balanced trees have height O(lg n).
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.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
Introduction to Algorithms Jiafen Liu Sept
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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: 
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
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.
Review for Exam 2 Topics covered: –Recursion and recursive functions –General rooted trees –Binary Search Trees (BST) –Splay Tree –RB Tree –K-D Trees For.
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.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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
Red-Black Tree Neil Tang 02/04/2010
G64ADS Advanced Data Structures
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Algorithms and Data Structures Lecture VIII
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red-black tree properties
Red Black Trees Top-Down Deletion.
Presentation transcript:

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 to be a black “node” –If a node is red, then both of its children are black. –Every path from a node to a NULL contains the same number of black nodes. –The root is black Definition: The black-height of a node, X, in a red-black tree is the number of black nodes on any path to a NULL, not counting X.

3 A Red-Black Tree with NULLs shown Black-Height of the tree = 4

4 A valid Red-Black Tree Black-Height = 2

5

6

7 Theorem 1 – Any red-black tree with root x, has n >= 2 bh(x) – 1 nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.

8 Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a NULL must be black. Proof – If there is a red node on the path, there must be a corresponding black node. Algebraically this theorem means bh( x ) ≥ h/2

9 Theorem 3 – In a red-black tree, no path from any node, N, to a NULL is more than twice as long as any other path from N to any other NULL. Proof: By definition, every path from a node to any NULL contains the same number of black nodes. By Theorem 2, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path

10 Theorem 4 – A red-black tree with n nodes has height h ≤ 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) ≥ h/2 From Theorem 1, n ≥ 2 bh(x) - 1 Therefore n ≥ 2 h/2 – 1 n + 1 ≥ 2 h/2 lg(n + 1) ≥ h/2 2lg(n + 1) ≥ h

11 Bottom –Up Insertion Insert node as usual in BST Color the Node RED What Red-Black property may be violated? –Every node is Red or Black –NULLs are Black –If node is Red, both children must be Black –Every path from node to descendant NULL must contain the same number of Blacks

12 Bottom Up Insertion Insert node; Color it RED; X is pointer to it Cases 0: X is the root -- color it black 1: Both parent and uncle are red -- color parent and uncle black, color grandparent red, point X to grandparent, check new situation 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left(right) on parent, rotate right(left) on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left (right) children -- color parent black, color grandparent red, rotate right(left) on grandparent

13 X P G U P G U Case 1 – U is Red Just Recolor and move up X

14 X P G U S X P G S U Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X

15 X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G

Black node Red node Insert 4 into this R-B Tree

17 Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree

18 Asymptotic Cost of Insertion O(lg n) to descend to insertion point O(1) to do insertion O(lg n) to ascend and readjust == worst case only for case 1 Total: O(log n)