Attribution Non-Commercial (BY-NC)

16 views

Attribution Non-Commercial (BY-NC)

- 12-SplayTrees
- r059210503 Advanced Data Structure
- Admin Security Dept Mental Level Delegated Admin
- Tree Data Structure
- C and Data Structure notes
- Content for Programming Interviews Exposed, 3rd Edition
- D3804A15 DS 4
- Lab Manual - Ds
- datastructure-100211095005-phpapp02
- Short Online Q&A for DataStructure
- 10.1.1.160
- Untitled
- 10 Compact Routing
- CS1201data Structures
- Multiway Range Trees
- MM-Visualization Tools for Real-Time Search Algorithms
- 2 - Decision Tree
- Data Structures Aptitude
- SR_Containers in Object Oriented ABAP
- slides12-continuations.pdf

You are on page 1of 86

AVL trees

All BST operations are O(d), where d is tree depth minimum d is for a binary tree with N nodes

! l

AVL trees

root 2 4 6 8 10

AVL trees 3

The disadvantage of a binary search tree is that its height can be as large as N 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 5(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.

AVL trees 4

for every node in the tree, the height of the left and right subtrees differ by at most 1.

AVL trees

AVL trees are height-balanced binary search trees Balance factor of a node

For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node

AVL trees

Node Heights

Tree A (AVL) BF=2-1=1 1 Tree B (AVL) 2 0 1

6 9

0 0

6

0 0

4

0

4 1 5 8

AVL trees

Tree A (AVL) 2 1 Tree B (not AVL) 2-3 = -1 1

6

0 0

6

0 1

4

0

9

0

4 1 5

0

9

-1

8 7

AVL trees

AVL trees

Rotations

Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property.

AVL trees

10

Insertion

First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation.

AVL trees

11

Let the node that needs rebalancing be E. There are 4 cases: (require single rotation) : 1. Insertion into left subtree of left child of pivot node. 2. Insertion into right subtree of right child of pivot node. (require double rotation) : 3. Insertion into right subtree of left child of pivot node. 4. Insertion into left subtree of right child of pivot node. The rebalancing is performed through four separate rotation algorithms.

AVL trees 12

2-3 = 1 0 0 2-0 2 =2

6

0 1

6 4

0 0

4

0

9 5

0

8

0

8 7

AVL trees

13

Single rotations: insert 14, 15, 16, 13, 12, 11, 10

14

15

AVL trees 14

Single rotations:

14 15 16

Need to rotate.

AVL trees 15

Single rotations:

Rotation type:

14 15 16

AVL trees

16

Single rotations:

15

14

16

AVL trees

17

Single rotations:

15

14

16

13

12

AVL trees 18

Single rotations:

Rotation type:

15

14

16

13

12

AVL trees

19

Single rotations:

15

13

16

12

14

AVL trees 20

Single rotations:

15

13

16

12

14

11

AVL trees 21

Single rotations:

Rotation type:

13

15

16

12

14

11

AVL trees

22

Single rotations:

13

12

15

11

14

16

AVL trees 23

Single rotations:

13

12

15

11

14

16

10

AVL trees

24

Single rotations:

Rotation type:

13

12

15

11

14

16

10

AVL trees

25

Single rotations:

13

11

15

10

12

14

16

AVL trees 26

AVL tree

13

10

15

11

14

16

1

12

Now insert 4.

AVL trees 27

Double rotations:

13

10

15

11

14

16

12

4

AVL trees 28

Double rotations:

Rotation type:

13

10

15

11

14

16

12

4

AVL trees 29

Double rotations:

10 2 13

11

15

12

14

16

Now insert 5.

AVL trees 30

Double rotations:

10 2 13

3 4

11

15

12

14

16

5

AVL trees 31

Single rotations:

Rotation type:

10 2 13

3 4

11

15

12

14

16

AVL trees

32

Single rotations:

10 2 13

4 5

11

15

12

14

16

Now insert 7.

AVL trees 33

Single rotations:

10 2 13

4 5 7

11

15

12

14

16

AVL trees

34

Single rotations:

Rotation type:

10 2 13

4 5 7

11

15

12

14

16

AVL trees

35

Double rotations:

10 4 13

5 7

11

15

12

14

16

Now insert 6.

AVL trees 36

2

20

0 1 0

Insert 5, 40 0

10 25

30

35

AVL trees

37

2-2 = 0 2-3 = -1 1 0 1

20

1

20

-1

10

0

30

10

0

30 25

0 -1

25

35

5

Now Insert 45

35

0

40

AVL trees

38

3

20

1 2 1

20 30 25

0 0

10

0 0

30

-2 0

10 35

-1

25

Imbalance

5 40

0

40 1

0

35

Now Insert 34

45

45

AVL trees

39

AVL trees

40

AVL trees

41

Double rotations:

10 4 13

5 7

11

15

12

14

16

Now insert 6.

AVL trees 42

Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8

13

11

15

10

12

14

16

AVL trees

43

Double rotations:

13

11

15

10

12

14

16

2

AVL trees 44

Double rotations: L-R rotation

Rotation type: L-R rotation( insertion in right subtree of left child node of pivot node) 13

11

Pivot node

15

10

2nd

12

14

16

Left child

1st

2

AVL trees 45

Double rotations: L-R rotation

Rotation type: L-R rotation( insertion in right subtree of left child node of pivot node) 13

11

Pivot node

15

10

2nd

12

14

16

2 1

AVL trees 46

Double rotations:

13

11

15

12

14

16

1

10

Now insert 3.

AVL trees 47

Double rotations:

13

11

15

12

14

16

1 3

10

AVL trees

48

Double rotations:

child node of pivot node)

