110 likes | 304 Views
COMP 6/4030 ALGORITHMS. RED-BLACK TREES. What is RBT?. Binary Tree Each node is colored Red or Black Black edge goes to Black node No Red node has Red child All external paths from a given node have the same Black length Root node is Black. Almost RBT (ARBT): Conditions 1-5 satisfied
E N D
COMP 6/4030 ALGORITHMS RED-BLACK TREES
What is RBT? • Binary Tree • Each node is colored Red or Black • Black edge goes to Black node • No Red node has Red child • All external paths from a given node have the same Black length • Root node is Black • Almost RBT (ARBT): • Conditions 1-5 satisfied • Root is Red
Advantages of RBT • Efficient way to implement Binary Search • Algorithms for Insertion & Deletion of nodes are established • Well balanced • Height is at most 2log(n+1) • ~ 2 * most balanced binary tree
Red-Black Trees (contd.) • RBT has 4 instance fields • Root, Left Subtree, Right Subtree, Color of root • Every node has one and only one color – either Black or Red
Red-Black Trees (contd.) • Defn. of RBT (recursive): RBh and ARBh Binary trees with colored nodes (B or R) • External nodes are Black: RB0 tree • h>0: an ARBh: Red root, left & right subtrees are RBh-1 trees • h>0: an RBh : Black root, left & right subtrees are RBh-1 or ARBh trees
RBT with black-depth convention • Red children at the level of parents • Black Depth = Geometric Depth • Advantages • Transparent • Bounds can be derived
RBT Operations • Search, Insert node, Delete node • Insertion: • Select location of new key: ‘K’ • Insert a tree containing ‘K’ (Red) • Fix up color violations (Color Flip; Rebalancing) • Deletion: • Do standard BST search to determine the node to be deleted • Identify the node structure to be changed • Carry out structural deletion and repair imbalance in Black height
Properties of RBTs • Theorem 1 Given that T is RBh tree (h is Black height) then • T has at least 2h – 1 internal Black nodes • T has at most 4h – 1 internal nodes • The depth (general) of any Black node is at most twice its Black depth • Theorem 2 Given that A is ARBh tree, then • A has at least 2h –2 internal Black nodes • A has at most ½ (4h) – 1 interval nodes • The depth of any Black node is at most twice its Black depth • Theorem 3 Given that T is an RBT with n internal nodes, then no node has depth larger than 2log(n+1) i.e., the height of T (general) is at most 2log(n+1) the desired upper limit for the depth of a RBT