Download presentation
Presentation is loading. Please wait.
Published byBrooke Strickland Modified over 9 years ago
1
Red–black trees
2
Define the red-black tree properties Describe and implement rotations Implement red-black tree insertion We will skip red-black tree deletion October 2004John Edgar2
3
Items can be inserted in and removed from BSTs in O(height) time So what is the height of a BST? If the tree is balanced: O( logn) If the tree is very unbalanced: O(n) October 2004John Edgar3 43612461123712244337 balanced BST height = O(logn) balanced BST height = O(logn) unbalanced BST height = O(n) unbalanced BST height = O(n)
4
Define a balanced binary tree as one where There is no path from the root to a leaf* that is more than twice as long as any other such path The height of such a tree is O(logn) Guaranteeing that a BST is balanced requires either A more complex structure (2-3 and 2-3-4 trees) or More complex insertion and deletion algorithms (red- black trees) October 2004John Edgar4 *definition of leaf on next slide
5
A red-black tree is a balanced BST Each node has an extra colour field which is red or black ▪ Usually represented as a boolean – isBlack Nodes have an extra pointer to their parent Imagine that empty nodes are added so that every real node has two children They are imaginary nodes so are not allocated space The imaginary nodes are always coloured black October 2004John Edgar5
6
1. Every node is either red or black 2. Every leaf is black This refers to the imaginary leaves ▪ i.e. every null child of a node is considered to be a black leaf 3. If a node is red both its children must be black 4. Every path from a node to a leaf contains the same number of black nodes 5. The root is black (mainly for convenience) October 2004John Edgar6
7
Perfect trees are perfectly balanced But they are inflexible, can only store 1, 3, 7, … nodes “Black” nodes form a perfect tree “Red” nodes allow flexibility Draw some pictures October 2004John Edgar7
8
The black height of a node, bh(v), is the number of black nodes on a path from v to a leaf Without counting v itself Because of property 4 every path from a node to a leaf contains the same number of black nodes The height of a node, h(v), is the number of nodes on the longest path from v to a leaf Without counting v itself From property 3 a red node’s children must be black ▪ So h(v) 2(bh(v)) October 2004John Edgar8
9
It can be shown that a tree with the red-black structure is balanced A balanced tree has no path from the root to a leaf that is more than twice as long as any other such path Assume that a tree has n internal nodes An internal node is a non-leaf node, and the leaf nodes are imaginary nodes A red-black tree has 2 bh – 1 internal (real) nodes ▪ Can be proven by induction (e.g. Algorithms, Cormen et al.) October 2004John Edgar9
10
CClaim: a red-black tree has height, h 2*log(n+1) nn 2 bh – 1 (see above) bbh h / 2 (red nodes must have black children) nn 2 h/2 – 1 (replace bh with h) llog(n + 1) h / 2 (add 1, log 2 of both sides) hh 2*log(n + 1) (multiply both sides by 2) October 2004John Edgar10
12
An item must be inserted into a red-black tree at the correct position The shape of a tree is determined by The values of the items inserted into the tree The order in which those values are inserted This suggests that there is more than one tree (shape) that can contain the same values A tree’s shape can be altered by rotation while still preserving the bst property Note: only applies to bst with no duplicate keys! October 2004John Edgar12
13
October 2004John Edgar13 x yz ADCB Left rotate(x) z y A D C B x
14
October 2004John Edgar14 x yz ADCB zy ADCB Right rotate(z) x
15
October 2004John Edgar15 Left rotation of 32, call the node x 47813213403744 Assign a pointer to x's R child Assign a pointer to x's R child temp
16
October 2004John Edgar16 47813213403744 temp Make temp’s L child x’s R child Make temp’s L child x’s R child Detach temp’s L child Detach temp’s L child Left rotation of 32, call the node x Assign a pointer to x's R child
17
October 2004John Edgar17 47813213403744 temp Make x temp's L child Make x temp's L child Make temp x's parent's child Make temp x's parent's child Make temp’s L child x’s R child Detach temp’s L child Left rotation of 32, call the node x Assign a pointer to x's R child
18
October 2004John Edgar18 47814032441337 Left rotation of 32, call the node x
19
October 2004John Edgar19 478132134072937 temp Right rotation of 47, call the node x Assign a pointer to x's L child
20
October 2004John Edgar20 478132134072937 Make temp’s R child x’s L child Make temp’s R child x’s L child Detach temp’s R child Detach temp’s R child temp Right rotation of 47, call the node x Assign a pointer to x's L child
21
Right rotation of 47, call the node x October 2004John Edgar21 478132134072937 Make x temp's L child Make x temp's L child temp Make temp’s R child x’s L child Detach temp’s R child Assign a pointer to x's L child
22
October 2004John Edgar22 Make temp the new root Make temp the new root temp 473213740293781 Right rotation of 47, call the node x Make x temp's L child Make temp’s R child x’s L child Detach temp’s R child Assign a pointer to x's L child
24
Insert as for a binary search tree Make the new node red October 2004John Edgar24
25
October 2004John Edgar25 Insert 65 Insert 65 477132 93
26
October 2004John Edgar26 Insert 65 47713265 93
27
Insert as for a binary search tree Make the new node red What can go wrong? (see slide 6) The only property that can be violated is that both a red node’s children are black (its parent may be red) So, after inserting, fix the tree by re-colouring nodes and performing rotations October 2004John Edgar27
28
The fixing of the tree remedies the problem of two consecutive red nodes There are a number of cases (that’s what is next) It is iterative (or recursive) and pushes this problem one step up the tree at each step I.e. if the consecutive red nodes are at level d, at the next step they are at d-1 This is why it turns out to be O(log n) ▪ We won’t go into the analysis October 2004John Edgar28
29
Need to fix tree if new node’s parent is red Case I for fixing: If parent and uncle are both red Then colour them black And colour the grandparent red ▪ It must have been black beforehand, why? October 2004John Edgar29
30
October 2004John Edgar30 Insert 65 47713265 93 Insert 82 Insert 82
31
October 2004John Edgar31 82 47713265 93 Insert 82 Insert 65
32
71 93 65 October 2004John Edgar32 82 4732 Insert 82 change nodes’ colours Insert 65
33
Need to fix tree if new node’s parent is red Case II for fixing: If parent is red but uncle is black Need to do some tree rotations to fix it October 2004John Edgar33
34
Insert 87 Insert 87 October 2004John Edgar34 659371 82 4732 Insert 82 Insert 65
35
October 2004John Edgar35 936571 82 473287 Insert 87 Insert 82 Insert 65
36
October 2004John Edgar36 936571 82 473287 Insert 87 Insert 82 Insert 65
37
October 2004John Edgar37 936571 87 473282 Insert 87 Insert 82 Insert 65
38
93 87 October 2004John Edgar38 6547328271 change nodes’ colours Insert 87 Insert 82 Insert 65
39
October 2004John Edgar39 87936547328271 Insert 87 Insert 82 Insert 65
40
Why were these rotations performed? First rotation made the two red nodes left children of their parents This rotation isn’t performed if this is already the case Note that grandparent must be a black node Second rotation and subsequent recolouring fixes the tree October 2004John Edgar40
41
Full details require a few cases See link to example code snippets at end Understand the application of tree rotations October 2004John Edgar41
43
Red-black trees are balanced binary search trees Augment each node with a colour Maintaining relationships between node colours maintains balance of tree Important operation to understand: rotation Modify tree but keep binary search tree property (ordering of nodes) October 2004John Edgar43
44
For implementation details, please see: http://en.wikipedia.org/wiki/Red-black_tree (see “Operations”) October 2004John Edgar44
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.