10 4

Pivot node

13

11

15

child

12

14

16

AVL trees

49

Double rotations:

child node of pivot node)

10 4

Pivot node

13

2 2nd 1 3

11

15

7

1st

child

12

14

16

AVL trees

50

Double rotations:

child node of pivot node)

10 4

Pivot node

13

2 2nd 1 3

11

15

child

12

14

16

AVL trees

51

Double rotations:

10 4 13

11

15

12

14

16

AVL trees 52

Double rotations:

10 4 13

11

15

12

14

16

9 8

AVL trees

53

Final tree:

10 4 13

11

15

12

14

16

AVL trees

54

3

20

1 -2 1

20 35

1

10

0 0 Imbalance

30

1 0

10 40 5 45 0 25 30

25

1 35

40 -1

0

34

45

Insertion of 34

34

AVL trees 55

An Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14

3 3 Fig 1 2 Fig 2 2 1 Fig 3 1 Fig 5 4

AVL trees

Single rotation

3 2

1 Fig 4

Single rotation

1 Fig 6

3

4

56

2 1 3 1

Single rotation

4

5 Fig 7 4 2 3

4

5 Fig 8 4

6

Single rotation

5

3 Fig 9 2 1 3 5 6 4 1

2 3

5

6 Fig 10 7

6

7 Fig 11

AVL trees 57

4 2 1 3 5 Fig 12 4

6

7 16

Double rotation

2 1 3 5 Fig 13 15

4 2

6

7 16 1 3

6

5 15 16

AVL trees

Fig 14

7

58

4 2 1 3

Double rotation

4 2 7 3 6 5 15

6

5 15 16 7 Fig 15 14 1

14 Fig 16

16

AVL trees

59

AVL trees

60

AVL trees 61

AVL trees 62

AVL trees

63

Restart our example Weve inserted 3, 2, 1, 4, 5, 6, 7, 16 Well insert 15, 14, 13, 12, 11, 10, 8, 9

4 2 1 3 5 6 7 k1 16 k3 1 2 3

4 6 5 7 Double rotation 15 k2 k1 15 k2 16 k3

AVL trees

64

4 2 1 3 A 5 k2 Insert 14 4 X 1 Insert 13 2 3 5 Y 6 14 13 Z k1 7 k2 15 16 1

AVL trees

4 6 k1 15 k3 7 16 14 C D 1 2 3 5 Double rotation 7 k2

6 k1 15 k3 14 16

7 4 2 3 6 5 14 13 Single rotation65 15 16

7 4 2 1 3 6 5 12 14 13 15 16 1 2 3 4 6 5

