Data Structures & Algorithms

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.
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
CS202 - Fundamental Structures of Computer Science II
CPSC 335 Height Balanced Trees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
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 (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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.
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.
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.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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 CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced trees
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
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.
Balancing Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees binary tree for every node x, define its balance factor
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.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
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 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.
TCSS 342, Winter 2006 Lecture Notes
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
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
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Copyright © Aiman Hanna All rights reserved
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
AVL-Trees (Part 2).
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:

Data Structures & Algorithms AVL Tree Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon

Height Balanced Trees A height balanced tree is one in which the difference in the height of the two subtrees for any node is less than or equal to some specified amount. One type of height balanced tree is the AVL tree named after its originators (Adelson-Velskii & Landis). In this tree the height difference may be no more than 1.

AVL Tree In the AVL trees, searches always stay close to the theoretical minimum of O(log n) and never degenerate to O(n). The tradeoff for search efficiency is increased time and complexity for insertion and deletion from these trees since each time the tree is changed it may go out of balance and have to be rebalanced. August 07, 2009

Balance Factor To implement an AVL tree each node must contain a balance factor which indicates its state of balance relative to its subtrees. If balance is determined by (height left tree) - (height right tree) then the balance factors in a balanced tree can only have values of -1, 0, or 1. August 07, 2009

AVL Tree : Examples AVL tree with 1 to 4 nodes -1 1 August 07, 2009

Insertion of a new Node Adding a new node can change the balance factor of any node by at most 1. If a node currently has a balance factor of 0 it cannot go out of balance. If a node is left high (left subtree 1 level higher than right) and the insertion is in its left subtree it may go out of balance ( same applies to right/right). If a node is left high and the insertion takes place in its right subtree then a) it cannot go out of balance b) it cannot affect the balance of nodes above it since the balance factor of higher nodes is determined by the maximum height of this node which will not change. Augus t 07, 2009

Pivot Node If the balance factor of each node passed through on the path to an insertion is examined then it should be possible to predict which nodes could go out of balance and determine which of these nodes is closest to the insertion point. This node will be called the pivot node. Restoring the balance of the pivot node restores the balance of the whole sub-tree and potentially all of the nodes that were affected by the insertion. The mechanism of restoring balance to an AVL tree is called rotation. August 07, 2009

August 07, 2009

Tree Rotations Correction of the LL or RR imbalance requires only a single rotation about the pivot node but the second case, LR or RL requires a prior rotation about the root of the affected subtree then a rotation about the pivot node. . A rotation consists of moving the child of the high subtree into the position occupied by the pivot, and moving the pivot down into the low subtree. August 07, 2009

LL (RR) Imbalance To correct an LL imbalance the high child replaces the pivot and the pivot moves down to become the root of this child’s right subtree. The original right child of this node becomes the left child of the pivot (for RR exchange left and right in the description). August 07, 2009

LR (RL) Imbalance To correct an LR imbalance a rotation is first performed about the left child of the pivot as if it were the pivot for a RR imbalance. This rotation effectively converts the imbalance of the original pivot to a LL which can now be corrected as above. August 07, 2009

Restoring Balance – Tree Rotation August 07, 2009

Single rotation The new key is inserted in the subtree A. The AVL-property is violated at x height of left(x) is h+2 height of right(x) is h. August 07, 2009

Single rotation The new key is inserted in the subtree C. The AVL-property is violated at x. August 07, 2009

x AVL Tree C y 8 B A Insert 0.8 3 5 After rotation 5 5 8 3 3 4 1 4 1

Double rotation The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape also called left-right rotate

Double rotation The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. also called right-left rotate

x y A z B C AVL Tree 8 Insert 3.5 4 5 After Rotation 5 5 8 3 3 4 1 4 1

An Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 August 07, 2009

Deletion Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf. 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. If yes, proceed to parent(x). If not, perform an appropriate rotation at x. There are 4 cases as in the case of insertion. For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root. August 07, 2009

Single rotations in deletion In both figures, a node is deleted in subtree C, causing the height to drop to h. The height of y is h+2. When the height of subtree A is h+1, the height of B can be h or h+1. the same single rotation can correct both cases. August 07, 2009

Summary : AVL Tree Balanced trees demonstrate superior performance to binary search trees due to log (n) tree depth Rotations are used as a typical mechanism to restore balance Balanced trees are used in lookup-intensive applications August 07, 2009

Queries August 07, 2009

Thanks!!! August 07, 2009