# Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,

## Presentation on theme: "Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,"— Presentation transcript:

Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion, deletion and search operations of the order O(log n) (if balanced). Binary balanced search-trees AVL trees Red-black trees Splay trees. General balanced search-trees 2-3 trees, 2-3-4 trees and B-trees Balanced search-tree data structures are trees that guarantee insertion, deletion and search operations of the order O(log n) by maintaining a balanced tree.

Course: Programming II - Abstract Data Types Red-Black Trees Slide Number 2Red-Black Trees Definition A Red-Black Tree is a Binary Search Tree where nodes have a color (Red or Black) and that satisfies the following properties Red-Black properties  Every node is either Red or Black.  The root node is Black  If a node is Red, then all of its children are Black  Every simple path from a given node x to any of its descendant leaves contains the same number of black nodes. (black-height(x)). No consecutive red nodes in a path.

Course: Programming II - Abstract Data Types Examples Slide Number 3Red-Black Trees Every node is either red or black The root node is black Red nodes have black parents Every simple path from a given node x to its descendant leaf has the same number of black nodes black-height(x). bh=0 bh=1 bh=2bh=1 bh=2 red-black tree Not red-black trees

Course: Programming II - Abstract Data Types Height of a Red-Black Tree Slide Number 4Red-Black Trees Theorem 1: A red-black tree with n elements has height h ≤ 2 log(n +1) Intuition: Merge red nodes into their black parents

Course: Programming II - Abstract Data Types Height of a Red-Black Tree Slide Number 5Red-Black Trees Theorem 1: A red-black tree with n elements has height h ≤ 2 log(n +1) Intuition: Merge red nodes into their black parents h’h’

Course: Programming II - Abstract Data Types Height of a Red-Black Tree Slide Number 6Red-Black Trees Theorem 1: A red-black tree with n elements has height h ≤ 2 log(n +1)  Number of null leaves in each tree is n+1 h’h’ h  Number of null leaves in a 2-3-4 tree is at least 2 h’ 2 h’ ≤ n+1 ⇒ h’ ≤ log(n+1) Also h ≤ 2 h’ h ≤ 2 log(n +1)

Course: Programming II - Abstract Data Types Interface for Red-Black trees Slide Number 7 public interface RBT,V>{ public boolean contains(K searchKey); //Post: Searches for an element with key equal to searchKey in the tree. //Post: Returns false if no element is found. public V retrieve(K searchKey); //Post: Retrieves an element with key equal to searchKey from the tree. Returns either the value of //Post: the element if found, or null if no element with given searchKey exists. public void insert(K key, V newValue); //Post: Adds a new element to the tree with given key and newValue, preserving the properties of //Post: the red-black tree. If the tree includes already an element with the given key, it replaces its //Post: value with newValue. public void delete(K searchKey) throws TreeException; //Post: Removes an element with key equal to searchKey from the tree, preserving the properties of //Post: the red-black tree. Throws an exception if such element is not found. } Red-Black Trees

Course: Programming II - Abstract Data Types Data Structure for Red-Black Trees Slide Number 8Red-Black Trees

Course: Programming II - Abstract Data Types Rotation revisited Slide Number 9Red-Black Trees Rotate right (B) rotateRight( ){ // Rotates the current object to the right and returns its new parent get leftChild of B; set T2 to be the left subtree of B; reparent the old leftChild of B; set B to be the right child of the old leftChild of B }

Course: Programming II - Abstract Data Types Rotation revisited Slide Number 10Red-Black Trees Rotate left (A) rotateLeft( ){ // Rotates the current object to the right and returns its new parent get rightChild of A; set T2 to be the right subtree of A; reparent the old rightChild of A; set A to be the left child of the old rightChild of B }

Course: Programming II - Abstract Data Types Insertion Slide Number 11Red-Black Trees  insert new node as per insert operation in binary search tree  color the new node red (to preserve property 4) if new node is root node, repaint it black (Case 1) if parent is black, property 3 is satisfied (Case 2) if parent is red, property 3 is violated The operation performs the following steps: Double red problem  Recoloring  Restructuring the links of the tree by rotation Three more cases

Course: Programming II - Abstract Data Types Insertion (continue..) Slide Number 12 Resolving double-red problem Parent node is Red, and Uncle node is Red Case 3: Recolor grandparent Red Recolor parent Black Recolor uncle Black Check cases again for grandparent Red-Black Trees

Course: Programming II - Abstract Data Types Insertion (continue..) Slide Number 13 Rotate right (parent node) Current node is old parent Case 5 on current node Red-Black Trees Resolving double-red problem Parent node is Red, and Uncle node is Black (or null) Parent is right child of grandparent, and Current node is left child of parent Case 4: 4b

Course: Programming II - Abstract Data Types Insertion (continue..) Slide Number 14Red-Black Trees Parent becomes Black Grandparent becomes Red Rotate left (grandparent node) Resolving double-red problem Parent node is Red, and Uncle node is Black (or null) Parent is right child of grandparent, and Current node is right child of parent Case 5: 5a

Course: Programming II - Abstract Data Types Summary of Cases Slide Number 15 Case 2 DONE Case 3 Recolor go to Case 1 with current = G + Red-Black Trees Case 1 Recolor node. DONE

Course: Programming II - Abstract Data Types Slide Number 16 Case 4a Rotates left parent go to Case 5 with current = P + Case 4b Rotates right P go to Case 5 with current = P + Summary of Cases Red-Black Trees

Course: Programming II - Abstract Data Types Slide Number 17 Case 5a Recolor P and G, rotate right G DONE Summary of Cases Case 5b Recolor P and G, rotate left G DONE Red-Black Trees

Course: Programming II - Abstract Data Types Slide Number 18 Implementing RedBlackTree Red-Black Trees public class RedBlackTree, V> implements RBT { private RBTNode root; public RedBlackTree() { this.root = null;} public V retrieve(K key){ RBTNode node = retrieveElem(root, key).getValue(); if (node == null){throw new NoSuchElementException();} return node.getValue(); } public void insert(K key, V newValue){ > }

Course: Programming II - Abstract Data Types Slide Number 19 Implementing Insert Red-Black Trees public void insert(K key, V newValue){ Tuple, RBTNode > pair = findNode(key); RBTNode parent = pair.getX(); RBTNode current = pair.getY(); if (current != null) { Set the value of current to be newValue;} else if (parent == null){ Set the root to the be new RBTNode; Set the color of the root to BLACK; } else { Create a new RBTNode with color RED; if (key < parent.getKey()) Set the newRBTNode to be the left child of parent; else Set the newRBTNode to be the right child of parent; insertCaseOne(newRBTNode); } } Key already exists The tree is empty, create root node Check properties of RedBlack Tree

Course: Programming II - Abstract Data Types Analysis Red-Black TreesSlide Number 20 AVL trees and Red-Black trees are data structure that guarantee insertion, deletion and search operations to be of the order O(log n). How do AVL trees and Red-Black trees compare?  The height of an AVL tree is also logarithmic in n.  An AVL tree can be colored to become a Red-Black tree,  A Red-Black tree cannot be seen as an AVL tree: o Red-black trees can have larger heights than AVL trees o AVL trees are more rigidly balanced than Red-Black trees  Red-Black tree have slightly faster insertions and deletion than AVL trees (TreeMap in Java uses Red-Black trees).

Similar presentations