avl tree n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
AVL Tree PowerPoint Presentation
Download Presentation
AVL Tree

Loading in 2 Seconds...

play fullscreen
1 / 35

AVL Tree - PowerPoint PPT Presentation


  • 133 Views
  • Uploaded on

AVL Tree. AVL Trees. Pencarian node pada unbalanced Binary Search Trees adalah tidak efisien . Worst case: operations take O(n). AVL ( Adelson-Velskii & Landis) trees adalah BST yang diseimbangkan

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'AVL Tree' - kaida


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
avl trees
AVL Trees
  • Pencarian node pada unbalanced Binary Search Trees adalahtidakefisien. Worst case: operations take O(n).
  • AVL (Adelson-Velskii & Landis) trees adalah BST yang diseimbangkan
  • Untuksetiap node pada tree, bedatinggiantara left subtreedan right subtreemaximum 1 saja.

X

X

H

H-2

H-1

avl trees1
AVL Trees

12

8

16

4

10

14

2

6

untuk setiap node pada tree beda tinggi antara left subtree dan right subtree maximum 1 saja
Untuk setiap node pada tree, beda tinggi antara left subtree dan right subtree maximum 1 saja.

AVL property violated here

avl trees2

10

10

5

20

5

20

43

3

3

1

2

1

3

AVL Trees

5

insertion for avl tree
Insertion for AVL Tree
  • After insert 1

12

8

16

4

10

14

2

6

1

insertion for avl tree1
Insertion for AVL Tree
  • Untuk memastikan AVL-tree dalam kondisi seimbang, setelah suatu node di-insert-kan, perlu dicek keseimbangan untuk tiap node
  • Jika setelah insertion ada node yang tidak seimbang, maka dilakukan operasi berikut :

Single rotation

Double rotation

insertions causing imbalance
Insertions Causing Imbalance

k2

k1

AVL Tree

HP=HQ=HR

  • Kemungkinan insertion yang menyebabkan ketidakseimbangan
    • Insertion pada P (outside) case1
    • Insertion pada Q (inside) case2

k1

k2

R

P

P

Q

Q

R

  • Kemungkinan insertion yang menyebabkanketidakseimbangan
    • Insertion pada Q (inside) case 3
    • Insertion pada R (outside) case 4
single rotation case 1
Single Rotation (case 1)

insertion pada A menyebabkan ketidakseimbangan

k2

k1

k1

k2

A

C

A

B

B

C

single rotation case 4
Single Rotation (case 4)

insertion pada C menyebabkan ketidakseimbangan

k1

k2

k2

k1

A

B

C

C

A

B

problem with single rotation
Problem with Single Rotation
  • Single rotation does not work for case 2 and 3 (inside case)

k2

k1

k1

k2

R

P

P

Q

Q

R

Insertion pada Q kemudian single rotation,

tetap tidak seimbang

double rotation
Double Rotation

k3

k2

k1

k1

k3

D

k2

A

B

C

D

A

B

C

double rotation1
Double Rotation

k1

k2

k3

k1

k3

A

k2

A

D

B

C

D

B

C

example

11

11

8

20

4

20

4

16

27

3

16

8

27

3

Example
  • Insert 3 into the AVL tree

8

example1

11

11

8

20

5

20

4

16

27

4

16

8

27

5

Example
  • Insert 5 into the AVL tree

8

avl trees exercise
AVL Trees: Exercise
  • Insertion order:
    • 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
remove operation in avl tree
Remove Operation in AVL Tree
  • Removing a node from an AVL Tree is the same as removing from a binary search tree. However, it may unbalance the tree.
  • Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node.
  • Use the appropriate single or double rotation to balance the tree.
  • May need to continue searching for unbalanced nodes all the way to the root.
deletion x in avl trees
Deletion X in AVL Trees
  • Deletion:
    • Case 1: if X is a leaf, delete X
    • Case 2: if X has 1 child, use it to replace X
    • Case 3: if X has 2 children, replace X with its inorder predecessor (and recursively delete it)
  • Rebalancing
delete 55 case 1
Delete 55 (case 1)

60

20

70

10

40

65

85

80

90

5

15

30

50

55

delete 55 case 11
Delete 55 (case 1)

60

20

70

10

40

65

85

80

90

5

15

30

50

55

delete 50 case 2
Delete 50 (case 2)

60

20

70

10

40

65

85

80

90

5

15

30

50

55

delete 50 case 21
Delete 50 (case 2)

60

20

70

10

40

65

85

50

80

90

5

15

30

55

delete 60 case 3

prev

Delete 60 (case 3)

60

20

70

10

40

65

85

80

90

5

15

30

50

55

delete 60 case 31
Delete 60 (case 3)

55

20

70

10

40

65

85

80

90

5

15

30

50

delete 55 case 3

prev

Delete 55 (case 3)

55

20

70

10

40

65

85

80

90

5

15

30

50

delete 55 case 31
Delete 55 (case 3)

50

20

70

10

40

65

85

80

90

5

15

30

delete 50 case 3

prev

Delete 50 (case 3)

50

20

70

10

40

65

85

80

90

5

15

30

delete 50 case 31
Delete 50 (case 3)

40

20

70

10

30

65

85

80

90

5

15

delete 40 case 3

prev

Delete 40 (case 3)

40

20

70

10

30

65

85

80

90

5

15

delete 40 rebalancing
Delete 40 : Rebalancing

30

20

70

10

Case ?

65

85

80

90

5

15

delete 40 after rebalancing
Delete 40: after rebalancing

30

10

70

20

65

85

5

80

90

15

Single rotation is preferred!

minimum element in avl tree
Minimum Element in AVL Tree
  • An AVL Tree of height H has at least FH+3-1 nodes, where Fi is the i-thfibonacci number
  • S0 = 1
  • S1 = 2
  • SH = SH-1 + SH-2 + 1

SH-1

SH-2

H

H-2

H-1

summary
Summary
  • Find element, insert element, and remove element operations all have complexity O(log n) for worst case
  • Insert operation: top-down insertion and bottom up balancing