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:

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
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.
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.
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
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
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL Trees / Slide 1 Delete Single rotation Deletion.
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.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 Theory I Algorithm Design and Analysis (3 - Balanced trees, AVL trees) Prof. Th. Ottmann.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
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!
AVL Trees ITCS6114 Algorithms and Data Structures.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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)
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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.
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:
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.
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.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
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 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.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CS202 - Fundamental Structures of Computer Science II
CS 201 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
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 CENG 213 Data Structures.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Data Structures & Algorithms
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CE 221 Data Structures and Algorithms
AVL-Trees.
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-black tree properties
AVL-Trees (Part 2).
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

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: a sequence of elements, array, linked lists * Tree: nested sets of elements, … * Binary tree * Binary search tree * Heap * …

AVL Trees / Slide 3 Binary Search Tree * If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 * Sequentially insert 3, 2, 1, 4, 5, 6 to an BST Tree Review of ‘insertion’ and ‘deletion’ for BST

AVL Trees / Slide 4 Balance Binary Search Tree * Worst case height of binary search tree: N-1 n Insertion, deletion can be O(N) in the worst case * We want a tree with small height * Height of a binary tree with N node is at least  (log N) * Goal: keep the height of a binary search tree O(log N) * Balanced binary search trees n Examples: AVL tree, red-black tree

AVL Trees / Slide 5 Balanced Tree? * Suggestion 1: the left and right subtrees of root have the same height n Doesn’t force the tree to be shallow * Suggestion 2: every node must have left and right subtrees of the same height n Only complete binary trees satisfy n Too rigid to be useful * Our choice: for each node, the height of the left and right subtrees can differ at most 1

AVL Trees / Slide 6 AVL Tree * An AVL (Adelson-Velskii and Landis 1962) tree is a binary search tree in which n for every node in the tree, the height of the left and right subtrees differ by at most 1. AVL property violated here AVL tree

AVL Trees / Slide 7 AVL Tree with Minimum Number of Nodes N 1 = 2N 2 =4N 3 = N 1 +N 2 +1=7N 0 = 1

AVL Trees / Slide 8 Smallest AVL tree of height 9 Smallest AVL tree of height 7 Smallest AVL tree of height 8

AVL Trees / Slide 9 Height of AVL Tree  Denote N h the minimum number of nodes in an AVL tree of height h  N 0 =0, N 1 =2 (base) N h = N h-1 + N h-2 +1 (recursive relation) * N > N h = N h-1 + N h-2 +1 >2 N h-2 >4 N h-4 >…>2 i N h-2i * If h is even, let i=h/2–1. The equation becomes N>2 h/2-1 N 2  N>2 h/2-1 x4  h=O(logN) * If h is odd, let i=(h-1)/2. The equation becomes N>2 (h-1)/2 N 1  N>2 (h-1)/2 x2  h=O(logN) * Thus, many operations (i.e. searching) on an AVL tree will take O(log N) time

AVL Trees / Slide 10 Insertion in AVL Tree * Basically follows insertion strategy of binary search tree n But may cause violation of AVL tree property * Restore the destroyed balance condition if needed Original AVL tree Insert 6 Property violated Restore AVL property

AVL Trees / Slide 11 Some Observations * After an insertion, only nodes that are on the path from the insertion point to the root might have their balance altered n Because only those nodes have their subtrees altered * Rebalance the tree at the deepest such node guarantees that the entire tree satisfies the AVL property 7 68 Rebalance node 7 guarantees the whole tree be AVL 6 Node 5,8,7 might have balance altered

AVL Trees / Slide 12 Different Cases for Rebalance * Denote the node that must be rebalanced α n Case 1: an insertion into the left subtree of the left child of α n Case 2: an insertion into the right subtree of the left child of α n Case 3: an insertion into the left subtree of the right child of α n Case 4: an insertion into the right subtree of the right child of α * Cases 1&4 are mirror image symmetries with respect to α, as are cases 2&3

AVL Trees / Slide 13 Rotations * Rebalance of AVL tree are done with simple modification to tree, known as rotation * Insertion occurs on the “outside” (i.e., left-left or right-right) is fixed by single rotation of the tree * Insertion occurs on the “inside” (i.e., left-right or right-left) is fixed by double rotation of the tree

AVL Trees / Slide 14 Insertion Algorithm * First, insert the new key as a new leaf just as in ordinary binary search tree * Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1 n If yes, proceed to parent(x) n If not, restructure by doing either a single rotation or a double rotation * Note: once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.

AVL Trees / Slide 15 Single Rotation to Fix Case 1(left-left) k2 violates An insertion in subtree X, AVL property violated at node k2 Solution: single rotation

AVL Trees / Slide 16 Single Rotation Case 1 Example k2 k1 X k2 X

AVL Trees / Slide 17 Single Rotation to Fix Case 4 (right-right) * Case 4 is a symmetric case to case 1 * Insertion takes O(Height of AVL Tree) time, Single rotation takes O(1) time An insertion in subtree Z k1 violates

AVL Trees / Slide 18 Single Rotation Example * Sequentially insert 3, 2, 1, 4, 5, 6 to an AVL Tree Insert 3, Single rotation Insert Insert 5, violation at node 3 Single rotation Insert 6, violation at node Single rotation Insert 1 violation at node 3

AVL Trees / Slide 19 * If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, Insert 7, violation at node Single rotation Insert 16, fine Insert 15 violation at node Single rotation But…. Violation remains

AVL Trees / Slide 20 Single Rotation Fails to fix Case 2&3 * Single rotation fails to fix case 2&3 * Take case 2 as an example (case 3 is a symmetry to it ) n The problem is subtree Y is too deep n Single rotation doesn’t make it any less deep Single rotation resultCase 2: violation in k2 because of insertion in subtree Y

AVL Trees / Slide 21 Double Rotation to Fix Case 2 (left-right) * Facts n The new key is inserted in the subtree B or C n The AVL-property is violated at k 3 n k 3 -k 1 -k 2 forms a zig-zag shape * Solution n We cannot leave k 3 as the root n The only alternative is to place k 2 as the new root Double rotation to fix case 2

AVL Trees / Slide 22 Double Rotation to fix Case 3(right-left) * Facts n The new key is inserted in the subtree B or C n The AVL-property is violated at k 1 n k 2 -k 3 -k 2 forms a zig-zag shape * Case 3 is a symmetric case to case 2 Double rotation to fix case 3

AVL Trees / Slide 23 * Restart our example We’ve inserted 3, 2, 1, 4, 5, 6, 7, 16 We’ll insert 15, 14, 13, 12, 11, 10, 8, Insert 16, fine Insert 15 violation at node Double rotation k1 k3 k2 k1k3

AVL Trees / Slide Insert 14 k1 k3 k Double rotation k2 k3 5 k1 A C D Insert Single rotation k1 k2 Z X Y

AVL Trees / Slide Insert Single rotation Insert Single rotation 14

AVL Trees / Slide Insert Single rotation Insert 8, fine then insert Single rotation 10