1 CSC 2053 -TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS202 - Fundamental Structures of Computer Science II
Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:
1 AVL Trees Drozdek Section pages
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Binary Search Trees CSE 331 Section 2 James Daly.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Trees (Revisited) CHAPTER 15 6/30/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
Data Structures AVL Trees.
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Tree.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AVL Trees CSE, POSTECH.
AA Trees.
Binary Search Trees (Continued)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL-Trees (Part 1).
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Tree Balancing: AVL Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

1 CSC TREES AVL & BALANCED TREES

2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to O(log n) But what is a balanced tree? – A balanced binary tree is a binary tree where all leaves are within a specified distance from the root.

3 Balanced Trees Keeping a tree balanced guarantees an efficient search Why don't we do this then? Why don't we rebalance a tree every time a new node is inserted? – Because rebalancing can take up to O(n) operations.

4 Rebalancing a Binary Tree (example from Standish book)

Tree is now unbalanced

6 Rebalancing a Binary Tree (example from Standish book) Compare the two trees. Every single node changes its position Rebalancing can indeed be O(n)

7 The Cost of Rebalancing To include complete rebalancing as part of the insert function, insertion may take O(n) instead of O(log n) What we need is the guarantee that search will take O(log n) in the worst case Can we do better? Can we find a way to get O(log n) for searches and O(log n) for insertion?

8 AVL Trees In 1960, two Russian mathematicians Georgii Maksimovich Adelson-Velskii and Evgenii Mikhailovich Landis developed a technique for keeping a binary search tree balanced as items are inserted into it. A binary search tree that incorporate their techniques into the operations is called an AVL tree You will be asked to spell their names in the final exam.

9 AVL TREES An AVL tree is a balanced binary search tree where the height of the two subtrees (children) of a node differs by at most one. A binary search tree has the AVL property if the heights of the left and right subtrees are either equal or differ by 1, and both subtrees hold the AVL property

10 Examples of AVL Trees

11 Examples of AVL Trees – Unbalanced Page 11

12 Balance factors We judge a tree to be balanced using the balance factor of nodes. – The balance factor of node A is the difference between the height of the left and the height of the right subtrees – – the left minus the right A tree is said to be an AVL tree if all of its nodes have balance factors 1, 0 or

13 Example of Trees and their balance factors subtract # of levels in the Left sub tree from the # of levels in the Right subtree

14 Example of Trees and their balance factors

15 Example of Trees and their balance factors

16 Example of Trees and their balance factors

17 Example of Trees and their balance factors

18 Example of Trees and their balance factors

19 Balance Factors and insertion There are only two ways that a tree can become unbalanced and that is through insertion or deletion. Thus each time one of these operations is performed, the balance factors must be checked starting at the point of insertion or removal of node and working back up the tree. Because of the need to go back up the tree, AVL trees are usually implemented with a reference to the parent node kept inside each node.

20 Rotations The most important characteristic of AVL trees is that: which are called rotations. rebalancing can be achieved using one of four possible shape transformations which are called rotations. new node is inserted These rotations are used when a new node is inserted

21 Rotations – Single-right rotation: – Single-right rotation: This rotation is used when the new item is in the left subtree of the left child of a node that has a balance factor of 2 – the node’s left sub tree is too long – Single-left rotation – Single-left rotation: This rotation is used when the new item is in the right subtree of the right child of an ancestor with balance factor -2 the nodes right sub tree is too long.

22 ROTATIONS – Double-right rotation(Left Right): – Double-right rotation(Left Right): This rotation is used when the new item is in the right subtree of the left child of the nearest ancestor with balance factor -2 – Double-left rotation(Right Left:) – Double-left rotation(Right Left:) This rotation is used when the new item is in the left subtree of the right child of the nearest ancestor with balance factor 2

23 AVL Trees balance factor of a node is 2left sub-tree has a path that is too long If the balance factor of a node is 2, this means the left sub-tree has a path that is too long balance factor of the left child is 1 long path is the left sub-tree of the left child If the balance factor of the left child is 1, this means that the long path is the left sub-tree of the left child simple right rotation of the left child In this case, a simple right rotation of the left child around the original node will solve the imbalance

24 balance factor of node is 2 Single-Right Rotation- balance factor of node is 2 C2C2 B1B1 A C B A Initial Configuration Final Configuration Parent

25 Single-Right Rotation C2C2 B A link from the parent of C to B 1. Reset the link from the parent of C to B left link of C equal to the right link of B. ( currently null) 2. Set the left link of C equal to the right link of B. ( currently null) right link of B to point to C. 3. Set the right link of B to point to C. link from the parent of C to B 1. Reset the link from the parent of C to B left link of C equal to the right link of B. ( currently null) 2. Set the left link of C equal to the right link of B. ( currently null) right link of B to point to C. 3. Set the right link of B to point to C. Parent

26 Single-Right Rotation C B A null 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. 3. Set the right link of B to point to C. P Represents the right link of B

27 Single-Right Rotation C B A X 1. Reset the link from the parent of C to B. Set the left link of C equal to the right link of B. ( which is null) 2. Set the left link of C equal to the right link of B. ( which is null) 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B. Set the left link of C equal to the right link of B. ( which is null) 2. Set the left link of C equal to the right link of B. ( which is null) 3. Set the right link of B to point to C. P

28 Single-Right Rotation C B A 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. Set the right link of B to point to C. 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. Set the right link of B to point to C. 3. Set the right link of B to point to C. X P

