AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9....

15
AVL Trees 1 © 2004 Goodrich, Tamassia AVL Trees 6 3 8 4 v z

Transcript of AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9....

Page 1: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 1© 2004 Goodrich, Tamassia

AVL Trees6

3 8

4

v

z

Page 2: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 2© 2004 Goodrich, Tamassia

AVL Tree Definition (§ 9.2)

AVL trees are balanced.An AVL Tree is a binary search treesuch that for every internal node v of T, the heights of the children of v can differ by at most 1.

88

44

17 78

32 50

48 62

2

4

1

1

2

3

1

1

An example of an AVL tree where the heights are shown next to the nodes:

Page 3: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 3© 2004 Goodrich, Tamassia

Height of an AVL TreeFact: The height of an AVL tree storing n keys is O(log n).Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h.We easily see that n(1) = 1 and n(2) = 2For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2.That is, n(h) = 1 + n(h-1) + n(h-2)Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). Son(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),n(h) > 2in(h-2i)

Solving the base case we get: n(h) > 2 h/2-1

Taking logarithms: h < 2log n(h) +2Thus the height of an AVL tree is O(log n)

3

4 n(1)

n(2)

Page 4: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 4© 2004 Goodrich, Tamassia

Insertion in an AVL TreeInsertion is as in a binary search treeAlways done by expanding an external node.Example: 44

17 78

32 50 88

48 62

54w

b=x

a=y

c=z

44

17 78

32 50 88

48 62

before insertion after insertion

Page 5: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 5© 2004 Goodrich, Tamassia

Trinode Restructuring

let (a,b,c) be an inorder listing of x, y, zperform the rotations needed to make b the topmost node of the three

b=y

a=z

c=x

T0

T1

T2 T3

b=y

a=z c=x

T0 T1 T2 T3

c=y

b=x

a=z

T0

T1 T2

T3

b=x

c=ya=z

T0 T1 T2 T3

case 1: single rotation(a left rotation about a)

case 2: double rotation(a right rotation about c, then a left rotation about a)

(other two cases are symmetrical)

z is the first unbalanced nodey and x are child and grandchildof z and ancestor or wa,b,c is in-order ordering of x,y,z

Page 6: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 6© 2004 Goodrich, Tamassia

Insertion Example, continued

88

44

17 78

32 50

48 62

2

5

1

1

3

4

2

1

54

1

T0T2

T3

x

y

z

2

3

4

5

67

1

88

44

17

7832 50

48

622

4

1

1

2 2

3

154

1

T0 T1

T2

T3

x

y z

unbalanced...

...balanced1

23

4

5

6

7

T1

green numbersare in-order traversal orderblack and red numbers areheights

z is first unbalanced node

Page 7: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 7© 2004 Goodrich, Tamassia

Restructuring (as Single Rotations)

Single Rotations:

T0T1

T2

T3

c = xb = y

a = z

T0 T1 T2

T3

c = xb = y

a = zsingle rotation

T3T2

T1

T0

a = xb = y

c = z

T0T1T2

T3

a = xb = y

c = zsingle rotation

Page 8: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 8© 2004 Goodrich, Tamassia

T0T1

T2

T3

c = xb = y

a = z

T0 T1 T2

T3

c = xb = y

a = zsingle rotation

a=z

T0

T1

T3

T2w

b=y

c=x

z is the first unbalanced nodey and x are child and grandchildof z and ancestor or wa,b,c is inorder ordering of x,y,z

Single Rotations:Case 1(b is son of a andfather of c)

Page 9: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 9© 2004 Goodrich, Tamassia

T3T2

T1

T0

a = xb = y

c = z

T0T1T2

T3

a = xb = y

c = zsingle rotation

a=x

T0

T2

T3

T1w

b=y

c=z

z is the first unbalanced nodey and x are child and grandchildof z and ancestor or wa,b,c is inorder ordering of x,y,z

Single Rotations:Case 2(b is son of cand father of a)

Page 10: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 10© 2004 Goodrich, Tamassia

Restructuring (as Double Rotations)

double rotations:

double rotationa = z

b = xc = y

T0T2

T1

T3 T0

T2T3T1

a = zb = x

c = y

double rotationc = z

b = xa = y

T0T2

T1

T3 T0

T2T3 T1

c = zb = x

a = y

Page 11: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 11© 2004 Goodrich, Tamassia

double rotationa = z

b = xc = y

T0T2

T1

T3 T0

T2T3T1

a = zb = x

c = y

a=z

T0

T2

T3

T1w

b=x

c=y

z is the first unbalanced nodey and x are child and grandchildof z and ancestor or wa,b,c is inorder ordering of x,y,z

Double Rotations:Case 1(b is grandson of aand son of c)

Page 12: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 12© 2004 Goodrich, Tamassia

a=y

T3

T2

T0

T1w

b=x

c=z

z is the first unbalanced nodey and x are child and grandchildof z and ancestor or wa,b,c is inorder ordering of x,y,z

Double Rotations:Case 2(b is grandson of cand son of a)

double rotationc = z

b = xa = y

T0T2

T1

T3 T0

T2T3 T1

c = zb = x

a = y

Page 13: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 13© 2004 Goodrich, Tamassia

Removal in an AVL TreeRemoval begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance.Example:

44

17

7832 50

8848

62

54

44

17

7850

8848

62

54

before deletion of 32 after deletion

Page 14: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 14© 2004 Goodrich, Tamassia

Rebalancing after a RemovalLet z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height.We perform restructure(x) to restore balance at z.As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached

44

17

7850

8848

62

54

w

c=x

b=y

a=z

44

17

78

50 88

48

62

54

Page 15: AVL Trees - Carleton Universitypeople.scs.carleton.ca/~sbtajali/2002/slides/09-2... · 2009. 9. 15. · © 2004 Goodrich, Tamassia AVL Trees 2 AVL Tree Definition (§ 9.2) AVL trees

AVL Trees 15© 2004 Goodrich, Tamassia

Running Times for AVL Trees

a single restructure is O(1)n using a linked-structure binary tree

find is O(log n)n height of tree is O(log n), no restructures needed

insert is O(log n)n initial find is O(log n)n Restructuring up the tree, maintaining heights is O(log n)

remove is O(log n)n initial find is O(log n)n Restructuring up the tree, maintaining heights is O(log n)