avl tree example this is the example we did in tutorial on thursday
Download
Skip this Video
Download Presentation
AVL Tree Example (This is the example we did in tutorial on Thursday)

Loading in 2 Seconds...

play fullscreen
1 / 17

AVL Tree Example (This is the example we did in tutorial on Thursday) - PowerPoint PPT Presentation


  • 127 Views
  • Uploaded on

AVL Tree Example (This is the example we did in tutorial on Thursday). Slides by Jagoda Walny CPSC 335, Tutorial 02 Winter 2008. AVL trees are self-balancing binary search trees. Each node of an AVL tree is assigned a balance factor as follows:.

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 Example (This is the example we did in tutorial on Thursday)' - pearl-brock


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 tree example this is the example we did in tutorial on thursday

AVL Tree Example(This is the example we did in tutorial on Thursday)

Slides by Jagoda Walny

CPSC 335, Tutorial 02

Winter 2008

slide2
AVL trees are self-balancing binary search trees.

Each node of an AVL tree is assigned a balance factor as follows:

  • -1 if the depth of the node’s left subtree is 1 more than the depth of the right node’s left sub tree
  • +1 if the depth of the node’s left sub tree is 1 more than the depth of the right node’s left subtree
  • 0 if both of the node’s subtrees has the same depths
  • -2 if the depth of the node’s left subtree is 2 more than the depth of the right node’s left sub tree
  • +2 if the depth of the node’s left sub tree is 2 more than the depth of the right node’s left subtree
  • If the balance factor becomes -2 or +2, the tree must be rebalanced.

Next: Rotations -->

slide3
If the balance factor of a node in an AVL tree is +2 or -2, the node is rotated to re-balance the tree using one of the four cases shown in the following picture:

The 4 cases of AVL tree rebalancing using rotations. Picture created by Marc Tanti, licensed for reuse under the GNU Free Documentation License, Version 1.2

Next: example -->

slide4
Balance ok

+1

72

-1

25

-1

94

0

0

0

0

0

0

0

0

38

57

78

26

63

37

3

47

Initial AVL tree with balance factors:

Next step: insert 1 -->

slide5
Balance not ok

(Balance factor of -2 is not allowed)

-1

57

-1

+1

26

72

-2

25

-1

94

-1

0

0

0

0

0

47

3

38

78

63

37

0

1

Insert 1 and recalculate balance factors

Next step: Find rebalancing case -->

slide6
Balance not ok

-1

57

-1

+1

26

72

-2

25

0

0

0

0

0

78

37

63

38

47

-1

3

0

1

Find rebalancing case

Left Left Case

-1

94

Pivot

Next step: apply Left Left rebalancing -->

slide7
Balance ok

0

57

0

+1

26

72

0

25

0

-1

3

94

0

0

0

0

0

38

78

63

37

47

0

1

Rebalance and recalculate balance factors

Next step: insert 30 -->

slide8
Balance ok

-1

57

+1

+1

26

72

0

25

-1

38

0

0

0

63

3

78

0

1

-1

37

0

47

0

30

Insert 30 and recalculate balance factors

-1

94

Next step: Insert 32 -->

slide9
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

63

78

3

0

1

-2

37

0

47

+1

30

0

32

Insert 32 and recalculate balance factors

Next step: Find rebalancing case -->

slide10
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

3

63

78

0

1

-2

37

0

47

Find rebalancing case

+1

30

Left Right Case

0

32

Next step: Rebalance (Step 1) -->

slide11
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

63

78

3

0

1

-2

37

0

47

0

30

-1

32

Rebalance (Step 1)

Next step: Rebalance (Step 2) -->

slide12
Balance ok

-1

57

+1

+1

26

72

0

25

-1

-1

38

94

0

0

0

63

78

3

0

1

0

37

0

47

0

30

0

32

Rebalance (Step 2) and recalculate balance factors

Next step: Insert 35 -->

slide13
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

78

63

3

0

1

-1

37

+1

0

32

47

0

30

0

35

Insert 35 and recalculate balance factors

Next step: Find rebalancing case -->

slide14
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

3

78

63

0

1

+1

0

32

47

0

-1

30

37

0

35

Insert 35

Start from first spot (from bottom of tree) where balance factor is incorrect.

Left Right Case

Next step: Rebalance (Step 1) -->

slide15
Balance not ok

-2

57

+2

+1

26

72

0

25

-2

-1

38

94

0

0

0

78

63

3

0

1

0

0

32

47

0

-2

30

37

0

35

Rebalance (Step 1)

Next step: Rebalance (Step 2) -->

slide16
Balance ok

-1

57

+1

+1

26

72

0

25

+1

-1

38

94

0

0

0

78

63

3

0

1

0

0

32

47

0

0

30

37

0

35

Rebalance (Step 2)

Next step: Finished! -->

slide17
Balance ok

-1

57

+1

+1

26

72

0

25

+1

-1

38

94

0

0

0

3

63

78

0

1

0

0

32

47

0

30

0

35

Finished!

0

37

Exercise: insert 36

ad