1 AVL Trees Drozdek Section 6.7.2 pages 257 -262.

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 (10.2) CSE 2011 Winter April 2015.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
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.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
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:
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.
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:
C++ Programming:. Program Design Including
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
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.
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.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
1 More Trees II Trees, Red-Black Trees, B Trees.
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.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
1 More Trees Trees, Red-Black Trees, B Trees.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Tree.
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.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AVL Trees CSE, POSTECH.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Trees Chapter 15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
Copyright © Aiman Hanna All rights reserved
Tree Rotations and AVL Trees
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Lecture 9: Self Balancing Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
Tree Balancing: AVL Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

1 AVL Trees Drozdek Section pages

2 Objectives You will be able to Describe an AVL tree. Describe the operations by which an AVL tree is maintained as nearly balanced as items are added. Add a new node to the diagram of an AVL tree.

3 Tree Balancing Consider a BST of state abbreviations. If the states are entered in the order NY, IL, GA, RI, MA, PA, DE, IN, VT, TX, OH, WY. the tree will be balanced. When tree is nicely balanced, search time is O(log 2 n)

4 Tree Balancing: AVL Trees If abbreviations entered in increasing order DE, GA, IL, IN, MA, MI, NY, OH, PA, RI, TX, VT, WY BST degenerates into linked list with search time O(n)

5 AVL Tree A height balanced tree Tree is rebalanced after additions and deletions make it skewed beyond a certain limit. Search time is O(log 2 n) Named after its two inventors: G.M. Adelson-Velskii and E.M. Landis G.M. Adelson-VelskiiE.M. Landis Adelson-Velskii, G.; E. M. Landis (1962). "An algorithm for the organization of information". Proceedings of the USSR Academy of Sciences 146: 263–266. (Russian) English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.Proceedings of the USSR Academy of Sciences

6 Balance Factor The Balance Factor of a node is defined as height of its left subtree minus height of its right subtree. Note: Some authors define balance factor as right - left. Drozdek defines balance factor as right - left. Examples in this presentation are from a different book. A balance factor with absolute value greater than 1 is unacceptable for an AVL tree.

7 AVL Tree Definition Defined as Binary search tree Balance factor of each node is 0, 1, or -1 Key is to rebalance the tree whenever an insertion or a deletion creates a balance factor with absolute value greater than 1 at any node.

8 AVL Tree as an ADT The differences between an AVL tree and the Binary Search Tree that we have studied previously are purely internal. As seen from the outside they are identical.

9 Examples: AVL Trees Nyhoff page 841 Numbers in the circles are the balance factors, not the values stored in the nodes.

10 Examples: Not AVL Trees Nyhoff page 841

11 Insertions may require adjustment of the tree to maintain balance. Given tree Becomes unbalanced Requires a right rotation on subtree RI Producing balanced tree AVL Trees Insert DE

12 Observation Inserting a new item into an AVL tree can result in a subtree having a balance factor (absolute value) greater than 1. But no more than 2. Inserting a new item does not necessarily increase the balance factor. May leave it unchanged. May decrease it. If the balance factor is unacceptable after an insertion, it can be fixed by one of four possible rebalancing rotations.

13 Examples Let’s look at how we can rebalance the tree while adding states. We will add states to the tree in the order RI, PA, DE, GA, OH, MA, IL, MI, IN, NY, VT, TX, WY Start by adding RI to an empty tree.

14 Add PA Tree is still balanced.

15 Add DE Tree is now unbalanced. A right rotation of RI will restore balance.

16 Rotations AVL tree rotations rearrange links so as to reduce the height of the larger subtree at an unbalanced node. They always preserve the BST invariants: Everything in a node's left subtree is less than or equal to it. Everything in a node's right subtree is greater than or equal to it.

17 Add GA Tree is still balanced.

18 Add OH The subtree rooted at DE is now unbalanced. A left rotation of DE will restore balance.

19 Add MA The subtree rooted at PA is now unbalanced. We need to do a right rotation at PA. But we can’t do that immediately with a simple right rotation. We need to do a left rotation at GA first. This is a left-right rotation.

