Top Banner
Data Structure and Algorithm (CS-102) Ashok K Turuk
54

Lecture 10 data structures and algorithms

Dec 14, 2014

Download

Education

Aakash Singhal

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 10 data structures and algorithms

Data Structure and Algorithm (CS-102)

Ashok K Turuk

Page 2: Lecture 10 data structures and algorithms

Consider the insertion of following element A, B, C, , ….,X, Y, Z into the BST

AB

C

X

Y

Z

O(N)

Page 3: Lecture 10 data structures and algorithms

Consider the insertion of following element Z, X, Y, , ….,C, B, A into the BST

Z

X

Y

C

B

A

O(N)

Page 4: Lecture 10 data structures and algorithms

Balanced binary tree

• The disadvantage of a binary search tree is that its height can be as large as N-1

• This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case

• We want a tree with small height• A binary tree with N node has height at least (log

N) • Thus, our goal is to keep the height of a binary

search tree O(log N)• Such trees are called balanced binary search

trees. Examples are AVL tree, red-black tree.

Page 5: Lecture 10 data structures and algorithms

AVL tree

Height of a node• The height of a leaf is 1. The height

of a null pointer is zero.• The height of an internal node is the

maximum height of its children plus 1

Page 6: Lecture 10 data structures and algorithms

AVL tree

• An AVL tree is a binary search tree in which– for every node in the tree, the height of

the left and right subtrees differ by at most 1.

– An empty binary tree is an AVL tree

Page 7: Lecture 10 data structures and algorithms

AVL treeTL left subtree of T h(TL ) Height of the subtree TL

TR Right subtree of T h(TR ) Height of the subtree TR

T is an AVL tree iff TL and TR are AVL tree and |h(TL ) - h(TR ) | <= 1

h(TL ) - h(TR ) is known as balancing factor (BF) and for an AVL tree the BF of a node can be either 0 , 1, or -1

Page 8: Lecture 10 data structures and algorithms

AVL Search Tree

C

G

D

A(0)

(0)

(-1)

(1)

Page 9: Lecture 10 data structures and algorithms

Insertion in AVL search Tree

Insertion into an AVL search tree may affect the BF of a node, resulting the BST unbalanced.

A technique called Rotation is used to restore he balance of the search tree

Page 10: Lecture 10 data structures and algorithms

AVL Search Tree

(0)

(0)

(-1)

(1)

C

G

D

A

E

Page 11: Lecture 10 data structures and algorithms

AVL Search Tree

(0)

(-1)

(-2)

(2)

C

G

D

A

E (0)

Page 12: Lecture 10 data structures and algorithms

Rotation

To perform rotation – Identify a specific node A whose BF(A) is neither 0, 1, or -1 and which is the nearest ancestor to the inserted node on the path from the inserted node to the root

Page 13: Lecture 10 data structures and algorithms

Rotation Rebalancing rotation are classified as LL,

LR, RR and RL LL Rotation: Inserted node is in the left

sub-tree of left sub-tree of node ARR Rotation: Inserted node is in the

right sub-tree of right sub-tree of node ALR Rotation: Inserted node is in the right

sub-tree of left sub-tree of node ARL Rotation: Inserted node is in the left

sub-tree of right sub-tree of node A

Page 14: Lecture 10 data structures and algorithms

LL Rotation

BL : Left Sub-tree of BBR : Right Sub-tree of BAR : Right Sub-tree of Ah : Height

A

B

c

AR

hBL BR

(0)

(+1)

Insert X into BL

A

B

c

AR

hBL BR

(+1)

(+2)

h+1 x

Unbalanced AVL search tree after insertion

Page 15: Lecture 10 data structures and algorithms

LL Rotation

LL Rotation

A

ARc h

BR

(0)B

BL

(0)

h+1 x

Balanced AVL search tree after rotation

A

B

c

AR

hBL BR

(+1)

(+2)

h+1 x

Unbalanced AVL search tree after insertion

