280 likes | 412 Views
CSE331 – Lecture 20 – Advanced Associative Structures. 1. Main Index. Contents. Chapter 11 Binary Search tree, red-black tree, and AVL tree Binary Search Trees 2-3-4 Tree Insertion of 2-3-4 tree Red-Black Trees Converting 2-3-4 tree to Red-Black tree.
E N D
CSE331 – Lecture 20 – Advanced Associative Structures 1 Main Index Contents • Chapter 11 • Binary Search tree, red-black tree, and AVL tree • Binary Search Trees • 2-3-4 Tree • Insertion of 2-3-4 tree • Red-Black Trees • Converting 2-3-4 tree to Red-Black tree • Four Situations in the Splitting of a 4-Node: • left child of a BLACK parent P • prior to inserting node • oriented left-left from G using a single right rotation • oriented left-right from G after the color flip • Building a Red-Black Tree • Red-Black Tree Representation • Summary Slide
2 Main Index Contents Binary Search Tree, Red-Black Tree and AVL Tree Example
Two Binary Search Tree Example • Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40
4 Main Index Contents 2-3-4 Tree (node types)
5 Main Index Contents 2-3-4 Tree Example
2-3-4 Tree InsertionSplits 4-nodes top-down After split, 7 is added to leaf following normal search tree order Inserting 7 into tree requires split of 4-node before adding 7 as child (leaf or part of existing leaf) 6 3 6 9 3 9 2 10 4 8 2 10 7 8 4
2-3-4 Tree Insertion Details • 4-nodes must be split on the way down • Split involves “promotion” of middle value into parent node • New value is always added to existing 2-node or 3-node • 2-3-4 tree is always perfectly balanced • Implementation is wasteful of space • Each node must have space for 3 values, 1 parent pointer and 4 child pointers, even if not always used • There is an equivalent red-black binary tree
8 Main Index Contents Building 2-3-4 Tree by Series of Insertions • Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
9 Main Index Contents Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
Red Black Tree Properties • The root is always BLACK • A RED parent never has a RED child • Every path from ROOT to an EMPTY subtree has the same black height (# of BLACK nodes) • Black Height of tree is O(log2n)
Red Black Tree Node Types • BLACK parent with two RED children is equivalent of a 4-node • A 4-node split is accomplished by flipping the colors to RED parent & two BLACK children, with possible rotation if colors conflict • BLACK parent with single RED child is equivalent of a 3-node • New nodes are inserted a RED leaves
13 Main Index Contents Red-Black Trees – binary equivalents of 2-3-4 trees
Building red black tree by repeated insertions • Insert root • Each successive insertion point is found in normal search tree order • Any 4-nodes (black parent with two red children) is split going down into tree • Requires color changes and possible rotations of subtrees • New value is added as RED leaf • If result is RED child of RED parent, then rotations are required to correct
16 Main Index Contents Situations in the Splitting of a 4-Node (A X B)
Required actions to split 4-node (A X B) with parent P & grandparent G • 4-node is either child of black parent • Flip subtree colors ONLY (X red, A & B black) • 4-node is child of red parent (left-left) • Flip subtree colors & single right rotation about root • 4-node is child of red parent (right-right) • Flip subtree colors & single left rotation about root • 4-node is child of red parent (left-right) • Double rotation (first left, then right) about X & then color X black, G & P red • 4-node is child of red parent (right-left) • Double rotation (first right, then left) about X & then color X black, G & P red
18 Main Index Contents Left child of a Black parent P:Split requires only a Color Flip
Example split prior to inserting 55: flip colors (40,50,60) & insert leaf
20 Main Index Contents RED parent, 4-node oriented left-left from G (grandparent)
21 Main Index Contents Red Parent : Oriented Left-Right From G
Red-black tree Red-black tree after first (left) rotation (about X) after color flip G G D P X D X P B C A B C A Red Parent : Oriented Left-Right From G
Red-black tree after first (left) rotation (about X) G X X P D G P C B D A B C A Red Parent : Oriented Left-Right From G After second (right) rotation about X and coloring X black, G & P red
24 Main Index Contents Building A Red-Black TreeInsertions: 2, 15, 12, 4, 8, 10, 25, 35
25 Main Index Contents Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35
27 Main Index Contents Summary Slide 1 §- 2-3-4 tree - a node has either 1 value and 2 children, 2 values and 3 children, or 3 values and 4 children - construction of 2-3-4 trees is complex, so we build an equivalent binary tree known as a red-black tree
28 Main Index Contents Summary Slide 2 §- red-black trees - Deleting a node from a red-black tree is rather difficult. - the class rbtree, builds a red-black tree