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

40
Part II

Upload: daquan-langley

Post on 14-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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 0

Part II

Page 2: 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 0
Page 3: 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 0

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

Page 4: 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 0

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

Page 5: 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 0

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

Page 6: 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 0

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.

Page 7: 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 0

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.

Page 8: 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 0

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

Page 9: 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 0

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

Page 10: 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 0

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

Page 11: 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 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

Page 12: 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 0

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

Page 13: 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 0

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

Page 14: 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 0

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.

Page 15: 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 0
Page 16: 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 0

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

Page 17: 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 0

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

Page 18: 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 0

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.

Page 19: 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 0

Example Red-Black Tree10

7 18

3 8 15

1 513

14

17

19

20

Page 20: 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 0

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.

Page 21: 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 0

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

Page 22: 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 0

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.

Page 23: 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 0

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.

Page 24: 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 0

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

Page 25: 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 0

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.

Page 26: 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 0

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

Page 27: 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 0

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

Page 28: 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 0

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

Page 29: 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 0

Example 10.4Insert 50, 10, 80, 90, 70, 60, 65, 62

50

Insert 50

Insert 10

50

Insert 80

10 10 80

50

Page 30: 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 0

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

Page 31: 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 0

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

Page 32: 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 0

u

pu

gu

d

10

50

90

80

60

70

Insert 65

65

10

50

90

80

60

65

70

pu

u

gu

Page 33: 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 0

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

Page 34: 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 0

10

50

90

80

65

70

62

u

pu

gu

d

60

10 90

65

70

62

u

60

8050

Page 35: 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 0

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.

Page 36: 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 0

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

Page 37: 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 0

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

Page 38: 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 0

Delete A Black Node of Degree 2Not possible, degree 2 nodes are never deleted

(Why?).

10 90

65

70

62

60

8050

Page 39: 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 0

Rebalancing Strategy

10 90

65

70

62

py

60

8050

y

Page 40: 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 0

Define the root of deficient subtreey is root of deficient subtree. py is parent of y.