7 15 13 12 14 16

Insert 12

Single rotation 7 15 4 2 1

AVL trees

7 4 2 1 3 6 5 11 13 12 14

13 6 12 11 15 14 16

66

16

Insert 11

Single rotation

7 4 2 1 3 6 5 10 7 4 4 2 1 3 6 5 8 9 10 11 12 13 2 15 1 14 16 3 5 6 12 11 13 15 14 16 1 2 3 4 6 5

7 13 11 10 12 15 14 16

Insert 10

AVL trees

15 14 16

12 10

Double rotation

67

Insertion Analysis

logN

Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) Then trace the path from the new leaf towards the root, for each node x encountered: O(logN)

Check height difference: O(1) If satisfies AVL property, proceed to next node: O(1) If not, perform a rotation: O(1)

A rotation is performed Or, weve checked all nodes in the path

68

Searching an AVL tree is exactly the same as searching a regular binary tree

all descendants to the right of a node are greater than the node all descendants to the left of a node are less than the node

AVL trees

69

Note that the last (deepest) node in a tree deleted is a leaf or a node with one child

Then trace the path from the new leaf towards the root

For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.

AVL trees 70

AVL trees

71

Deletion

The only difference from insertion is that after we perform a rotation at x, we may have to perform a rotation at some ancestors of x. It may involve several rotations. Therefore, we must continue to trace the path until we reach the root. The time complexity to delete a node is dependent on the height of the tree, which is also O(log(n)).

AVL trees

72

Deletion Example 1

20 10 5 15 18 25 30 35 40 38 45 50 Delete 5, Node 10 is unbalanced Single Rotation 10 15 18 25 30 20 35 40 38 45 50

AVL trees

73

Contd

20 15 10 18 25 30 35 40 38 45 50 10 15 18 20 25 30 35 40 38 45 50

Single Rotation

For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node.

AVL trees 74

Deletion Example 2

Double rotation

AVL trees

Ok here!

75

Example 2 Contd

New case

AVL trees

76

Deletion : no rotation

No need to rotate.

AVL trees

77

AVL trees 78

AVL trees 79

Let Nh represent the number of nodes of an AVL tree of height h. Let nh be the lower bound Nh, i.e., Nh nh By definition, we have n1 = 1, and n2 = 2, and n3 = 4, etc, and nh+1 nh.

n1 = 1 15 10 n2 = 2 15 10 n3 = 4 15 20

13

AVL trees 80

Let x be the root of an AVL Tree. To keep the number of nodes in the AVL tree of height h small, one strategy is to make the height of left(x)=h-1 and the height of right(x)=h-2.

x

nh=nh-1+nh-2 +1

h nh-1 nh-2

AVL trees

81

We then have : nh = nh-1 + nh-2 + 1 2 nh-2 + 1 > 2 nh-2 > 22nh-4 By repeated substitution, we obtain the general form nh > 2i nh-2i

AVL trees

82

Nh nh Nh log2(Nh) h h

In other words, if n is the number of nodes of an AVL tree, then its height h is at most () O(log n). This implies that operations including search, insert, delete, successor, and predecessor on an AVL tree will take O(h)=O(log n).

AVL trees

83

find takes O(log n) time, because height of the tree is always O(log n). insert: O(log n) time because we do a find (O(log n) time), and then we may have to visit every node on the path back to the root, performing up to 2 single rotations (O(1) time each) to fix the tree. remove: O(log n) time. Left as an exercise.

AVL trees

84

guarantees the height of the AVL tree be O(log n) implies that functions search, min, and max, insert, and delete will be performed in O(logn)

Performs single or double rotation to restore the AVL tree property if necessary. Requires a little more work for insertion and deletion.

AVL trees 85

Arguments for AVL trees:

1. Search is O(log N) since AVL trees are always balanced. 2. Insertion and deletions are also O(logn) 3. The height balancing adds no more than a constant factor to the speed of insertion.

1. Difficult to program & debug; more space for balance factor. 2. Asymptotically faster but rebalancing costs time. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees).

AVL trees

86

