Part II. Delete an Node from an AVL Tree Consider to delete 4 10 2 1 8 1 0 9 5 0 3 1 6 4 0 7 0 11 12...
-
Upload
daquan-langley -
Category
Documents
-
view
218 -
download
1
Transcript of Part II. Delete an Node from an AVL Tree Consider to delete 4 10 2 1 8 1 0 9 5 0 3 1 6 4 0 7 0 11 12...
Part II
Delete an Node from an AVL TreeConsider to delete 4
10
-1
2
1
8
-1
1
0
9
5
0
3
1
6
-1
4
0
7
0
11
-1
12
0
-1
Delete an Node from an AVL TreeLet q be parent of deleted node. Retrace path from q towards root.
10
-1
2
1
8
-1
1
0
9
5
0
3
2
6
-1
4
7
0
11
-1
12
0
-1
q
New Balance Factor of qDeletion from left subtree of q
bf--Deletion from right subtree of q
bf++Case1: New bf = 1 or –1
no change in height of subtree rooted at q.Case 2: New bf = 0
height of subtree rooted at q has decreased by 1. Retrace path back to the root, update the bf, and do rebalancing if
necessary.
Case 3: New balance factor = 2 or –2tree is unbalanced at q.
Perform rebalancing.
q
Example of Case 2Consider to delete 7
10
-1
2
1
8
-1
1
0
9
5
0
3
1
6
-1
4
0
7
0
11
-1
12
0
-1
10
-1
2
1
8
-2
1
0
9
5
0
3
1
6
0
4
0
11
-1
12
0
-1
If new bf is 0, retrace the path back to the root and update the bf.
Imbalance ClassificationLet A be the nearest ancestor of the deleted node
whose balance factor has become 2 or –2 following a deletion.
Deletion from left subtree of: type L.Deletion from right subtree of A: type R.Take type R for instance: new bf(A) = 2.
Therefore, old bf(A) = 1.Suppose A has a left child B.
bf(B) = 0 => R0. bf(B) = 1 => R1. bf(B) = –1 => R-1.
R0 Rotation
Subtree height is unchanged.No further adjustments to be done.
Similar to LL rotation.
Before deletion.
0
A
B
BL BR
AR
h h
h
1 B
A
After rotation.
BR
hA’Rh-1
BL
h
-1
1
A
B
BL BR
A’R
After deletion.
h h
h-10
2
R1 Rotation
Similar to LL and R0 rotations.Subtree height is reduced by 1.
Therefore, we must continue on path to root.
Before deletion.
1
A
B
BL BR
AR
h h-1
h
1 B
A
After rotation.
BR
h-1
A’R
h-1
BL
h
0
0
A
B
BL BR
A’R
After deletion.
h h-1
h-1
1
2
R-1 Rotation
Similar to LR.New balance factor of A and B depends on b.
Subtree height is reduced by 1.Therefore, we must continue on path to root.
1A
B
BL
CR
AR
h-1
h
CL
C
-1
b
A
B
BL
CR
A’R
h-1
h-1
CL
C
2
-1
b
C
A
CR A’Rh-1
B
BL
h-1
CL
0
L0 Rotation
Subtree height is unchanged.No further adjustments to be done.
Similar to RR rotation.
Before deletion.
A
B
BL BR
AL
h h
h
-1 B
A
After rotation.
A’Lh-1
BL
h
BR
h
1
1
A
B
BL BR
A’L
After deletion.
h h
h-1
-2
00
L1 Rotation
Similar to RL.New balance factor of A and B depends on b.
Subtree height is reduced by 1.Therefore, we must continue on path to root.
-1A
B
BR
CR
AL
h-1
h
CL
C
1
b
C
B
CR BR
h-1
A
A’L
h-1
CL
0
-2A
B
BR
CR
A’L
h-1
h-1
CL
C
1
b
L-1 Rotation
Similar to RR rotations.Subtree height is reduced by 1.
Therefore, we must continue on path to root.
Before deletion.
A
B
BL BR
AL
h-1 h
h
-1
-1
B
A
After rotation.
AL
h-1
BL
h-1
BR
h
0
0
A
A’L
After deletion.
h-1
-2
B
BL BR
h-1 h
-1
Number Of Rebalancing RotationsAt most O(log n) for one insertion.
At most O(log n) to update bf.O(log n) for one deletion.
Red-Black TreeA red-black tree is an extended binary search tree
that satisfies the following two definitions:Each node/pointer is colored red or black.
Colored nodes definition Colored edges definition 6
4 9
2
1
5 8
3
11
12107
External nodes
Extended binary search tree
Internal nodes
IntroductionColored Nodes Definition
RB1: The root and all external nodes are black.RB2: No root-to-external-node path has two
consecutive red nodes.RB3: All root-to-external-node paths have the same
number of black nodes
IntroductionColored Edges Definition
RB1’: Pointer to an external node is black.RB2’: No root to external node path has two
consecutive red pointers.RB3’: Every root to external node path has the same
number of black pointers.
Example Red-Black Tree10
7 18
3 8 15
1 513
14
17
19
20
PropertiesLet the rank of a node be the number of black
pointers on any path from the node to any external node.
Suppose that the rank of the root is r.From RB2’, each red pointer is followed by a black
pointer. Therefore, each root-to-external-node path has
between r and 2r.
PropertiesLemma 10.2
Let h be the height of a red-black tree (excluding the external nodes), let n be the number of internal nodes, and let r be the rank of the root.a) h 2r≦b) n 2≧ r-1
c) h 2log≦ 2(n+1)
From b), we have r log≦ 2(n+1). This equality together with a) yields c).
From b), we have r log≦ 2(n+1). This equality together with a) yields c).
PropertiesSince the height of a red-black tree is at most
2log2(n+1), search, insert, and delete algorithms that work in O(h) time have complexity O(log n).
Notice:The worst-case height of a red-black tree is more than
the worst-case height (approximately 1.44log2(n+2)) of an AVL tree with the same number of internal nodes.
Inserting into a RED-Black TreeElements is first inserted using the strategy used for
ordinary binary tree.New node color options.
Black node: one root-to-external-node path has an extra black node (violation to RB3). Hard to remedy.
Red node: one root-to-external-node path may have two consecutive red nodes may or may not violate to RB2
Can be remedied by color flips and/or a rotation. We will make the new node red.
Classification of Two Red NodesLL type
LL type u is red, pu is red and gu is black (gu must be black. Why?) LLr: the other child of gu, d, is red. LLb: the other child of gu, d, is black.
a b
c
d
gu
pu
u
Classification of Two Red NodesType XYz
X: relationship between gu and pu. For example, if pu is the left child of gu, then X = L.
Y: relationship between pu and u. For example, if u is the right child of pu, then Y = R.
z: b (black) if d is black. r (red) if d is red.
Resolution of Type XYrColor change
Move u, pu, and gu up two levels. gu becomes new u.
Continue rebalancing if necessary. If RB2 is satisfied, stop propagation. If gu is the root, forced gu to be black (The number of black nodes for all
root-to-external-node paths increases by 1.) Otherwise, continue color change or rotation.
a b
c
d
gu
pu
u
a b
c
d
gu
pu
u
Resolution of Type XYbRotate
Same as the rotation schemes taken for an AVL tree.For example, when dealing with LLb rotation,
a b c d
y
x z
a b
c
d
gu
pu
u x
z
y LLb
u
Resolution of Type XYbLRb Rotation: same as LR rotation of AVL tree.
RRb and RLb are symmetric.
y
a b
z
c d
x
b c
a
d
gp
pp
py
x
z
LRb
u
Example 10.4Insert 50, 10, 80, 90, 70, 60, 65, 62
50
Insert 50
Insert 10
50
Insert 80
10 10 80
50
Insert 90
10 80
50
90
u
pu
gu
d
10
50
90
u
pu
gu
d
80 10
50
90
u
pu
gu
d
80
This violates RB1
10
50
90
u
pu
gu
d
Insert 70
70
80
10
50
90
Insert 60
70
80
60
u
pu
10
50
90
80
60
70
u
pu
gu
d
10
50
90
80
60
70
Insert 65
65
10
50
90
80
60
65
70
pu
u
gu
10
50
90
80
60
65
70
Insert 62
62
u
pu
gu
d
10
50
90
80
65
70
Insert 62
62
u
pu
gu
d
60
10
50
90
80
65
70
62
u
pu
gu
d
60
10 90
65
70
62
u
60
8050
Deletion from a Red-Black TreeDelete as for unbalanced binary search tree.If red node deleted, no rebalancing needed.If black node deleted, a subtree becomes one black
pointer (node) deficient.
Delete A Black LeafDefine y as the root of deficient subtree.
py is parent of y.
10 90
65
70
62
60
8050
10 90
65
62
py
60
8050
y
Delete A Black Node of Degree 1Define y as the root of deficient subtree.
py is parent of y.
10 90
65
70
62
py
60
8050
y
Delete A Black Node of Degree 2Not possible, degree 2 nodes are never deleted
(Why?).
10 90
65
70
62
60
8050
Rebalancing Strategy
10 90
65
70
62
py
60
8050
y
Define the root of deficient subtreey is root of deficient subtree. py is parent of y.