Page 16: Lecture 10 data structures and algorithms

LL Rotation Example

Insert 36(0)

(+1)

96

85 110

64 90 (0)

(0)

(0)

(+1)

(+2)

96

85 110

64 90 (0)

(0)

(+1)

36

Unbalanced AVL search tree

(0)

Page 17: Lecture 10 data structures and algorithms

LL Rotation Example

LL Rotation

(+1)

(+2)

96

85 110

64 90 (0)

(0)

(+1)

36

Unbalanced VAL search tree

(0)

85

110

64

(0)

(+1) 96

(0)

(0)

Balanced AVL search tree after LL rotation

(0)

9036(0)

Page 18: Lecture 10 data structures and algorithms

RR Rotation

A

B

c

AL

h

BL BR

(0)

(-1)

Insert X into BR

h

A

B

c

BL BR

(-1)

(-2)

h+1x

Unbalanced AVL search tree after insertion

AL

Page 19: Lecture 10 data structures and algorithms

RR Rotation

RR Rotation

B

Ac

BL BR

(0)

(0)

h+1x

Balanced AVL search tree after Rotation

AL

A

B

c

BL BR

(-1)

(-2)

h+1x

Unbalanced AVL search tree after insertion

AL

h

Page 20: Lecture 10 data structures and algorithms

RR Rotation Example

Insert 65(0)

(-1)

34

26 44

40 56(0)

(0)

(0)

(0)

(-2)

34

26 44

40 56(-1)

(-1)

(0)

65

Unbalanced AVL search tree

(0)

Page 21: Lecture 10 data structures and algorithms

RR Rotation Example

RR Rotation (0)

(-2)

44

34 56

40 65(0)

(-1)

(0)26

Balanced AVL search tree after RR rotation

(0)

(0)

(-2)

34

26 44

40 56(-1)

(-1)

(0)

65(0)

Page 22: Lecture 10 data structures and algorithms

LR Rotation

Insert X into CL

B

C

c

BL

h-1

CL CR

(0)

(-1)

h

B

C

c

CL CR

(-1)

(-1)

x

Unbalanced AVL search tree after insertion

BL

A

h

A(+1)

AR

(+2)

AR

Page 23: Lecture 10 data structures and algorithms

LR Rotation

LR Rotation

B

C

c

BL

h

CL CR

(-1)

(-1)

h

BA

cCL

CR

(-1)(0)

x

Balanced AVL search tree after LR Rotation

BL

A

h

C(+2)

AR

(0)

AR

x

Page 24: Lecture 10 data structures and algorithms

LR Rotation Example

Insert 37(0)

(+1)

44

30 76

16 39 (0)

(0)

(0)

(-1)

(+2)

44

30 76

16 39 (+1)

(0)

(+1)

37

Unbalanced AVL search tree

(0)

Page 25: Lecture 10 data structures and algorithms

LR Rotation Example

LR Rotation

(-1)

(+2)

44

30 76

16 39 (+1)

(0)

(0)

(0)

(0)

39

30 44

16 37 (0)

(-1)

(0)

76

Balanced AVL search tree

(0)37

(0)

Page 26: Lecture 10 data structures and algorithms

RL Rotation

Insert X into CR

h

B

C

cAL h-1

CL CR

(0)

(0)

h

A(-1)

BR

h

B

C

cAL h-1

CL CR

(-1)

(+1)

h

A(-2)

x

Unbalanced AVL search tree after insertion

BR

Page 27: Lecture 10 data structures and algorithms

RL Rotation

RL Rotation

h

B

C

cAL h-1

CL CR

(-1)

(+1)

h

A(-2)

BR

x

Balanced AVL search tree after RL Rotation

x h

BA

c

AL

h-1

CLCR

(+1)

(0)

h

C(0)

BR

Page 28: Lecture 10 data structures and algorithms

RL Rotation Example

Insert 41(0)

(-1)

34

26 44

40 56(0)

(0)

(0)

(0)

(-2)

34

26 44

40 56(0)

