Download presentation

Presentation is loading. Please wait.

Published byGerardo Pincock Modified over 2 years ago

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

2
© 2004 Goodrich, Tamassia AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes:

3
© 2004 Goodrich, Tamassia AVL Trees3 Height of an AVL Tree Fact: 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) = 2 For 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). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2 i n(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) 3 4 n(1) n(2)

4
© 2004 Goodrich, Tamassia AVL Trees4 Insertion in an AVL Tree Insertion is as in a binary search tree Always done by expanding an external node. Example: 44 1778 325088 4862 54 w b=x a=y c=z 44 1778 325088 4862 before insertionafter insertion

5
© 2004 Goodrich, Tamassia AVL Trees5 Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three b=y a=z c=x T0T0 T1T1 T2T2 T3T3 b=y a=z c=x T0T0 T1T1 T2T2 T3T3 c=y b=x a=z T0T0 T1T1 T2T2 T3T3 b=x c=ya=z T0T0 T1T1 T2T2 T3T3 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)

6
© 2004 Goodrich, Tamassia AVL Trees6 Insertion Example, continued 88 44 17 78 3250 48 62 2 4 1 1 2 2 3 1 54 1 T 0 T 1 T 2 T 3 x y z unbalanced......balanced T 1

7
© 2004 Goodrich, Tamassia AVL Trees7 Restructuring (as Single Rotations) Single Rotations:

8
© 2004 Goodrich, Tamassia AVL Trees8 Restructuring (as Double Rotations) double rotations:

9
© 2004 Goodrich, Tamassia AVL Trees9 Removal in an AVL Tree Removal 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 783250 88 48 62 54 44 17 7850 88 48 62 54 before deletion of 32after deletion

10
© 2004 Goodrich, Tamassia AVL Trees10 Rebalancing after a Removal Let 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 88 48 62 54 w c=x b=y a=z 44 17 78 5088 48 62 54

11
© 2004 Goodrich, Tamassia AVL Trees11 Running Times for AVL Trees a single restructure is O(1) using a linked-structure binary tree find is O(log n) height of tree is O(log n), no restructures needed insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) remove is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)

Similar presentations

OK

1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014

1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Free ppt on brand management Ppt on blood groups in humans Ppt on tcp/ip protocol suite free download Ppt on social problem in contemporary india Ppt on the art of war machiavelli Ppt on forest society and colonialism class 9 Ppt on hepatitis b vaccine Ppt on regional trade agreements of china Ppt on leadership styles theories Ppt on remote operated spy robot project