Chapter 10 Efficient Binary Search Trees - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Chapter 10 Efficient Binary Search Trees

Description:

Delete an Node from an AVL Tree. Let q be parent of deleted node. ... Case1: New bf = 1 or 1. no change in height of subtree rooted at q. Case 2: New bf = 0 ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 41
Provided by: tcu3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10 Efficient Binary Search Trees


1
Chapter 10Efficient Binary Search Trees
  • Part II

2
Delete an Node from an AVL Tree
3
Delete an Node from an AVL Tree
  • Consider to delete 4

-1
5
-1
1
8
3
1
-1
-1
0
10
2
6
4
0
0
0
-1
1
9
7
11
0
12
4
Delete an Node from an AVL Tree
  • Let q be parent of deleted node.
  • Retrace path from q towards root.

-1
5
-1
2
q
8
3
-1
1
-1
10
2
6
4
0
0
0
-1
1
9
7
11
0
12
5
New Balance Factor of q
  • Deletion 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 2
  • tree is unbalanced at q.
  • Perform rebalancing.

6
Example of Case 2
  • Consider to delete 7

If new bf is 0, retrace the path back to the root
and update the bf.
7
Imbalance Classification
  • Let 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 gt R0.
  • bf(B) 1 gt R1.
  • bf(B) 1 gt R-1.

8
R0 Rotation
  • Subtree height is unchanged.
  • No further adjustments to be done.
  • Similar to LL rotation.

9
R1 Rotation
  • Similar to LL and R0 rotations.
  • Subtree height is reduced by 1.
  • Therefore, we must continue on path to root.

10
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.

11
L0 Rotation
A
-1
  • Subtree height is unchanged.
  • No further adjustments to be done.
  • Similar to RR rotation.

B
0
AL
h
BL
BR
h
h
Before deletion.
12
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.

13
L-1 Rotation
  • Similar to RR rotations.
  • Subtree height is reduced by 1.
  • Therefore, we must continue on path to root.

Before deletion.
14
Number Of Rebalancing Rotations
  • At most O(log n) for one insertion.
  • At most O(log n) to update bf.
  • O(log n) for one deletion.

15
Red-Black Tree
16
Red-Black Tree
  • A 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
8
11
5
Internal nodes
1
3
12
10
7
External nodes
Extended binary search tree
17
Introduction
  • Colored 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

18
Introduction
  • Colored 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.

19
Example Red-Black Tree
10
18
7
19
15
3
8
20
17
13
1
5
14
20
Properties
  • Let 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.

21
Properties
  • Lemma 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.
  • h ? 2r
  • n ? 2r-1
  • h ? 2log2(n1)

From b), we have r?log2(n1). This equality
together with a) yields c).
22
Properties
  • Since the height of a red-black tree is at most
    2log2(n1), 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(n2)) of an AVL tree with the same
    number of internal nodes.

23
Inserting into a RED-Black Tree
  • Elements 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.

24
Classification of Two Red Nodes
  • LL 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.

25
Classification of Two Red Nodes
  • Type 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.

26
Resolution of Type XYr
  • Color 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-nod
    e paths increases by 1.)
  • Otherwise, continue color change or rotation.

27
Resolution of Type XYb
  • Rotate
  • Same as the rotation schemes taken for an AVL
    tree.
  • For example, when dealing with LLb rotation,

u
LLb
28
Resolution of Type XYb
  • LRb Rotation same as LR rotation of AVL tree.
  • RRb and RLb are symmetric.

u
LRb
29
Example 10.4
  • Insert 50, 10, 80, 90, 70, 60, 65, 62

50
50
50
10
10
80
Insert 50
Insert 10
Insert 80
30
This violates RB1
gu
gu
50
gu
50
50
pu
d
pu
d
pu
d
10
80
10
80
10
80
u
u
u
90
90
90
Insert 90
31
50
gu
80
10
d
pu
90
70
50
u
60
10
80
pu
50
u
90
70
Insert 60
80
10
90
70
60
Insert 70
32
gu
50
50
pu
80
80
10
10
gu
u
90
70
90
65
pu
d
60
60
70
u
65
Insert 65
33
gu
50
50
pu
d
80
10
80
10
u
gu
90
65
90
65
pu
d
70
60
60
70
u
62
62
Insert 62
Insert 62
34
gu
u
50
65
pu
d
80
10
80
50
u
90
65
10
90
70
60
70
60
62
62
35
Deletion from a Red-Black Tree
  • Delete 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.

36
Delete A Black Leaf
  • Define y as the root of deficient subtree.
  • py is parent of y.

65
65
py
80
50
80
50
y
10
90
70
60
10
90
60
62
62
37
Delete A Black Node of Degree 1
  • Define y as the root of deficient subtree.
  • py is parent of y.

65
py
80
50
10
90
70
60
y
62
38
Delete A Black Node of Degree 2
  • Not possible, degree 2 nodes are never deleted
    (Why?).

65
80
50
10
90
70
60
62
39
Rebalancing Strategy
65
py
80
50
10
90
70
60
y
62
40
Define the root of deficient subtree
  • y is root of deficient subtree.
  • py is parent of y.
Write a Comment
User Comments (0)
About PowerShow.com