Lecture 17 Red-Black Trees

Slides:



Advertisements
Similar presentations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Advertisements

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.
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.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
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.
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.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
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.
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
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Red-Black Tree Neil Tang 02/07/2008
Red-Black Tree Neil Tang 02/04/2010
BCA-II Data Structure Using C
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.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
Red Black Trees
Chapter 11: Multiway Search Trees
Lecture 16 Multiway Search Trees
CS202 - Fundamental Structures of Computer Science II
AVL Tree.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
AVL Tree 27th Mar 2007.
Red-Black Trees Motivations
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
Lecture 26 Multiway Search Trees Chapter 11 of textbook
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
Advanced Associative Structures
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
(2,4) Trees (2,4) Trees (2,4) Trees.
Algorithms and Data Structures Lecture VIII
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
AVL-Trees (Part 1).
Red-Black Trees Bottom-Up Deletion.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-black tree properties
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Red Black Trees.
Red Black Trees Top-Down Deletion.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

Lecture 17 Red-Black Trees Slides taken from lectures by Prof. Naveen Garg

2-4 Tree insert, search, and delete of O(log n) but nodes are dynamic with 2-4 keys

Can we have benefits of 2-4 trees with simple binary nodes?

Lets convert 2-4 tree to a binary tree!

Stretch it from top and bottom! g h i j k l n o p q r s u v w x y B C D

Red Black Tree b a c e m t A d l o r v y C D g j k n p q s u w x B f h i

Convert the below 2-4 tree to Red Black Tree! 13 3 8 10 18 1 2 4 5 6 9 11 12 14 15 20 13 8 18 3 10 15 20 2 5 9 12 14 1 4 6 11

Red Black Trees A BST with Red and Black colored nodes Root is black Black node can have black children, but red node can only have black children Every external node has the same black depth

Red Black Tree Examples Black height of tree is 2 Black height of tree is 2

Are these Red-Black Trees? Double red Black height not uniform

Red-Black Tree to 2-4 Tree Take a black node and its red children and combine them into one node of a 2-4 tree. Each node so formed has at least 1 and at most 3 keys. Black height = number of levels in 2-4 tree

Convert Red-Black Tree to 2-4 Tree 9 4 13 2 7 11 19 1 3 5 17 4 13 9 1 3 2 5 7 11 17 19

Height of Red-Black Tree

Minimum number of nodes for black height h? n≥2h+1-1 h≤log2(n+1)-1

Maximum number of nodes for black height h? n≤22h+1-1 n≤4h+1-1 h≥log4(n+1)-1

Red-Black Tree Black Height log4(n+1)-1 ≤ h ≤ log2(n+1)-1 1/2log2(n+1) ≤ h+1 ≤ log2(n+1) O(log n)

Height of a Red-Black tree is at max twice the black height, i. e Height of a Red-Black tree is at max twice the black height, i.e. O(log n)

Insert (k) Search for k as in BST that will give you the correct place to insert Create a new node at this place and insert the key The new node is colored red Restore the red-black tree property

Two Cases No problem Double red problem Red root problem k k No problem Double red problem k Red root problem Color the root black

Case 1: Sibling of the parent of the inserted node is black or NULL NULL or k c a b k c Equivalent 2-4 tree node contains {b,a,k} but malformed. The rotation corrects the defect.

Case 2: Sibling of the parent of the inserted node is red k k b c b a k c a k

The double red problem can move up the tree… k What if d is root?

Time complexity of insert? BST search O(log n) Rebalancing: 1 rotation, O(log n) recoloring O(log n)

Delete To delete a node we proceed as in a BST. Thus the node which is deleted is the parent of an external node. Hence it is either a leaf or the parent of a leaf.

Three Situations 19 1 11 17 17

Deleting black Node Reduces the height by 1 In general, it may reduce the height of a subtree from h to h-1

The cases: a a is red a is black a b b is black b is red a b c a b c a Both c are black Both c are black Both d are black Some c is red a b a b c a b Some c is red Some d is red a b c a b c d a b c

Deletion: case1.1 a c b b a h to h-1 h-1 c h-1 h-1 h-1 h-1 h-1 h-1 a a

Deletion: case1.2 a a b b h to h-1 h-1 h-1 h-1 h-1 h-1 a b b a

Deletion: case2.1.1 c a b a h to h-1 b c d h h h-1 h-1 d h-1 h-1 h-1

Deletion: case2.1.2 b a a b h c h to h-1 h-1 c h h-1 h-1 h-1 h-1 a b b

Deletion: case2.2.1 a d c c a h to h-1 h-1 d h-1 h-1 h-1 h-1 h-1 h-1 a

Deletion: case2.2.2 a a c c h to h-1 h-1 h-1 h-1 h-1 h-1 a c a c

Summary In all cases, except 2.2.2, deletion can be completed by a simple rotation/recoloring In case 2.2.2, the height of the subtree reduces and so we need to proceed up the tree. But in case 2.2.2 we only recolor nodes. Thus, if we proceed up the tree then we only need to recolor. Eventually we would do a rotation.

Insert and Delete Summary In both insertion and deletion we need to make at most one rotation. We might have to move up the tree but in doing so we only recolor nodes. Time taken is O(log n).

In what situations would you prefer red-black tree over AVL Tree?

(a,b) Trees A multiway search tree. Each node has at least a and at most b children. Root can have less than a children but it has at least 2 children. All leaf nodes are at the same level. Height h of (a,b) tree is at least logb n and at most loga n.

B-Tree Every leaf has the same depth. Except the root, all nodes have between t and 2t children. The root node has between 0 and 2t children. Height is O(log n) A large value of t is used for disk based storage.

Examples Courtesy: Jan Manuch

Insertion Example Insert 65 47 32 71 93

Insertion Example Insert 65 47 32 71 65 93

Insertion Example Insert 65 Insert 82 47 32 71 65 93

Insertion Example Insert 65 Insert 82 47 32 71 65 93 82

Insertion Example Insert 65 Insert 82 47 32 71 71 65 65 93 93 82

Insertion Example Insert 65 Insert 82 47 Insert 87 32 71 65 93 82

Insertion Example Insert 65 Insert 82 47 Insert 87 32 71 65 93 82 87

Insertion Example Insert 65 Insert 82 47 Insert 87 32 71 65 93 82 87

Insertion Example Insert 65 Insert 82 47 Insert 87 32 71 65 87 82 93

Deletion Example 1 Delete 87 47 32 71 65 82 87 82 93

Deletion Example 2 Delete 71 47 32 65 71 65 87 Replace with predecessor Attach predecessor’s child 51 51 82 93

Deletion Example 3 Delete 32 47 32 71 65 87 82 93

Deletion Example 3 Delete 32 47 32 71 65 87 82 93

Deletion Example 3 71 47 47 87 65 65 82 93

Deletion Example 4 Delete 25 47 32 71 25 40 65 87 50 82 93

Deletion Example 4 Delete 25 47 32 71 25 40 65 87 50 82 93

Deletion Example 4 47 32 71 40 65 87 50 82 93