Download presentation
Presentation is loading. Please wait.
Published byPenelope Wendy Melton Modified over 9 years ago
1
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8
2
Motivation of Red-Black Trees Balanced and Unbalanced Trees AVL Trees Definition of Red-Black Trees Operations for Red-Black Trees Search Insertion
3
2020 3030 3535 2525 1010 1515 5 Balanced and Unbalanced Trees
4
AVL-Tree The balance factor of a node is the height of its left subtree minus the height of its right subtree A node with balance factor 1, 0, or -1 is considered balanced 6060 4040 5050 3030 Balance Factor: 2-0=2 Balance Factor: 2-0=2 Balance Factor: 0-1=-1 Balance Factor: 0-1=-1 4545 Unbalanced
5
AVL-Tree: Rotation Operation
6
Left Right Case 6060 4040 5050 3030 4545 6060 4040 5050 3030 4545 6060 4040 5050 3030 4545
7
Definition: a binary tree, satisfying: 1. Every node is colored either red or black 2. The root is black 3. If a node is red, its children must be black ▪ consecutive red nodes are disallowed 4. Every path from a node to a null reference must contain the same number of black nodes
8
The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 30 15 550 10 70 8560 4055 809065 20
9
Each path must contain the same number of black nodes. (Rule #4) Consecutive red nodes are not allowed. (Rule #3) The longest path is at most twice the length of the shortest path
10
B = total black nodes from root to leaf N = total all nodes H = height All operations guaranteed logarithmic!
11
A new node must be colored red Why? ▪ A new item is always inserted as a leaf in the tree ▪ If we color a new item black, then the number of black nodes from root would be different (violate property #4) If the parent is black, no problem. If the parent is red, we create two consecutive red nodes (violate property #3) ▪ Thus, we have to do some rotating/recolouring…
12
AB G X S P C DE AB G X S P C DE X: new node P: parent S: sibling G: Grandparent Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “outer node” (left-left or right-right)
13
BC G X S P A DE AB G P S X C DE X: new node P: parent S: sibling G: Grandparent Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “inner node” (left-right or left)
14
Case after insertion: Consecutive red Sibling of parent is red Outer node (left-left or right-right) AB G X SP C DE AB G X S P C DE But what if P’s parent is red? We have to keep going up the tree all the way to the root
15
The solution: prevent S from ever being red! Starting from the root (searching for insertion point) Never allow 2 red siblings If we see a node X with 2 red children, do a colour flip. X C2C1 X C2
16
Maintains property #4 Possible violation of #3: if X’s parent is red! ▪ Do single or double rotation ▪ X’s parent’s sibling can never be red! Set the root to black (to maintain property #2) X C2C1 X C2
17
AB G X SP C DE AB G X SP C DE If we do the colour flipping on the way down to the insertion point, we will never reach a condition where P & S are red!
18
30 15 5 10 70 85 60 50 55 8090 65 20 40 18
19
30 15 5 10 70 85 60 50 55 8090 65 20 40 18 2
20
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10
21
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10 45
22
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10 45 Color-flip!
23
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10 45 Color-flip!
24
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10 45
25
30 15 2 5 70 85 60 50 55 8090 65 20 40 18 10 45
26
The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
27
Deletion in BST: only leaf nodes or nodes with one child are really deleted (Why?) If the deleted node is red: no problem (all properties maintained). Leaf nodes: Single child nodes:
28
If node to be deleted is black violate property #4 Always ensure that the node to be deleted is red. Top-down traversal from root (looking for node to be deleted): X: visited node P: parent S: sibling B P S X A CD Idea: make sure that X is red!
29
P is red (inductive invariant) X and S are black (result of property #3) 2 cases: 1. Both X’s children (A & B) are black 2. X has at least one red child (A, B, or both) B P S X A CD
30
Depends on children of S (C & D): Both C & D are black: simply colour-flip: B P S X A C D B P S X A C D
31
Case 1: Both X’s children are black Outer child of S (C) is Red: do a single rotation and recolour B P S X A C D C S D P B X A
32
Inner child of S (C) is Red: do a double rotation and recolour B P S X A D C C S P B X A D
33
Recurse down to X’s child If we land on a red node, fine. If we land on a black node, rotate sibling and parent: B P S X A C C S P B X A D D
34
Red-Black trees use color as balancing information instead of height in AVL trees. An insertion may cause a local perturbation (two consecutive red nodes) The pertubation is either resolved locally (rotations), or propagated to a higher level in the tree by recoloring (color flip) O(1) for a rotation or color flip At most one restructuring per insertion. O(log n) color flips Total time: O(log n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.