Download presentation

Presentation is loading. Please wait.

Published bySofia Moreno Modified over 3 years ago

1
1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16

2
See: G. Adelson-Velskii and E.M. Landis, "An algorithm for the organization of information." Doklady Akademii Nauk SSSR, 146:263–266, 1962 (Russian). English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.RussianEnglish Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 458–475 of section 6.2.3: Balanced Trees. Note that Knuth calls AVL trees simply "balanced trees".Donald Knuth ISBN 0-201-89685-0 AVL Trees 2 ADS2 Lecture 16

3
AVL Trees Discovered in 1962, Adelson-Velskii and Landis –Trying to devise a better data structure for a chess engine! Depth ensured to be O(log n) –But not always perfectly balanced Recursive definition: –The sub-trees of every node differ in height by at most one. –Every sub-tree is an AVL tree. Height of an empty tree is -1 Height of a single node is 0 3 ADS2 Lecture 16

4
84 6 11 1 84 6 AVL tree not an AVL tree 3 117 1 84 6 3 7 1 84 6 2 AVL tree not an AVL tree 5 5 AVL tree 11 1 8 4 6 3 13 For more examples, see board 15 4 ADS2 Lecture 16

5
Operations on AVL trees Search, as for BSTs Insert is the same as for BST but –May have to fix the tree after insertion to restore the balance condition –Can always do this via a modification known as a rotation Single rotation or double rotation Similarly for deletion Note that the balance condition ensures that search,insert and delete operations are all O(log n), where n is the number of nodes balancing is O(1) 5 ADS2 Lecture 16

6
The basic idea of single rotation k1k1 k2k2 X Y Z k1k1 k2k2 X YZ In each case: k1

7
To rebalance a binary search tree rooted at r If too heavy on the left…right rotation If too heavy on the right… left rotation k1k1 r k2k2 T1T1 T2T2 T3T3 T4T4 k2k2 r T3T3 k1k1 T1T1 T2T2 T4T4 k1k1 r k2k2 T1T1 T2T2 T3T3 T4T4 k1k1 r T1T1 T2T2 k2k2 T3T3 T4T4 7 ADS2 Lecture 16

8
If node r is too heavy on the left, rotating about node r and left child: Rotate the whole subtree rooted at r round one place to the right Take the right subtree of new root and make it the left subtree of r If node r is too heavy on the right, rotating about node r and right child: Rotate the whole subtree rooted at r round one place to left Take the left subtree of new root and make it the right subtree of r Right rotation left rotation 8 ADS2 Lecture 16

9
Example: right rotation 10 3 5 1 24 3 5 1 2 4 See board for explanation Note that the first tree is not an AVL tree, but the second one is Rotation involves only a few pointer changes and allows us to fix the structure of a tree whilst preserving search tree property 9 ADS2 Lecture 16

10
Rotation continued Doesnt have to be done at the root of a tree; can be done at any node since that node is the root of some subtree. We are doing the balancing about this node 21 20 15 103 5 1 24 17 21 10 320 5 15 1 2 4 17 21 20 15 103 5 1 24 3 1 24 17 21 10 320 5 15 1 2 4 17 10 ADS2 Lecture 16

11
Applying single rotation – a worked example To maintain balance condition, every time you insert a new node: Starting at the node just inserted, travel up tree If we reach root node without finding a bad node, we are done If we reach bad node, do a rotation and we are done If tree is now balanced, continue.. (otherwise..what?) Start with an empty AVL tree and insert values 1 to 7 in sequential order then values 8 to 15 in reverse order 11 ADS2 Lecture 16

12
Worked example (adding nodes containing 1..7 in order) 1 1 2 1 2 3 Left rotation. (Node 2 has no left subtree to move) 2 3 1 balanced? Yes continue 1 is unbalanced now (too heavy on the right). 2 3 1 4 2 3 1 4 5 add 1 add 2 add 3 add 4 add 5 3 is unbalanced now (too heavy on the right). 12 ADS2 Lecture 16

13
Worked example contd. 2 3 1 4 5 Left rotation. (Node 4 has no left subtree to move) 2 4 1 3 5 balanced? Yes continue add 6 2 4 1 3 5 6 2 is unbalanced now (too heavy on the right). 13 ADS2 Lecture 16

14
Worked example contd. 2 4 1 3 5 6 Left rotation. (Node 4 has left subtree to move) 4 5 2 3 6 1 Example for you: add the next two nodes, i.e. node containing the value 7 and node containing the value 15 balanced? Yes continue add 7 and 15 4 6 2 57 3 1 15 14 ADS2 Lecture 16

15
Worked example contd. 4 6 2 57 3 1 15 add 14 4 6 2 57 3 1 15 14 7 is unbalanced now (too heavy on the right). Left rotation. 15 has a left subtree to move 4 6 2 5 15 3 1 7 14 4 6 2 57 3 1 15 14 balanced? No! What now? Answer: double rotation 15 ADS2 Lecture 16

16
AVL tree insert Let x be the deepest node where an imbalance occurs. Four cases to consider. The insertion is in the left subtree of the left child of x. right subtree of the left child of x. left subtree of the right child of x. right subtree of the right child of x. Cases 1 & 4 are solved by a single rotation. Cases 2 & 3 are solved by a double rotation. This is the situation we have here 16 ADS2 Lecture 16

