IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.

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

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: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture3.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
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
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:
AVL Trees / Slide 1 Delete Single rotation Deletion.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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):
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.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
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.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
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.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Data Structures AVL Trees.
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.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
AVL Trees CSE, POSTECH.
Data Structures – LECTURE Balanced trees
Search Trees.
Binary search tree. Removing a node
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CS202 - Fundamental Structures of Computer Science II
Lecture 17 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
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.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Balanced Binary Search Trees
AVL Trees CSE 373 Data Structures.
Lecture No.20 Data Structures Dr. Sohail Aslam
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
1 Lecture 13 CS2013.
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
Presentation transcript:

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27 th Mar 2007 AVL Tree

2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree Definition Properties Operations Outline

3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 XX AVL Trees Unbalanced Binary Search Trees are bad. Worst case: operations take O(n). AVL (Adelson-Velskii & Landis) trees maintain balance. For each node in tree, height of left subtree and height of right subtree differ by a maximum of 1. H H-1 H-2

4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees

5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees

6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar Insertion for AVL Tree After insert 1

7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Insertion for AVL Tree To ensure balance condition for AVL-tree, after insertion of a new node, we back up the path from the inserted node to root and check the balance condition for each node. If after insertion, the balance condition does not hold in a certain node, we do one of the following rotations: Single rotation Double rotation

8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Insertions Causing Imbalance R P Q k1k1 k2k2 Q k2k2 P k1k1 R An insertion into the subtree: P (outside) - case 1 Q (inside) - case 2 An insertion into the subtree: Q (inside) - case 3 R (outside) - case 4 H P =H Q =H R

9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 A k2k2 B k1k1 C CB A k1k1 k2k2 Single Rotation (case 1) H A =H B +1 H B =H C

10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Single Rotation (case 4) C k1k1 B k2k2 A AB C k2k2 k1k1 H A =H B H C =H B +1

11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Q k2k2 P k1k1 R R P Q k1k1 k2k2 Problem with Single Rotation Single rotation does not work for case 2 and 3 (inside case) H Q =H P +1 H P =H R

12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 Double Rotation: Step C k3k3 A k1k1 D B k2k2 H A =H B =H C =H D

13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 Double Rotation: Step

14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 C k3k3 A k1k1 D B k2k2 C k3k3 A k1k1 D B k2k2 Double Rotation H A =H B =H C =H D

15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 B k1k1 D k3k3 A C k2k2 B k1k1 D k3k3 A C k2k2 Double Rotation H A =H B =H C =H D

16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar Example Insert 3 into the AVL tree

17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Example Insert 5 into the AVL tree

18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Trees: Exercise Insertion order: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Remove Operation in AVL Tree Removing a node from an AVL Tree is the same as removing from a binary search tree. However, it may unbalance the tree. Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node. Use the appropriate single or double rotation to balance the tree. May need to continue searching for unbalanced nodes all the way to the root.

20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Deletion X in AVL Trees Deletion: Case 1: if X is a leaf, delete X Case 2: if X has 1 child, use it to replace X Case 3: if X has 2 children, replace X with its inorder predecessor (and recursively delete it) Rebalancing

21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 1)

22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 1)

23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 2)

24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 2)

25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 60 (case 3) prev

26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 60 (case 3)

27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 3) prev

28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 55 (case 3)

29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 3) prev

30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 50 (case 3)

31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40 (case 3) prev

32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40 : Rebalancing Case ?

33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Delete 40: after rebalancing Single rotation is preferred!

34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Minimum Element in AVL Tree An AVL Tree of height H has at least F H+3 -1 nodes, where F i is the i-th fibonacci number S 0 = 1 S 1 = 2 S H = S H-1 + S H S H-1 S H-2 H H-1 H-2

35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree: analysis (1)

36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 AVL Tree: analysis (2) The depth of AVL Trees is at most logarithmic. So, all of the operations on AVL trees are also logarithmic. The worst-case height is at most 44 percent more than the minimum possible for binary trees.

37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar 2007 Summary Find element, insert element, and remove element operations all have complexity O(log n) for worst case Insert operation: top-down insertion and bottom up balancing

38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture 27 th Mar /resources/animation/data- structure/avl/avltree.html Section 19.4 of Weiss book Further Study