Red-Black Trees.

Slides:



Advertisements
Similar presentations
Definitions and Bottom-Up Insertion
Advertisements

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 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.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Balanced Trees Balanced trees have height O(lg n).
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.
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.
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 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 Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
Red-Black Trees Definitions and Bottom-Up Insertion.
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.
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
Red-Black Tree Neil Tang 02/07/2008
Red-Black Tree Neil Tang 02/04/2010
G64ADS Advanced Data Structures
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Topics covered (since exam 1):
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
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
TCSS 342, Winter 2006 Lecture Notes
Topics covered (since exam 1):
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Topics covered (since exam 1):
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Red Black Tree Essentials
Red Black Trees Top-Down Deletion.
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Topics covered (since exam 1, excluding PQ):
Red Black Tree Essentials
Red-Black Trees Bottom-Up Deletion.
AVL-Trees.
Red-Black Trees.
Topics covered (since exam 1):
Red-black tree properties
Red-Black Tree Rotations
Red-Black Tree Rotations
Red Black Trees Top-Down Deletion.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

Red-Black Trees

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

Red-Black Trees 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 leaf contains the same number of black nodes. Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting n.

A valid Red-Black Tree Black-Height = 2

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

Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.

Theorem 3 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf 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 leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path

Theorem 4 – A red-black tree with n internal 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 >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h

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 Leaf nodes are Black NULLS If node is Red, both children must be Black Every path from node to descendant leaf must contain the same number of Blacks

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

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

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

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

Insert 4 into this R-B Tree 11 Insert 4 into this R-B Tree 14 2 15 1 7 5 8 Red node Black node

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

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)