29 Single-Right Rotation - Result C2 C2 B A C B A Initial Configuration Final Configuration parent

30 AVL Trees balance factor of a node is -2 If the balance factor of a node is -2, right sub-tree has a path that is too long this means the right sub-tree has a path that is too long balance factor of the right child is -1, long path is the right sub-tree of the right child Then if the balance factor of the right child is -1, this means that the long path is the right sub-tree of the right child simple left rotation of the right child In this case, a simple left rotation of the right child around the original node will solve the imbalance

31 Single-Left Rotation - Right subtree too long C F N Initial Configuration Final Configuration P C (-2) F N P

32 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. P

33 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. (which is null, for this example) 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. (which is null, for this example) 3. Set the left link of F to point to C. P

34 Single-Left Rotation C F N X 1. Reset the link from the parent of C to F Set the right link of C equal to the left link of F. 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F Set the right link of C equal to the left link of F. 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. P Represents the left link of F (null) Could have a value

35 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. Set the left link of F to point to C. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. Set the left link of F to point to C. 3. Set the left link of F to point to C. P

36 Single-Left Rotation C F N Initial Configuration Final Configuration C (-2) F N parent

37 AVL Trees If the balance factor of a node is 2, this means the left sub-tree has a path that is too long Then if the balance factor of the left child is -1, this means that the long path is the right sub-tree of the left child In this case, a leftright rotation will solve the imbalance

38 Double-Right Rotation(left-right rotation) M (2) F H M F H Initial Configuration Final Configuration P Parent

39 Double-Right Rotation(left-right rotation) M F H Reset the link from the parent of F(M) to H 1. Reset the link from the parent of F(M) to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. Reset the link from the parent of F(M) to H 1. Reset the link from the parent of F(M) to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P M points to H We need to rotate left around F so M points to H

40 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F (M)to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F (M)to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P M points to H

41 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F to H Set the left link of H to point to F. 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H Set the left link of H to point to F. 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P We now need a right rotation around H

42 Double-Right Rotation(left-right rotation) B 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. Reset the link from the parent of M (P) to H 3. Reset the link from the parent of M (P) to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. Reset the link from the parent of M (P) to H 3. Reset the link from the parent of M (P) to H 4. Set the right link of H to point to M. M F H P Make P reference H Make P reference H

43 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P

44 Final Configuation Tree rebalanced F H P M

45 L (-2) P1P1 S O M F 1.RIGHT- LEFT ROTATION Another implementation point P. left to right child of M point P. left to right child of M L(-2) P1 S M F 2. Then POINT PARENT OF P (L) TO M p.left

46 L(-2) M-2 P O S-0 F POINT PARENT OF P TO M Still unbalanced, need to rotate left around the M. RIGHT- LEFT ROTATION point P. left to right child of M point P. left to right child of M

47 L1 M0 P O S0 F Rotate Left

48 AVL Trees If the balance factor of a node is -2, this means the right sub- tree has a path that is too long Then if the balance factor of the right child is 1, this means that the long path is the left sub-tree of the left child In this case, a rightleft (Double Left) rotation will solve the imbalance Same as leftright in reverse.

49 Building an AVL tree P0 P0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

50 Building an AVL tree P1 P1 J0 J0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

51 Building an AVL tree P2 P2 J1 J1 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

52 Building an AVL tree P2 P2 J1 J1 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E The new item (B) is in the left subtree of a left child(J) of a node that has balance factor 2 (P) We need to apply a single-right rotation The new item (B) is in the left subtree of a left child(J) of a node that has balance factor 2 (P) We need to apply a single-right rotation

53 Building an AVL tree J0 J0 P0 P0 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

54 Building an AVL tree J1 J1 P0 P0 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E

55 Building an AVL tree J0 J0 P -1 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0

56 Building an AVL tree J0 J0 P0 P0 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M0 M0

57 Building an AVL tree J1 J1 P1 P1 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -1 O0 O0

58 Building an AVL tree J -2 P2 P2 B -1 D0 D0 I nsert Elements : P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O1 O1 N0 N0

59 Building an AVL tree J -2 P2 P2 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O1 O1 N0 N0 The new item (N) is in the left subtree of a right child(O) of a node that has balance factor -2 (M) We need to apply a rightleft rotation The new item (N) is in the left subtree of a right child(O) of a node that has balance factor -2 (M) We need to apply a rightleft rotation

60 Building an AVL tree J -2 P2 P2 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O0 O0 N First we need a right rotation around the O and then a left rotation around the M

61 Building an AVL tree J -1 P1 P1 B -1 D0 D0 Insert Elements : P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 Then a left rotation around the M

62 Building an AVL tree J -1 P1 P1 B0 B0 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0

63 Building an AVL tree J0 J0 P -1 B -1 D -1 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G0 G0

64 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 What rotation is necessary?

65 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 The new item (E) is in the left subtree of a right child(G) of a node that has balance factor -2 (D) We need to apply a rightleft rotation The new item (E) is in the left subtree of a right child(G) of a node that has balance factor -2 (D) We need to apply a rightleft rotation First a right rotation around the G

66 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 We execute a right rotation around the G and then a left around the D

67 Building an AVL tree J0 J0 P1 P1 B -1 E0 E0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G0 G0 D0 D0 We execute a right rotation around the G and then a left around the D

68 Rotations These were simple examples and were done when building a tree. In the homework, we will look at another way to rotate in the manner of the next slide when the tree already exists and needs to be fixed.

69 A leftright rotation - Easy way Rotate left around the 5Rotate right around the 7