17
Double rotation Let k 1 be the deepest node at which imbalance occurs k 2 and k 3 next two nodes along the path leading to the inserted node Then Rotate about k 2 and k 3 Rotate about k 1 and k 3 k1k1 k2k2 k3k3 4 6 2 57 3 1 4 6 2 57 3 1 14 15 k1k1 k3k3 k2k2 4 6 2 5 3 1 4 6 5 7 14 15 4 6 2 57 3 1 14 4 6 2 57 3 1 15 14 k2k2 k3k3 k1k1 Example: a right-left double rotation 17 ADS2 Lecture 16

18
Continuing the example add 13 4 6 2 5 3 1 4 6 5 7 14 15 4 6 2 5 3 1 4 6 5 7 14 15 13 6 is unbalanced now (too heavy on the right). New node is in the left subtree of the right child so double rotation required. 4 6 2 5 3 1 4 6 5 7 14 15 13 k3k3 k2k2 k1k1 right rotate left rotate 4 6 2 5 3 1 4 6 5 13 7 14 15 k3k3 k2k2 k1k1 4 2 3 1 4 7 6 13 5 14 15 k3k3 k2k2 k1k1 Again, right-left rotation required 18 ADS2 Lecture 16

19
Example continued 4 2 3 1 4 7 6 13 5 14 15 add 12 14 15 13 3 7 2 1 4 6 5 12 4 is unbalanced now (too heavy on the right). New node is in the right subtree of the right child so single (left) rotation required. 14 15 13 3 7 2 1 4 6 5 12 Left rotation. 7 has a left subtree to move 13 3 14 15 7 2 1 4 6 5 12 19 ADS2 Lecture 16

20
Example contd. Inserting 11, 10, 9 single rotation needed, inserting 8 requires no rotation. The resulting tree is: 10 3 12 14 7 2 1 4 6 5 9 8 11 13 15 Example for you: Show this is correct But we havent seen an example of a left-right double rotation! 20 ADS2 Lecture 16

21
A left-right rotation 10 3 12 14 7 2 1 4 6 5 9 8 11 13 15 Suppose we want to add a node containing the value 8.5 add 8.5 10 3 12 14 7 2 1 4 6 5 9 8 11 13 15 8.5 9 is unbalanced now (too heavy on the left). New node is in the right subtree of the left child so double rotation required. 21 ADS2 Lecture 16

22
Left-right rotation contd. Rotate left 10 3 12 14 7 2 1 4 6 5 9 8 11 13 15 8.5 k1k1 k3k3 k2k2 10 3 12 14 7 2 1 4 6 5 9 8.5 11 13 15 8 k2k2 k3k3 k1k1 10 3 12 14 7 2 1 4 6 5 8.5 11 13 15 8 Rotate right 9 See example sheet 3 for long worked examples of AVL trees 22 ADS2 Lecture 16

23
public class AVLNode { //Instance variables: private E element; private AVLNode left; private AVLNode right; int height; //methods } AVL implementation in Java See (outline of) AVLTree.java in BSTrees folder Need to keep a record of the height of a node –So AVLnode defined thus: whats this? See board 23 ADS2 Lecture 16 Height of node p = -1 if p is null 0 if p is leaf Max(height(p.L),height(p.R)) + 1 otherwise. Height of AVLTree = height of root.

24
AVL implementation contd. ADS2 Lecture 16 24 /**return height of tree rooted at p*/ public int height(AVLNode p){ } /**reset heights of all nodes starting at p */ public void resetHeights(AVLNode p){} Also (private static) max method to return max of two integers AVLTree.java includes methods:

25
AVLTree.java Deletion : Normal BSTree deletion does not maintain balance condition * Insert needs to call other methods –sRotateLeft –SRotate_right –dRotateLeftRight –dRotateRightLeft * But a proper implementation of delete should maintain the balance condition We dont look at all of these here, but will look at sRotateLeft and dRotateLeftRight 25 ADS2 Lecture 16

26
k1k1 k2k2 XY Z k1k1 k2k2 X YZ 26 ADS2 Lecture 16 public AVLNode sRotateLeft(AVLNode k1){ AVLNode k2=k1.getRight(); k1.setRight(k2.getLeft()); k2.setLeft(k1); k1.setHeight(max(height(k1.getLeft()), height(k1.getRight()))+1); k2.setHeight(max(height(k2.getRight()), height(k1))+1); return k2; }

27
k2k2 k1k1 A B D C k3k3 k2k2 k3k3 k1k1 B C D A k2k2 k1k1 B C D k3k3 A Examples, see board (if time) and Examples sheet 3 27 ADS2 Lecture 16 public AVLNode dRotateLeftRight(AVLNode k1){ AVLNode k2=k1.getLeft(); AVLNode k3=k2.getRight(); k1.setLeft(sRotateLeft(k2)); sRotateRight(k1); return k3; }

28
When do I need to do a rotation? ADS2 Lecture 16 28 During recursive implementation of insert for AVL tree (value e): When insertion has been performed to a left subtree of node p: if height of subtrees of p differ by 2, need rotation: if the new root of left subtree < e need a single right rotation else need a double left right rotation Similar argument for when insertion has been performed to a right subtree of node p. Only need to reset the heights of the tree when new node added, but no rotation required.

Similar presentations

OK

Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on care of public property tax Ppt on production and cost analysis Ppt on stone age tools Ppt on carburetor for sale Ppt on trial and error examples Ppt on condition monitoring job Ppt on real numbers for class 9th notes Ppt on eye os Ppt on ozone layer and greenhouse effect Ppt on cross-sectional study strengths and weaknesses