(+1)

(-1)

41

Unbalanced AVL search tree

(0)

Page 29: Lecture 10 data structures and algorithms

RL Rotation Example

RL Rotation

(0)

(-2)

34

26 44

40 56(0)

(+1)

(-1)

(0)

(0)

(+1)

(0)

40

34 44

41 56(0)

(0)

(0)

Balanced AVL search tree

26

41

Page 30: Lecture 10 data structures and algorithms

AVL Tree

Construct an AVL search tree by inserting the following elements in the order of their occurrence

64, 1, 14, 26, 13, 110, 98, 85

Page 31: Lecture 10 data structures and algorithms

Insert 64, 1

Insert 14

64

1

(+1)

(0)

14

64

1

(+2)

(-1)

(0)

64

(0)14

1

(0)

(0)LR

Page 32: Lecture 10 data structures and algorithms

Insert 26, 13, 110,98

64

14

1

(0)

(0) (0)

64

14

1

(-1)

(-1) (-1)

98

11026

13 (+1)(0)

(0)

(0)

Page 33: Lecture 10 data structures and algorithms

Insert 85

64

14

1

(-2)

(-1) (-2)

98

11026

13(+2)

(0)

(+1)

(0)

(0)85

64

14

1

(-1)

(-1) (-1)

85

9826

13(0)

(0)

(0)

(0)

110

(0)

LL

Page 34: Lecture 10 data structures and algorithms

Deletion in AVL search TreeDeletion in AVL search tree proceed

the same way as the deletion in binary search tree

However, in the event of imbalance due to deletion, one or more rotation need to be applied to balance the AVL tree.

Page 35: Lecture 10 data structures and algorithms

AVL deletion

Let A be the closest ancestor node on the path from X (deleted node) to the root with a balancing factor +2 or -2

Classify the rotation as L or R depending on whether the deletion occurred on the left or right subtree of A

Page 36: Lecture 10 data structures and algorithms

AVL Deletion

Depending on the value of BF(B) where B is the root of the left or right subtree of A, the R or L imbalance is further classified as R0, R1 and R -1 or L0, L1 and L-1.

Page 37: Lecture 10 data structures and algorithms

R0 Rotation

A

B

c

AR

hBL BR

(0)

(+1)

Delete node X

hx

A

B

c

AR

hBL BR

(0)

(+2)

Unbalanced AVL search tree after deletion of node x

h -1

Page 38: Lecture 10 data structures and algorithms

R0 Rotation

R0 Rotation

Balanced AVL search tree after rotation

A

B

c

AR

hBL BR

(0)

(+2)

Unbalanced AVL search tree after deletion of x

A

ARc h

BR

(+1)B

BL

(-1)

BF(B) == 0, use R0 rotation

Page 39: Lecture 10 data structures and algorithms

R0 Rotation Example

Delete 60

Unbalanced AVL search tree after deletion

(0)

(0)

(+1)

46

20 54

18 23 (-1)

(-1)

(+1)

7(0) (0)

60

24

(0)

(+2)

46

20 54

18 23 (-1)

(0)

(+1)

7(0) (0)

24

Page 40: Lecture 10 data structures and algorithms

R0 Rotation Example

R0(0)

(+2)

46

20 54

18 23 (-1)

(0)

(+1)

7(0) (0)

24

Balanced AVL search tree after deletion

(+1)

(-1)

20

18 46

7 23(-1)

(+1)

(0)

54(0)

(0)

24

Page 41: Lecture 10 data structures and algorithms

R1 Rotation

A

B

c

AR

h -1BL BR

(+1)

(+1)

Delete node X

hxh

A

B

c

AR

h

BL BR

(+1)

(+2)

Unbalanced AVL search tree after deletion of node x

h -1

h -1

Page 42: Lecture 10 data structures and algorithms

R1 Rotation

R1 Rotation

Balanced AVL search tree after rotation

A

B

c

AR

h -1BL BR

(+1)

(+2)

A

