Download presentation

Presentation is loading. Please wait.

Published byDaquan Langley Modified about 1 year ago

1
Part II

3
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

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

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

6
Example of Case 2 Consider to delete 7 1010 2 1 8 1 0 9 5 0 3 1 6 4 0 7 0 1 12 0 10 2 1 8 -2 1 0 9 5 0 3 1 6 0 4 0 11 12 0 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 => R0. bf(B) = 1 => R1. bf(B) = –1 => R-1.

8
R0 Rotation Subtree height is unchanged. No further adjustments to be done. Similar to LL rotation. Before deletion. 0 A B BLBL BRBR ARAR hh h 1 B A After rotation. BRBR h A’ R h-1 BLBL h 1 A B BLBL BRBR A’ R After deletion. hh h-1 0 2

9
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 BLBL BRBR ARAR hh-1 h 1 B A After rotation. BRBR h-1 A’ R h-1 BLBL h 0 0 A B BLBL BRBR A’ R After deletion. hh-1 1 2

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. 1 A B BLBL CRCR ARAR h-1 h CLCL C b A B BLBL CRCR A’ R h-1 CLCL C 2 b C A CRCR A’ R h-1 B BLBL CLCL 0

11
L0 Rotation Subtree height is unchanged. No further adjustments to be done. Similar to RR rotation. Before deletion. A B BLBL BRBR ALAL hh h B A After rotation. A’ L h-1 BLBL h BRBR h 1 1 A B BLBL BRBR A’ L After deletion. hh h-1 -2 0 0

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. A B BRBR CRCR ALAL h-1 h CLCL C 1 b C B CRCR BRBR A A’ L h-1 CLCL 0 -2 A B BRBR CRCR A’ L h-1 CLCL C 1 b

13
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 BLBL BRBR ALAL h-1h h B A After rotation. ALAL h-1 BLBL BRBR h 0 0 A A’ L After deletion. h-1 -2 B BLBL BRBR h-1h

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.

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 49 2 1 5 8 3 11 12107 External nodes Extended binary search tree Internal nodes

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 7 18 38 15 15 13 14 17 19 20

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

22
Properties Since the height of a red-black tree is at most 2log 2 (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.44log 2 (n+2)) 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 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. ab c d gu pu u

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-node paths increases by 1.) Otherwise, continue color change or rotation. ab c d gu pu u ab c d gu pu u

27
Resolution of Type XYb Rotate Same as the rotation schemes taken for an AVL tree. For example, when dealing with LLb rotation, abcd y x z ab c d gu pu u x z y LLb u

28
Resolution of Type XYb LRb Rotation: same as LR rotation of AVL tree. RRb and RLb are symmetric. y a b z cd x b c a d gp pp p y x z LRb u

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

30
Insert 90 1080 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

31
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

32
u pu gu d 10 50 90 80 60 70 Insert 65 65 10 50 90 80 60 65 70 pu u gu

33
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

34
10 50 90 80 65 70 62 u pu gu d 60 1090 65 70 62 u 60 8050

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. 1090 65 70 62 60 8050 1090 65 62 py 60 8050 y

37
Delete A Black Node of Degree 1 Define y as the root of deficient subtree. py is parent of y. 1090 65 70 62 py 60 8050 y

38
Delete A Black Node of Degree 2 Not possible, degree 2 nodes are never deleted (Why?). 1090 65 70 62 60 8050

39
Rebalancing Strategy 1090 65 70 62 py 60 8050 y

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google