CS202 - Fundamental Structures of Computer Science II

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

CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
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.
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.
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.
Balanced Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
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:
Tirgul 5 AVL trees.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
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.
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.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
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.
Data Structures AVL Trees.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
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))
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSE332: Data Abstractions Lecture 7: AVL Trees
AA Trees.
Search Trees.
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.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
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 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.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Richard Anderson Spring 2016
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
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:

CS202 - Fundamental Structures of Computer Science II lec06-balancedtrees April 15, 2017 Balanced Search Trees The height of a binary search tree is sensitive to the order of insertions and deletions. The height of a binary search tree is between  log2(N+1)  and N. So, the worst case behavior of some BST operations are O(N). There are various search trees that can retain their balance at the end of each insertion and deletion. AVL Trees 2-3 Trees 2-3-4 Trees Red-Black Trees In these height balanced search trees, the run time complexity of insertion, deletion, and retrieval operations is O(log2N) at the worst case. Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II 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 the ideal tree (completely balanced tree). AVL Tree maintains a height close to the minimum. Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees Definition: An AVL tree is a binary search tree such that for any node in the tree, the height of the left and right subtrees can differ by at most 1. An AVL tree NOT an AVL tree (unbalanced nodes are darkened) Spring 2015 CS202 - Fundamental Structures of Computer Science II

AVL Trees -- Properties The depth of a typical node in an AVL tree is very close to the optimal log2N. Consequently, all searching operations in an AVL tree have logarithmic worst-case bounds. An update (insert or delete) in an AVL tree could destroy the balance.  It must then be rebalanced before the operation can be considered complete. Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Tree -- Insertions Insert is the same as Binary Search Tree insertion Then, starting from the insertion point, check for balance at each node It is enough to perform correction “rotation” only at the first node where imbalance occurs On the path from the inserted node to the root. Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL -- Insertions An AVL violation might occur in four possible cases: Insertion into left subtree of left child of node n Insertion into right subtree of left child of node n Insertion into left subtree of right child of node n Insertion into right subtree of right child of node n (1) and (4) are mirror cases (2) and (3) are mirror cases If insertion occurs “outside” (1 & 4), then perform single rotation. If insertion occurs “inside” (2 & 3), then perform double rotation. Spring 2015 CS202 - Fundamental Structures of Computer Science II

AVL Trees -- Balance Operations Balance is restored by tree rotations. There are four different cases for rotations: Single Right Rotation Single Left Rotation Double Right-Left Rotation Double Left-Right Rotation Spring 2015 CS202 - Fundamental Structures of Computer Science II

AVL Trees -- Single Rotation A single rotation switches the roles of the parent and the child while maintaining the search order. We rotate between a node and its child (left or right). Child becomes parent Parent becomes right child in Case 1 (single right rotation) Parent becomes left child in Case 2 (single left rotation) The result is a binary search tree that satisfies the AVL property. Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 1 -- Single Right Rotation Child becomes parent Parent becomes right child Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 1 -- Single Right Rotation Child becomes parent Parent becomes right child Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 2 – Single Left Rotation Before Rotation After Rotation Child becomes parent Parent becomes left child Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 3 -- Double Right-Left Rotation The height of B (or C) is the same as the height of D First perform single right rotation on k2 and k3 Then perform single left rotation on k2 and k1 Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 4 -- Double Left-Right Rotation The height of B (or C) is the same as the height of A First perform single left rotation on k2 and k1 Then perform single right rotation on k2 and k3 Spring 2015 CS202 - Fundamental Structures of Computer Science II

Case 4 -- Double Left-Right Rotation First perform single left rotation on k2 and k1 Then perform single right rotation on k2 and k3 Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Insertion It is enough to perform rotation only at the first node Where imbalance occurs On the path from the inserted node to the root. The rotation takes O(1) time. After insertion, only nodes that are on the path from the insertion point to the root can have their balances changed. Hence insertion is O(logN) Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Insertion Exercise: Starting with an empty AVL tree, insert the following items 7 6 5 4 3 2 1 8 9 10 11 12 Check the following applet for more exercises. http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Deletion is more complicated. It requires both single and double rotations We may need more than one rebalance operation (rotation) on the path from the deleted node back to the root. Steps: First delete the node the same as deleting it from a binary search tree Remember that a node can be either a leaf node or a node with a single child or a node with two children Walk through from the deleted node back to the root and rebalance the nodes on the path if required Since a rotation can change the height of the original tree Deletion is O(logN) Each rotation takes O(1) time We may have at most h (height) rotations, where h = O(logN) Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion For the implementation We have a shorter flag that shows if a subtree has been shortened Each node is associated with a balance factor left-high the height of the left subtree is higher than that of the right subtree right-high the height of the right subtree is higher than that of the left subtree equal the height of the left and right subtrees is equal In the deletion algorithm Shorter is initialized as true Starting from the deleted node back to the root, take an action depending on The value of shorter The balance factor of the current node Sometimes the balance factor of a child of the current node Until shorter becomes false Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Three cases according to the balance factor of the current node The balance factor is equal  no rotation The balance factor is not equal and the taller subtree was shortened The balance factor is not equal and the shorter subtree was shortened  rotation is necessary Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 1: The balance factor of p is equal. Change the balance factor of p to right-high (or left-high) Shorter becomes false – p deleted T2 T1 \ No rotations Height unchanged Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 2: The balance factor of p is not equal and the taller subtree is shortened. Change the balance factor of p to equal Shorter remains true / p deleted T2 T1 – No rotations Height reduced Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 3: The balance factor of p is not equal and the shorter subtree is shortened. Rotation is necessary Let q be the root of the taller subtree of p We have three sub-cases according to the balance factor of q Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 3a: The balance factor of q is equal. Apply a single rotation Change the balance factor of q to left-high (or right-high) Shorter becomes false – q T3 T2 deleted T1 \ p h-1 h / Single rotation Height unchanged Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 3b: The balance factor of q is the same as that of p. Apply a single rotation Change the balance factors of p and q to equal Shorter remains true p \ q T3 T2 deleted T1 h-1 h – Single rotation Height reduced Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Case 3c: The balance factor of q is the opposite of that of p. Apply a double rotation Change the balance factor of the new root to equal Also change the balance factors of p and q Shorter remains true T4 h-1 / q T3 T1 r T2 h-1 or h-2 p \ – deleted Double rotation Height reduced Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Deletion Exercise: Delete o from the following AVL tree Check the following applet for more exercises. http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html m a b c d f e g h i j l k p o n s r t u Spring 2015 CS202 - Fundamental Structures of Computer Science II

CS202 - Fundamental Structures of Computer Science II AVL Trees -- Analysis H 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 30 40 50 minN 33 54 88 143 232 376 609 986 1.596 2.583 4.180 6.764 10.945 17.710 2.178.308 267.914.295 32.951.280.098 logN 2,81 3,58 4,32 5,04 5,75 6,46 7,16 7,86 8,55 9,25 9,95 10,64 11,33 12,03 12,72 13,42 14,11 21,05 28,00 34,94 H / logN 1,42 1,39 1,40 1,41 1,43 What is the minimum number of nodes in an AVL tree? minN(0) = 0 minN(1) = 1 minN(2) = 2 minN(3) = 4 ... minN(h) = minN(h-1) + minN(h-2) + 1 Max height of an N-node AVL tree is less than 1.44 log N Spring 2015 CS202 - Fundamental Structures of Computer Science II