- 12-SplayTreesUploaded byZaigham Abbas
- r059210503 Advanced Data StructureUploaded bySRINIVASA RAO GANTA
- Admin Security Dept Mental Level Delegated AdminUploaded bymohf
- Tree Data StructureUploaded bysel&nit
- C and Data Structure notesUploaded byNatwarr
- Content for Programming Interviews Exposed, 3rd EditionUploaded byzarickzheng
- D3804A15 DS 4Uploaded bySurendra Singh Chauhan
- Lab Manual - DsUploaded byMithun Aremmal
- datastructure-100211095005-phpapp02Uploaded byamritamurali
- Short Online Q&A for DataStructureUploaded bynikunjpatel90
- 10.1.1.160Uploaded byrabik1234
- UntitledUploaded bydeepak9471
- 10 Compact RoutingUploaded bysumukha1999
- CS1201data StructuresUploaded byssrizwan
- Multiway Range TreesUploaded bysud_mishra
- MM-Visualization Tools for Real-Time Search AlgorithmsUploaded byLados Ludomir
- 2 - Decision TreeUploaded bybandaru_jahnavi
- Data Structures AptitudeUploaded bySundeep Chopra
- SR_Containers in Object Oriented ABAPUploaded byRagini Palluru
- slides12-continuations.pdfUploaded by252966576
- Algo-ds Important QnsUploaded byryu447
- Coding QuestionsUploaded byMukesh Panch
- Superimposition- A Language-Independent Approach to Software CompositionUploaded byGeorge
- TreesUploaded byAnjaney Sharma
- Fully Persistent ListsUploaded byGeorge
- C Programs FinalUploaded bySanJeev Dani PeDrosa
- TreePlan-204-Example-Windows.xlsxUploaded byRyu Kurosaki
- PrecisionTree5 EnUploaded byJorgito Arias
- 282373599-Cocos2d-Programguide.pdfUploaded byloicuoi
- treeUploaded byamit patel

- Lecture Unit1 McaUploaded byAnilkumar Patil
- Buffer Overflow by Alfred ChinUploaded byalfred_chin_2
- Ableton Live Tips and Tricks Part 3Uploaded byPatrick Ijsselstein
- 260-Photoshop Module v7c-PC for WEB-2.pdfUploaded byRaquel Javinez
- Operating System HandoutUploaded byKapil Kaul
- Flashing UIQ3 OfflineUploaded byyulistana
- tps3809l30Uploaded byToni Biosca
- EAM-INT-325-TS-Document Management interface (7).docxUploaded byMounica Vendra
- Communication CoDeSysUploaded bycastro2160
- 850pm2.0Uploaded bykingjiaozi
- Attacking SDN InfrastructureUploaded byTấn Long Lê
- rp matrixUploaded byjohn
- Bussmann Advanced Guide to SCCRsUploaded byBrian Burke Sr.
- Preview of “Convert ANY file to your iPod format using VLC ... Complete GRAPHiCAL GUiDE ...”Uploaded byAshwini Anaokar Shah
- Amcas Instruction ManualUploaded byBrad Isaacs
- Instructions to AuthorsUploaded byscarlogarcia
- Troubleshooting Guide 474283Uploaded byأبو عبد الله عسالي
- Downloadconfigfile.confUploaded byArielJuneAceroAndrada
- Lecture 1Uploaded byNikhil Agarwal
- JIT Prod MgmtUploaded byShreesh Chandra
- Headline Magazine: Fall 2012Uploaded byBrainstreams
- ISCO Theories of Meaning and Perspectives on the Design of Info SystemsUploaded byahmadmedo
- P543&5_EN_TM_M.pdfUploaded byNgoc Neo
- Epicor Storefront 8 Release GuideUploaded byMarius Sirbu
- SRAM Characterization (1).pdfUploaded bySazzad Hossain
- EC8103-DIGITAL VIDEO SIGNAL CODING.docUploaded byMegha Srivastava
- Monika Nanaware ResumeUploaded byMonika Nanaware
- Sagar.NARESH.s-1.docxUploaded byNaresh Naidu
- Call Processing and L3 MessagesUploaded bylatestjobsinpakistan
- my resumeUploaded byAnonymous Nk3I9T

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.