Download presentation

Presentation is loading. Please wait.

Published bySimone Keach Modified over 2 years ago

1
1 Unbalanced binary search trees Balanced search trees 2-3-4 trees Red-black trees CSE 30331 Lecture 17 – Balanced trees

2
2 Unbalanced Binary Search Tree and balanced alternatives

3
3 Two Binary Search Tree Examples Insertion sequence for tree (a): 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 5 18 35 25 1775 6 127 20 9 153 40 5 18 35 25 17 75 6 12 7 20 9 15 3 40 Depth = 6 Average comparisons per search = 4.0 Depth = 4 Average comparisons per search = 3.47 (a) unbalanced(b) Same data, but more balanced 100

4
4 What is a 2-3-4 Tree? Each node has 1, 2 or 3 values Each node has 2, 3 or 4 children The tree is ALWAYS perfectly balanced Depth of all subtrees of a node are equal

5
5 2-3-4 Tree Example

6
6 2-3-4 Tree (node types) A 2-node (value < A) (value > A) A B 3-node A < B (value < A)(A < value < B)(value > B) A B C 4-node A < B < C (value < A)(A < value < B)(value > C)(B < value < C)

7
7 2-3-4 Tree Insertion Splits 4-nodes top-down 3 6 9 24 10 8 Inserting 7 requires splitting of the 4-node before adding 7 as a child (leaf or part of existing leaf) 93 6 2107 8 4 After splitting, 7 is added to a leaf following normal search tree order

8
8 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 LEAF 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 always an equivalent red-black binary tree

9
9 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 4, first split the root (a 4-node) and then add value to leaf

10
10 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 Insert 8 To insert 10, first split the root (a 4-node) and then add value to leaf

11
11 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 4 12 8 10 15 25 Insert 25 2 4 12 15 25 35 8 10 Insert 35 2 4 12 25 8 10 15 35 2 4 12 25 8 10 15 35 55 Insert 55 Split 4-node (15, 25, 35) To insert 55, first split the leaf (a 4-node) and then add value to leaf

12
12 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 11, again requires top-down split of each 4-node encountered, so root is split and new value is inserted in leaf 2 8 10 15 35 55 25 4 12 Split 4-node (4, 12, 25) 2 8 10 11 15 35 55 25 4 12 Insert 11

13
13 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 9, split the leaf (a 4-node) and insert new value 215 35 55 25 12 4 10 118 Split 4-node (8, 10, 11) 2 15 35 55 25 12 4 10 11 8 9 Insert 9

14
14 Building 2-3-4 Tree by Series of Insertions Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 15 35 55 25 12 4 10 11 5 8 9 Insert 5 2 15 35 55 25 12 4 8 10 11 9 5 Split 4-node (5, 8, 9) 2 15 35 55 25 12 4 8 10 119 5 7 And finally insert 7 in a leaf

15
15 Inefficiencies 2-3-4 trees require space for up to... 3 values in every node Pointers to 4 children from every node Even if they are not used in all cases So, we can use a Red Black tree instead A (colored) form of binary search tree Always relatively balanced Nodes have no more storage required than normal binary search tree

16
16 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(log 2 n)

17
17 Red Black Tree Node Types BLACK parent with two RED children is the 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 always inserted a RED leaves

18
18 Red-Black subtree equivalents of 2-3-4 tree nodes

19
19 Converting a 2-3-4 Tree to Red-Black Tree (top-down) First, convert the root (note: shaded nodes are RED) Original

20
20 Converting a 2-3-4 Tree to Red-Black Tree (top-down) Then convert the right subtree and we’re done Next, convert left subtree

21
21 Building red black tree by repeated insertions Insert root and color it BLACK 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

22
22 Situations in the Splitting of a 4-Node (A X B)

23
23 Required actions to split a 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 P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

24
24 Left child of a Black parent P: Split requires only a Color Flip

25
25 Example split prior to inserting 55: flip colors (40,50,60) & insert leaf

26
26 Required actions to split a 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 P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

27
27 RED parent, 4-node oriented left-left from G (grandparent)

28
28 Required actions to split a 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 P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

29
29 Red Parent : Oriented Left- Right From G

30
30 Red Parent : Oriented Left- Right From G B A X G P C D Red-black tree after first (left) rotation (about X) BA X G P C D Red-black tree after color flip

31
31 Red Parent : Oriented Left- Right From G B A X G P C D Red-black tree after first (left) rotation (about X) After second (right) rotation about X and coloring X black, G & P red B A G X P CD

32
32 Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

33
33 Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

34
34 Example on blackboard Insert: 2, 12, 15, 7, 5, 8, 10

35
35 Deleting nodes from a Red- Black Tree If node has 2 children Swap value with that of inorder successor and Delete the successor node (it has at most 1 child) If node has 1 or 0 children If it is RED Delete it and replace with its BLACK child If it is BLACK and its child is RED Delete it, replace with its RED child and recolor it BLACK If it is BLACK and its child is BLACK Yikes!!!!

36
36 Deleting nodes from a Red- Black Tree If node has 1 or 0 children If it is BLACK and its child is BLACK Delete the node and replace with its child BUT... Now we have a situation. We have removed a BLACK node from some paths and thus the BLACK height may differ in this part of the tree from others If the node deleted was the root, we are done All paths are shorter by 1 black node If not the root, then there are 5 other cases These involve bottom-up rebalancing of the tree using recoloring and various rotations See http://en.wikipedia.org/wiki/Red-black_treehttp://en.wikipedia.org/wiki/Red-black_tree

37
37 Representing a Red-Black Tree The rbNode class from Ford & Topp

38
38 Summary 2-3-4 tree Each node has either … 1 value and 2 children 2 values and 3 children 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google