ARc h-1

BR

(0)B

BL

(0)

BF(B) == 1, use R1 rotation

h

h -1

Page 43: Lecture 10 data structures and algorithms

R1 Rotation Example

Delete 39

Unbalanced AVL search tree after deletion

(0)

(+1)

(+1)

37

26 41

18 28(0)

(+1)

(+1)

16(0)

39

(+1)

(+2)

37

26 41

18 28 (0)

(0)

(+1)

16(0)

Page 44: Lecture 10 data structures and algorithms

R1 Rotation Example

R1 Rotation

Balanced AVL search tree after deletion

(+1)

(+2)

37

26 41

18 28 (0)

(0)

(+1)

16(0)

(+1)

(0)

26

18 37

16 28 (0)

(0)

(0)41

(0)

Page 45: Lecture 10 data structures and algorithms

R-1 Rotation

Delete X

B

C

c

BL

h-1

CL CR

(0)

(-1)

h-1

A

h

(+1)

AR

x

A

B

C

c

CL CR

(0)

(-1)

Unbalanced AVL search tree after deletion

BL

(+2)

AR

h-1

Page 46: Lecture 10 data structures and algorithms

R-1 Rotation

R -1

B

C

c

BL

h-1

CL CR

(0)

(-1)

h-1

A

h -1

(+2)

AR

C

BA

c

CLCR

(0)

(0)

Balanced AVL search tree after Rotation

BL

(0)

AR

h -1 h -1

BF(B) == -1, use R-1 rotation

Page 47: Lecture 10 data structures and algorithms

R-1 Rotation Example

Delete 52(-1)

(+1)

44

22 48

18 28 (0)

(-1)

(0)52

2923

(-1)

(+2)

44

22 48

18 28 (0)

(0)

(0)

23

Unbalanced AVL search tree after deletion

(0) 29

Page 48: Lecture 10 data structures and algorithms

R-1 Rotation Example

R-1(-1)

(+1)

44

22 48

18 28 (0)

(-1)

(0)52

2923

(0)

(0)

28

22

4818 23(0)

(0)

(0)

44

Balanced AVL search tree after rotation

(0)

29

Page 49: Lecture 10 data structures and algorithms

L0 Rotation

A

B

c

AL

h

BL BR

(0)

(-1)

Delete X

h

x

A

B

c

BL BR

(0)

(-2)

h

Unbalanced AVL search tree after deletion

AL

h-1

Page 50: Lecture 10 data structures and algorithms

L0 Rotation

A

B

c

AL

h

BL BR

(0)

(-1)

Delete X

h -1

B

Ac

BL

BR

(0)

(+1)

h

Balanced AVL search tree after deletion

AL

h-1

(-1)

Page 51: Lecture 10 data structures and algorithms

L1 Rotation

A

B

c

AL

h-1

CL

BR

(+1)

(-1)

Delete X

h

x C

CR

h-1

(0)

A

B

c

CL

BR

(+1)

(-2)

h-1

Unbalanced AVL search tree after deletion

AL

h-1

C

CR

(0)

Page 52: Lecture 10 data structures and algorithms

L1 Rotation

A

B

c

AL

h-1

CL

BR

(+1)

(-2)

L1

h-1

C

CR

h-1

(0)

(0)

A

C

B

c

CL

BR

(0)

(0)

h-1

Unbalanced AVL search tree after deletion

AL

h-1CR

Page 53: Lecture 10 data structures and algorithms

L-1 Rotation

A

B

c

AL

h

BL BR

(-1)

(-1)

Delete X

h

x

h-1

A

B

c

BL BR

(-1)

(-2)

h

Unbalanced AVL search tree after deletion

AL

h-1h-1

Page 54: Lecture 10 data structures and algorithms

L-1 Rotation

A

B

c

AL

h

BL BR

(-1)

(-2)

Delete X

h-1

B

A

c

BL BR

(-1)

(0)

h

Balanced AVL search tree after deletion

AL

h-1

h-1