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.

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

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 (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
CS202 - Fundamental Structures of Computer Science II
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
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.
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.
CSIT 402 Data Structures II
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.
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.
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.
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
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
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.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: 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 Tree A Balanced Binary Search Tree
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Copyright © Aiman Hanna All rights reserved
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Tree Chapter 6 (cont’).
INSERT THE TITLE OF YOUR PRESENTATION HERE:
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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 is essentially a linked list) and the search time degrades to O(N). In this section we will consider a variation of a BST that ensures that the tree remains “balanced”. The AVL tree (named for its inventors: Adelson-Velskii and Landis) was the first balanced binary search tree and was invented in Definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of its left and right sub-trees differs by at most 1. The height of an empty tree is taken to be –1. It can be shown that provided this balance property is maintained, the search operation is O( log N ) in the worst case.

CPSC 252 AVL Trees Page 2 It is important to realize that after an insertion into a tree that is already balanced, the only nodes that have their balances altered are those that are on the path from the insertion point to the root. Consider what happens if we insert a 1 into the BST below: We will show that the entire tree can be rebalanced by rebalancing the lowest sub-tree that becomes unbalanced due to the insertion.

CPSC 252 AVL Trees Page 3 How do we determine if a node is unbalanced? We store additional data in each node that indicates one of the following possible states: E – the left and right sub-trees have equal height RH – the right sub-tree is higher (has a longer path to a leaf) LH – the left sub-tree is higher

CPSC 252 AVL Trees Page 4 When we perform an insertion into a tree, we update the balance indicator for each node on the path from the insertion point to the root until we find the first node that is not balanced: We now attempt to balance the sub-tree that has this unbalanced node as its root. This can be done using what is called a tree rotation…

CPSC 252 AVL Trees Page 5 We rotate the unbalanced node with its left child: The code to perform this operation is very straightforward. Assume that ubNode is a pointer to the unbalanced node:

CPSC 252 AVL Trees Page 6 To this point, we have examined only one special case. We must now show that we can balance a tree just as easily in every case. Suppose that we need to rebalance a node N. Such rebalancing may occur because of any one of the following operations: 1. N is currently LH and we insert into the left sub-tree of the left child of N 2. N is currently LH and we insert into the right sub-tree of the left child of N

CPSC 252 AVL Trees Page 7 3. N is currently RH and we insert into the left sub-tree of the right child of N 4. N is currently RH and we insert into the right sub-tree of the right child of N

CPSC 252 AVL Trees Page 8 Note that cases 1 and 4 are mirror images of each other – the insertion occurs on the “outside” of the tree: either to the left sub- tree of the left child (which we will call the LL case) or to the right sub-tree of the right child (which we will call the RR case). Similarly, cases 2 and 3 are mirror images of each other – the insertion occurs on the “inside” of the tree: either to the right sub- tree of the left child (which we will call the LR case) or to the left sub-tree of the right child (which we will call the RL case). We will see that balancing the tree in the LL and RR cases is fairly straightforward while the LR and RL cases require a little more work.