Data Structures AVL Trees.

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

AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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: Lecture27.
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 (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
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.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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.
CSE 326: Data Structures AVL Trees
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.
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.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
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 Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
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.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
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.
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.
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 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.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSE373: Data Structures & Algorithms
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree 27th Mar 2007.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
CSE 373 AVL trees read: Weiss Ch. 4, section
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
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
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Data Structures AVL Trees

Balanced BST strong enough! is easy to maintain Observation BST: the shallower the better! For a BST with n nodes Average height is O(log n) Worst case height is O(n) Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario: height O(n) Solution: Require a Balance Condition that ensures depth is O(log n) is easy to maintain strong enough!

The AVL Tree Data Structure Adelson-Velskii and Landis An AVL tree is: A binary search tree Heights of left and right subtrees of every node differ by at most 1 8 5 11 2 6 10 12 4 7 9 13 14 15

Recursive Height Calculation Recall: height is max number of edges from root to a leaf A hright hleftt How do we compute height at A? Note: height(null) = -1

AVL Tree? 6 3 4 1 8 2 1 7 11 1 12 10

AVL Tree? 4 6 3 4 8 1 2 1 5 0 7 11 3 1 2

An AVL Tree has Height O(log n) Proof Let S(h) be the min # of nodes in an AVL tree of height h AVL tree of height h=4 with the min # of nodes (12) 8 4 Claim: S(h) = S(h-1) + S(h-2) + 1 5 2 11 3 Solution of recurrence: S(h) = O(2h) (like Fibonacci numbers) 2 6 1 10 1 12 2 7 9 13 14 1 0 0 15

Testing the Balance Property We need to be able to: 10 1. Track Balance 5 15 2. Detect Imbalance 2 9 20 3. Restore Balance 7 17 30 NULL has a height of -1

An AVL Tree 10 3 10 5 20 15 30 2 9 17 7 data height children 3 2 2 1 1 30 2 9 17 7 Track height at all times.

AVL trees: find, insert, delete AVL find: Same as BST find. AVL insert: Same as BST insert, except you need to check your balance and may need to “fix” the AVL tree after the insert. AVL delete: We’re not going to talk about it, but same basic idea. Delete it, check your balance, and fix it.

Bad Case #1 Insert(6) Insert(3) Insert(1) 6 3 1 Simple illustration

Fix: Apply Single Rotation AVL Property violated at this node (x) 2 1 6 3 1 3 1 6 1 Intuition: 3 must become root Single Rotation: 1. Rotate between x and child Simple illustration

Bad Case #2 Insert(1) Insert(6) Insert(3) 1 6 3

Single Rotation Does Not Work AVL Property violated at this node (x) It’s a BST, but it’s unbalanced 2 2 1 1 1 6 1 3 6 3

Fix: Apply Double Rotation AVL Property violated at this node (x) 2 2 1 1 1 3 1 1 3 6 1 6 3 6 Double Rotation 1. Rotate between x’s child and grandchild 2. Rotate between x and x’s new child

AVL tree insert Find spot for new key Hang new node there with this key Search back up the path for imbalance If there is an imbalance: Case #1: Perform single rotation and exit Case #2: Perform double rotation and exit Both rotations keep the subtree height unchanged. Hence only one rotation is sufficient!

Case #1: Single Rotation h+3 a h+2 h b h+1 h Z X Y single rotation X < b < Y < a < Z b a X Y Z

Single rotation example 15 8 22 4 10 19 24 3 6 17 20 16

Single rotation example 15 8 22 4 10 19 24 3 6 17 20 16

Single rotation example 15 8 22 4 10 19 24 3 6 17 20 16 15 8 19 4 10 17 22 16 3 6 20 24

Case #2: Double Rotation h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) Let’s break subtree into pieces: c

Case #2: Double Rotation h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) Let’s break subtree into pieces: c b

Case #2: Double Rotation h+3 a h+2 b h h+1 h c h-1 h U Z X V Insert on left child’s right (at U or V) c a Let’s break subtree into pieces: b U V X Z

Can also do this in two rotations b h+1 h c h U h-1 Z X V First rotation X < b < U < c < V < a < Z a c h h-1 b h Z h V

Second rotation a c b Z V U X Second rotation c b a U V X Z h+3 h+2 h

Double rotation example 15 8 19 4 10 17 22 3 6 16 20 24 5

Double rotation example 15 8 19 4 10 17 22 3 6 16 20 24 5

Double rotation example 15 8 19 4 10 17 22 3 6 16 20 24 5

Double rotation example 15 8 19 4 10 17 22 3 6 16 20 24 5 15 8 19 10 17 22 16 20 24

Double rotation example 15 8 19 4 10 17 22 3 6 16 20 24 5 15 8 19 6 10 17 22 4 16 20 24 3 5

Double rotation example 15 8 19 6 10 17 22 4 16 20 24 3 5 15 6 19 4 8 17 22 3 5 10 16 20 24

Case #3: a b c X Z U V Double rotation c a b U V Z X h+3 h+2 h h+1 h h

Case #4: a b X Y Z Single rotation b a Z X Y h+3 h+2 h h h+1 h+2 h+1

Recap of AVL tree insert Let x be the node where an imbalance occurs. Four cases to consider. The insertion is in the left subtree of the left child of x. right subtree of the left child of x. left subtree of the right child of x. right subtree of the right child of x. Cases 1 & 4 are solved by a single rotation Cases 2 & 3 are solved by a double rotation

AVL complexity O(log n) O(n log n) What is the worst case complexity of a find? O(log n) What is the worst case complexity of an insert? What is the worst case complexity of build a AVL Tree? O(n log n)