Chapter 12 Advanced Data Structurestadavis/cs303/ch12sm.pdf · Chapter 12 Advanced Data Structures...
Transcript of Chapter 12 Advanced Data Structurestadavis/cs303/ch12sm.pdf · Chapter 12 Advanced Data Structures...
Chapter 12Advanced Data Structures
Red-Black Trees
2
add the attribute of _________ (red or black) to links/nodesred-black trees used in
C++ Standard Template Library (STL)Java to implement maps (or _________________, as inPython)
Red-Black Trees
3
a red-black tree is a ______ with the following properties:every node is either red or blackthe root is __________if a node is red, its children must be _________every path from the root to a null link contains the samenumber of black nodes
perfect black ___________the height of an -node red-black BST is at most
, sosearch, insertion, and deletion are operations
Red-Black Trees
4
building a red-black treein order to maintain perfect black balance, any new nodeadded to the tree must be ______if the parent of the new node is _________, all is wellif the parent of the new node is ______, this violates thecondition that red nodes have only black children
fix with ___________ similar to those for AVL and splaytreescan be used to maintain the red-black structure at anypoint in the tree, not just at insertion of a new node
Red-Black Trees
5
top-down insertion: color flipsto preserve perfect black balance, a newly inserted nodemust be redin top-down insertion, we change the tree as we movedown the tree to the point of ______________
the changes we make insure that when we insert thenew node, the parent is _________if we encounter a node X with two red children, wemake X red and its children black
if X is the root, we change the color back to blacka color flip can cause a red-black violation (a red child
______
Red-Black Trees
6
red-black tree rotationscase 1: the ___________black (or missing)
this is a _________ rotation and a color swap for P and G
Red-Black Trees
7
red-black tree rotations
(or missing)
this is a _________ rotation and a color swap for X and G
Red-Black Trees
8
red-black tree rotations______
and its sibling are both redwe changed all such pairs to _________ on the waydown
Red-Black Trees
9
example: GOTCHA
Red-Black Trees
10
example: GOTCHA (cont.)
Red-Black Trees
11
example: GOTCHA (cont.)
Red-Black Trees
12
example: GOTCHA (cont.)
the standard ______ (rightmost) for GOTCHA is slightlyshorter
Red-Black Trees
13
example: ISOGRAM
Red-Black Trees
14
example: ISOGRAM (cont.)
Red-Black Trees
15
example: ISOGRAM (cont.)
Red-Black Trees
16
example: ISOGRAM (cont.)
Red-Black Trees
17
example: ISOGRAM (cont.) 2-3-4 treesuseful because we can insert new items while maintaining___________ balancea 2-3-4 tree consists of
2-nodes: one key, ______ children3-nodes: two keys, __________ children4-nodes: three keys, ________ children
2-3-4 Trees
18
2-3-4 Trees
19
insertion into 2-3-4 treesinsert the new key into the ___________ existing nodereached in the search
2-3-4 Trees
20
what about a 4-node?top-down insertion
as we move down the tree, whenever we encounter a4-node, we move the __________ element up into theparent node and break up the remainder into _______2-nodes
2-3-4 Trees
21
what about a 4-node?top-down insertion (cont.)
insertion, if done here, now reduces to the case of a__________ or ___________
2-3-4 Trees
22
top-down insertionas we move down the tree, we split up 4-nodes as weencounter them through the following process
move the middle key up to the parentsplit the remaining keys into 2-nodes
this action ______________ that the parent of any 4-nodewe encounter is a 2-node or 3-node
therefore, the tree will always have room to accept the___________ element of the 4-node
2-3-4 Trees
23
example: insert 42, 9, 39, 11, 27, 13, 33, 16, 28the first three insertions are straightforward
when inserting 11, we encounter a 4-node, which we ______
39 is first promoted as a new root nodeperfect balance is maintained in 2-3-4 trees by growing atthe ________
2-3-4 Trees
24
example: insert 42, 9, 39, 11, 27, 13, 33, 16, 28 (cont.)insert 27
insert 13: first split 4-node
2-3-4 Trees
25
example: insert 42, 9, 39, 11, 27, 13, 33, 16, 28 (cont.)insert 33
insert 16: split 4-node
2-3-4 Trees
26
example: insert 42, 9, 39, 11, 27, 13, 33, 16, 28 (cont.)insert 28: split 4-node at root
once again, ___________ at the root maintains perfectbalance
2-3-4 Trees
27
complexity of 2-3-4 tree operationsthe height of an -node 2-3-4 tree is between
and
searching and inserting are both operationsrather than splitting 4-nodes on the way down, we couldalso perform _______________ insertion, starting at theinsertion node and moving upwardsdeletion involves ___________ nodes (and is also )
2-3-4 Trees as Red-Black Trees
28
red-black trees are a way of realizing 2-3-4 trees as binarysearch trees
allows us to re-use an implementation of a BST, andsimplifies _______________add the attribute of ________ (red or black) to links/nodes
2-3-4 Trees as Red-Black Trees
29
encoding 2-3-4 trees as red-black trees
2-3-4 Trees as Red-Black Trees
30
encoding 2-3-4 trees as red-black trees
2-3-4 Trees as Red-Black Trees
31
a red-black tree is a BST with the following properties:every node is either red or blackthe root is blackif a node is red, its children must be blackevery path from the root to a null link contains the samenumber of black nodes
in the encoding of 2-3-4 trees from red-black trees, the _____links in the red-black tree correspond to the links in the 2-3-4tree, while the ____ links denote a split of a 2-node or 3-nodecondition 4 corresponds to the perfect balance of 2-3-4 trees
the height of an -node red-black BST is at most ,so search, insertion, and deletion are operations
2-3-4 Trees as Red-Black Trees
32
building a red-black treein order to maintain perfect black balance, any new nodeadded to the tree must be redif the parent of the new node is black, all is wellif the parent of the new node is red, this violates thecondition that red nodes have only black children
fix with rotations similar to those for AVL and splay treescan be used to maintain the red-black structure at anypoint in the tree, not just at insertion of a new node
2-3-4 Trees as Red-Black Trees
33
top-down insertion: color flipswe will follow a top-down insertion scheme as we did with2-3-4 treesas we move down the tree to insert a node, if weencounter a node X with two red children, we make X redand its children black
if X is the root, we change the color back to blacka color flip can cause a red-black violation (a red child with
2-3-4 Trees as Red-Black Trees
34
color flips correspond to splitting 4-nodes
2-3-4 Trees as Red-Black Trees
35
red-black tree rotations
(or missing)
this is a single rotation and a color swap for P and G
2-3-4 Trees as Red-Black Trees
36
color flips correspond to splitting 4-nodes
2-3-4 Trees as Red-Black Trees
37
red-black tree rotations
(or missing)
this is a double rotation and a color swap for X and G
2-3-4 Trees as Red-Black Trees
38
color flips correspond to splitting 4-nodes
2-3-4 Trees as Red-Black Trees
39
red-black tree rotations
and its sibling are part of a ____________we split all the 4-nodes we encountered on the waydown
2-3-4 Trees as Red-Black Trees
40
example: GOTCHA
2-3-4 Trees as Red-Black Trees
41
example: GOTCHA (cont.)
2-3-4 Trees as Red-Black Trees
42
example: GOTCHA (cont.)
2-3-4 Trees as Red-Black Trees
43
example: GOTCHA (cont.)
the standard BST (rightmost) for GOTCHA is slightlyshorter
2-3-4 Trees as Red-Black Trees
44
example: ISOGRAM
2-3-4 Trees as Red-Black Trees
45
example: ISOGRAM
2-3-4 Trees as Red-Black Trees
46
example: ISOGRAM
2-3-4 Trees as Red-Black Trees
47
example: ISOGRAM
2-3-4 Trees as Red-Black Trees
48
example: ISOGRAM