20 After Left Rotation at GA Now we can do a right rotation at PA obtaining a balanced tree.

21 Insert IL and MI Tree is still balanced.

22 Insert IN The subtree rooted at GA is now unbalanced. Again we need a double rotation to restore balance. Start with a right rotation at MA.

23 After Right Rotation at MA Now we can do a left rotation at GA, restoring balance.

24 Insert NY The subtree rooted at OH is now unbalanced. We will need a left-right double rotation in the left subtree of OH. Notice what happens to IN!

25 After Left Rotation at IL Now we can do a right rotation at OH, restoring balance.

26 Insert VT Produces an inbalance at PA, which can be removed with a simple left rotation to yield:

27 Insert TX and WY This does not unbalance the tree, resulting in the final version.

28 Observations When an unacceptable imbalance was produced, we always did a rotation at the first ancestor of the added node having an unacceptable imbalance. We always rotated that node away from side with the larger subtree height. Its child with the larger subtree height takes its place and it becomes a new child of that node. If the first two steps from the first unblanced ancestor toward the added node were in the same direction, a single rotation restored balance. Otherwise, a double rotation was necessary.

29 Rebalancing Rotations 1. Single right rotation Use when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the left subtree of its left child. 2. Single left rotation Use when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in right subtree of its right child. 3. Left-right rotation Use when first unacceptably unbalanced ancestor has a balance factor of +2 and the inserted item is in the right subtree of its left child. 4. Right-left rotation Use when first unacceptably unbalanced ancestor has a balance factor of -2 and the inserted item is in the left subtree of its right child.

30 Single Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the left subree of its left child. First two steps from ancestor to inserted node are both to the left. Shown graphically on next slide. Described in detail on the slide following that.

31 Single Right Rotation Note that the right subtree of B R (B) flips over to become the left subtree of A.

32 Single Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the left subree of its left child. Let A = nearest out of balance ancestor of inserted item B = left child of A Set link from parent of A to point to B. Set left link of A to point to the right child of B. Set right link of B to point to A

33 Single Left Rotation Symmetrical with single right rotation. Exchange “right” and “left” in description of single right rotation.

34 Single Left Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of -2 (heavy to the right) and the inserted node is in the right subree of its rightchild. Let A = nearest out of balance ancestor of inserted item B = right child of A Set link from parent of A to point to B. Set right link of A to point to the left child of B. Set left link of B to point to A

35 Left-Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the right subree of its left child. First two steps from ancestor to inserted node are in opposite directions (left then right). Shown graphically on following slides Described in detail on slides following them.

36 Left-Right Rotation Nyhoff page 851 Suppose we insert a new node into the left subtree of C, increasing the height of that subtree.

37 Left-Right Rotation Do a left rotation at B (left child of first out-of-balance ancestor of added node.)

38 Left-Right Rotation Now do a right rotation at A. Note that the left subtree of C L (C) flips over to become the right subtree of B.

39 Left-Right Rotation The right subtree of C R (C) has become the left subtree of A.

40 Left-Right Rotation Used when the nearest out of balance ancestor of the inserted node has a balance factor of +2 (heavy to the left) and the inserted node is in the right subree of its left child. Let A = nearest ancestor of the inserted item. B = left child of A C = right child of B New item added to subtree rooted at C. First do a left rotation of B and C. 1. Set left link of A to point to C. 2. Set right link of B equal to left link of C 3. Set left link of C to point to B Now a right rotation of A and C. 1. Reset link from parent of A to point to C 2. Set left link of A equal to right link of C 3. Set right link of C to A

41 Right-Left Rotation The Right-Left Rotation is symmetrical. In the description of the Left-Right rotation, exchange “right” and “left”.

42 Summary Adding nodes to a binary tree can result in an unbalanced tree. But we can restore balance with one of four kinds of rotations. Single Left Single Right Left-Right Right-Left

43 Exercise Draw by hand, showing the tree after each step with the balance factor at each node, as the following values are added to an AVL tree of integers: The resulting tree should be complete and balanced. Repeat the exercise with the integers added in the following order: The final result should be the same, but some intermediate results will be different.