Download presentation
Presentation is loading. Please wait.
1
1 2-3-4 Trees and Red-Black Trees Gordon College Prof. Brinton
2
2 Regular Binary Trees Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 Insertion sequence: 9, 5, 20, 3, 7, 15, 75, 6, 12, 17, 35, 100, 18, 25, 40
3
3 Balanced Trees Need a new search-tree structure - a balanced binary search tree 1.Maintains balanced node when adding or removing them Extra time needed at insert and remove 2.Guarantees at worst O(log n) search time We start with 2-3-4 trees… - Perfectly balanced - Difficult and inefficient to implement
4
4 2-3-4 Trees Definitions 2-node - a data value and pointers to 2 subtrees 3-node - two data values and pointers to 3 subtrees 4-node - three data values and pointers to 4 subtrees A<B A<B<C
5
5 2-3-4 Tree 2-3-4 trees have search tree properties What is the basic 2-3-4 search algorithm?
6
6 Inserting into a 2-3-4 Tree Insert begins with a single node and adds elements until it is full Insert another item 1. split the 4 node using the median value as the parent (promoting the median value to the parent level) 2. Insert the new item (inserted based on the BST rules for insertion) (always insert node in leaf of tree) C
7
7 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
8
8 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
9
9 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
10
10 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
11
11 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
12
12 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
13
13 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25) Proactive top-down approach to splitting a 4-node
14
14 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25)
15
15 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
16
16 2-3-4 Insertion Example Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7
17
17 Another example Keys: A S E R C H I N G X What would the 2-3-4 tree look like after inserting this set of keys?
18
18 Another example Keys: A S E R C H I N G X A
19
19 Another example Keys: A S E R C H I N G X A S
20
20 Another example Keys: A S E R C H I N G X A E S
21
21 Another example Keys: A S E R C H I N G X R S E A
22
22 Another example Keys: A S E R C H I N G X R S E A C
23
23 Another example Keys: A S E R C H I N G X H R S E A C
24
24 Another example Keys: A S E R C H I N G X H IS A C E R
25
25 Another example Keys: A S E R C H I N G X H I NS A C E R
26
26 Another example Keys: A S E R C H I N G X G HN A C E I R S
27
27 Another example Keys: A S E R C H I N G X G HN A C S X I E R
28
28 2-3-4 Tree Facts With N elements, the max number of nodes visited during the search for an element is int(log 2 n) + 1 Inserting an element into a tree with n elements requires splitting no more than int(log 2 n) + 1 4- nodes (often far fewer) Problem: allocated a large amount of wasted space 3n + 1 unused pointers (n - nodes) 28 wasted pointers
29
29 2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55
30
30 2-3-4 Warmup Exercise Create a 2-3-4 tree from the following sequence of numbers: 45 6 23 5 78 9 10 11 48 99 12 55
31
31 Red-Black Trees Designed to represent 2-3-4 tree without the additional link overhead. Colors are used to represent the 3-node and 4-node. Red-Black trees are simple binary trees with a color indicator that is used to maintain certain properties - these properties keep the tree balanced.
32
32 Red-Black Nodes 2-nodes simple binary node (black node) 4-nodes center value becomes the parent (black) with outside values becoming the children (red) N H I N I H N
33
33 Red-Black Nodes 3-nodes A B A B B A or Note: 1.Red-black trees are not unique 2.However, the corresponding 2-3-4 tree is unique
34
34 Red-Black Nodes A B B A NI H N Use color grouping of nodes to indicate the corresponding nodes in the 2-3-4 tree
35
35 Converting a 2-3-4 Tree to Red- Black Tree Example Top-down conversion algorithm: (start at the root) 1.Apply red-black tree representation to each node 2.Repeat for next level…
36
36 Converting a 2-3-4 Tree to Red- Black Tree Example
37
37 Converting a 2-3-4 Tree to Red- Black Tree Example H I N I H N
38
38 Converting a 2-3-4 Tree to Red- Black Tree Example
39
39 Converting a 2-3-4 Tree to Red- Black Tree Example
40
40 Converting a 2-3-4 Tree to Red- Black Tree Example How could this be different?
41
41 Red-Black Tree Properties 1.The Root of a red-black tree is BLACK 2.A RED parent never has a RED child – there are never 2 RED nodes in succession 3.Every path from the root to an empty subtree (NULL pointer) has the same number of BLACK nodes. a BLACK node corresponds to a level change in the 2-3-4 tree
42
42 Inserting Nodes Guidelines 1.Maintain root as BLACK node 2.Enter a new node as a RED node – since each new node enters a 2- node or a 3-node. 3.Whenever it results in two RED nodes in succession – rotate nodes to create a BLACK parent. 4.When scanning down a path to find insertion location – split any 4- node.
43
43 Inserting Nodes Inserting a 2 3-node 5 12
44
44 Inserting Nodes Inserting a 2 3-node 4-node 5 12 5 2
45
45 Inserting Nodes Inserting a 14 5 12
46
46 Inserting Nodes Inserting a 14 5 12 5 14
47
47 Inserting Nodes Inserting a 14 5 12 14 5 5 12 14 Single left rotation
48
48 Inserting Nodes Inserting a 10 5 12
49
49 Inserting Nodes Inserting a 10 5 12 5 10
50
50 Inserting Nodes Inserting a 10 5 12 5 10 12 5 right - left rotation
51
51 Splitting a 4-node 4 possible situations Step1: color flip – parent x becomes RED and its two children become BLACK.
52
52 Splitting a 4-node Parent: BLACK Color flip is enough.
53
53 Splitting a 4-node and inserting node 55 Color flip is enough.
54
54 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-left from node G using a single right rotation
55
55 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-right from node G after the color flip
56
56 Splitting a 4-node Parent: RED Splitting a 4-node oriented left-right from node G after the color flip Red-Black Tree Property Violation?
57
57 Two REDs: rotations X G P A B A P X G B C D C D A G P X B D C Single Left-rotation About X P G X A B D C Single Right-rotation About X
58
58 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7
59
59 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7 Split when Going down? 55 11
60
60 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7 Correct 5 12 2 4 node
61
61 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7
62
62 Building a RED-BLACK tree 2, 15, 12, 4, 8, 10, 25, 35, 55,11, 9, 5, 7
63
63 Building a RED-BLACK tree 10, 20, 30, 40, 50, 31, 34, 36 No problem up to this point. What would the 2-3-4 tree look like?
64
64 Deleting RED-BLACK nodes To Delete: same strategy as deleting any node from a binary tree (Chapter 10) Delete node 25 Replace deleted node with value nearest to deleted value: either 15 or 30
65
65 Deleting RED-BLACK nodes If replace node is RED then level height of tree is unchanged - no other action is needed except making sure color properties are maintained
66
66 Deleting RED-BLACK nodes If replace node is BLACK then adjustments must be made: a bottom-up algorithm is used to flip and rotate. DELETE: O(log n)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.