Red black trees

JudeTCHAYEKONDI 254 views 41 slides Sep 26, 2019
Slide 1
Slide 1 of 41
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41

About This Presentation

A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK.


Slide Content

Red-Black Trees Name : TCHAYE-KONDI JUDE Simulator Link : https://www.cs.usfca.edu/~galles/visualization/RedBlack.html

RESUME INTRODUCTION TREE BINARY TREE BINARY SEARCH TREE RED BLACK TREE OPERATIONS

INTRODUCTION

Tree Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

Root Edge

Leaf

Internal Nodes

Level

Height

Path

BINARY TREE A tree in which every node can have a maximum of two children is called as Binary Tree. A binary tree has the following time complexities : Search Operation - O(n) Insertion Operation - O(n) Deletion Operation - O(n)

BINARY SEARCH TREE Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.

BINARY SEARCH TREE  left subtree of every node contains nodes with smaller values and right subtree of every node contains larger values. Algorithm Average Worst case Search O(log  n ) O( n ) Insert O(log  n ) O( n ) Delete O(log  n ) O( n ) Time complexity

EG Worse Case Input : 7, 10, 13, 15 Average Case Input : 10 , 7 ,15 ,13 We can easily prove this by counting nodes on each level, starting with the root, assuming that each level has the maximum number of nodes: Geometrical sequence sum : n = 1 + 2 + 4 + ... + 2 h-1 + 2 h = 2 h+1 - 1

Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced tree supports efficient operations, There are many implementations of balanced BSTs, including AVL trees, RedBlack trees and AA trees.

RED BLACK TREE A red-black tree is a binary search tree with one extra bit of storage per node: its color , which can be either RED or BLACK . By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other. Each node of the tree now contains the attributes color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL.

PROPERTIES A red-black tree is a binary tree that satisfies the following 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 simple paths from the node to descendant leaves contain the same number of black nodes.

BLACK-HEIGHT the number of black nodes on any simple path from, but not including, a node x down to a leaf the black-height of the node, denoted bh (x) or B We define the black-height of a red-black tree to be the black-height of its root.

OPERATIONS & COMPLEXITY

OPERATIONS INSERTION, DELETION, ROTATIONS SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, AND PREDECESSOR…

THE NUMBER OF INTERNAL NODES OF A NODE X N(X) ≤  

INTERNAL NODES OF X Let prove by induction that : the subtree rooted at any node x contains at least internal nodes.   If the height of x is 0 then x must be a leaf = 0   For the inductive step, consider a node x that has positive height and is an internal node with two children : Each child has a black-height of either bh (x) or bh (x) - 1 , depending on whether its color is red or black , respectively. We conclude that each child has at least : internal nodes ( the height of a child of x is less than the height of x itself )   each child has at least internal nodes.(and we have 2 childs ) Then the subtree rooted at x contains at least ( )+( ) +1 = (+1 because of the root node)  

INTERNAL NODES OF X Let prove by induction that : the subtree rooted at any node x contains at least internal nodes.   If the height of x is 0 then x must be a leaf = 0   For the inductive step, consider a node x that has positive height and is an internal node with two children : Each child has a black-height of either bh (x) or bh (x) - 1 , depending on whether its color is red or black , respectively. We conclude that each child has at least : internal nodes ( the height of a child of x is less than the height of x itself )   each child has at least internal nodes. Then the subtree rooted at x contains at least ( )+( ) +1 =  

Conclusion THE NUMBER OF INTERNAL NODES OF A NODE X ( N(X) ) N(X) ≤  

HEIGHT OF A RB TREE Knowing the number of internal nodes (n)

HEIGHT OF A RB TREE(H) A red-black tree with n internal nodes has height at most .   According to property 4( If a node is red, then both its children are black. ) , at least half the nodes on any simple path from the root to a leaf, not including the root, must be black. Consequently, the black-height of the root must be at least h=2 ; thus, As an immediate consequence of this lemma, we can implement the dynamic-set operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR in Olg (n) time on red-black trees,

OPERATIONS They do not directly support the dynamic-set operations INSERT and DELETE, since they do not guarantee that the modified binary search tree will be a red-black tree.

EG Input : 8 18 5 15 Violation of the property saying : If a node is red, then both its children are black. Thus, the set operations are fast if the height of the search tree is small.

ROTATIONS

LEFT ROTATION :

RIGHT ROTATION

Rotation is followed by Recolor operation

INSERTION

INSERTION We can insert a node into an n-node red-black tree in Olg (n) time.

INSERTION

Example Input : 8 18 5 15 17 25 40 80

Big-O Complexity Chart