160 likes | 369 Views
Foundation of Computing Systems. Lecture 9 Trees: Part VI-A. Red Black Binary Trees. Approaches to balance a binary search tree so that the searching time can be reduced Height-balanced tree (AVL tree) Red-Black tree. Definition of Red Black Tree.
E N D
Foundation of Computing Systems Lecture 9 Trees: Part VI-A
Red Black Binary Trees • Approaches to balance a binary search tree so that the searching time can be reduced • Height-balanced tree (AVL tree) • Red-Black tree IT 60101: Lecture #9
Definition of Red Black Tree • A red-black tree is a binary search tree that satisfies the following properties Color property: • Every node is either red or black. Root property: • The root is always colored with black. External property: • Every external node is black. Internal property: • If a node is red then both its children are black. Depth property: • For each node, all paths from that node to descendant external nodes contain the same number of black nodes. IT 60101: Lecture #9
Definition of Red Black Tree It is a binary search tree [Basic property] IT 60101: Lecture #9
Definition of Red Black Tree Every node is either red or black [Color property] IT 60101: Lecture #9
Definition of Red Black Tree The root is colored as black [Root property] IT 60101: Lecture #9
Definition of Red Black Tree All external nodes are colored with black color [External property] IT 60101: Lecture #9
Definition of Red Black Tree Children of every red-colored node are black [Internal property] IT 60101: Lecture #9
Definition of Red Black Tree For every node in the tree and all paths from that node to external nodes, counts of black colored nodes are same [Depth property] IT 60101: Lecture #9
Operations on Red-Black Trees • Major operations are: • Insertion (to insert a new node) • Deletion (to remove an existing node) • Searching (to find an element) • Structure of a node in RBT IT 60101: Lecture #9
Red-Black Trees: Insertion • Suppose the node we are going to insert having value x and x is not present in the tree. • We follow the following preprocessing steps • Allocate a node for red-black tree • Assign the value x into the field DATA • Initially, the color of the node is made RED • The node has two black children as external nodes IT 60101: Lecture #9
Red-Black Trees: Insertion • Cases: • Red-Black tree is empty • The node x is inserted so that its parent is colored with BLACK • The node x is inserted so that its parent is colored with RED • x’ parent is RED and x’ uncle is RED • x is RIGHT child and x’s uncle is BLACK • x is the LEFT child and x’s uncle is BLACK IT 60101: Lecture #9
Insertion : Case 1 • Case 1: Red-Black tree is empty IT 60101: Lecture #9
Insertion : Case 2 • Case 1: The node x is inserted so that its parent is colored with BLACK IT 60101: Lecture #9
To be continued… IT 60101: Lecture #9