AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.

Slides:



Advertisements
Similar presentations
Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight.
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.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
AVL Tree Iris Jiaonghong Shi. AVL tree operations AVL find : – Same as BST find AVL insert : – First BST insert, then check balance and potentially.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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 Balanced Binary Search Trees (not covered in book, but related to pp )
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.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
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.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
AVL Trees. Knowing More How many nodes? – Determine on demand.
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.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
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.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
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 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))
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL Tree 1. is a binary search tree that For each node: –The height of its left and right subtree can only differ at most 1. –If a tree is an empty tree,
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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
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.
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.
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Representation of an AVL Node
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.
CS 201 Data Structures and Algorithms
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Trees CENG 213 Data Structures.
Trees.
Representation of an AVL Node
Data Structures Lecture 21 Sohail Aslam.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Tree.
Red-black tree properties
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Presentation transcript:

AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book

AVL (Adelson-Velskii and Landis) tree A balanced binary search tree where the height of the two subtrees (children) of a node differs by at most one. Look-up, insertion, and deletion are O( log n), where n is the number of nodes in the tree. vltree.htmlhttp:// vltree.html Source: nist.gov

Definition of height (reminder) Height: the length of the longest path from a node to a leaf. –All leaves have a height of 0 –An empty tree has a height of –1

The insertion problem Unless keys appear in just the right order, imbalance will occur It can be shown that there are only two possible types of imbalance (see next slide): –Left-left (or right-right) imbalance –Left-right (or right-left) imbalance –The right-hand imbalances are the same, by symmetry

The two types of imbalance Left-left (right-right) Left-right (right-left) 2 1 A B C 2 1 A C B Left imbalance There are no other possibilities for the left (or right) subtree so-called ‘dog-leg’

Localising the problem Two principles: Imbalance will only occur on the path from the inserted node to the root (only these nodes have had their subtrees altered - local problem) Rebalancing should occur at the deepest unbalanced node (local solution too)

Left(left) imbalance (1) [and right(right) imbalance, by symmetry] B and C have the same height A is one level higher Therefore make 1 the new root, 2 its right child and B and C the subtrees of 2 Note the levels 2 1 A B C

Left(left) imbalance (2) [and right(right) imbalance, by symmetry] B and C have the same height A is one level higher Therefore make 1 the new root, 2 its right child and B and C the subtrees of 2 Result: a more balanced and legal AVL tree Note the levels 2 1 A BC

Single rotation 2 1 A BC 2 1 A B C

Left(right) imbalance (1) [and right(left) imbalance by symmetry] Can’t use the left-left balance trick - because now it’s the middle subtree, i.e. B, that’s too deep. Instead consider what’s inside B A C B

Left(right) imbalance (2) [and right(left) imbalance by symmetry] B will have two subtrees containing at least one item (just added We do not know which is too deep - set them both to 0.5 levels below subtree A 3 1 A C 2 B1B2

Left(right) imbalance (3) [and right(left) imbalance by symmetry] Neither 1 nor 3 worked as root node so make 2 the root Rearrange the subtrees in the correct order No matter how deep B1 or B2 (+/- 0.5 levels) we get a legal AVL tree again AC B1B2 13 2

double rotation 3 1 A C 2 B1B2 AC B1B2 13 2

private AvlNode insert( Comparable x, AvlNode t ) { /*1*/if( t == null ) t = new AvlNode( x, null, null ); /*2*/else if( x.compareTo( t.element ) < 0 ) { t.left = insert( x, t.left ); if( height( t.left ) - height( t.right ) == 2 ) if( x.compareTo( t.left.element ) < 0 ) t = rotateWithLeftChild( t ); else t = doubleWithLeftChild( t ); } /*3*/else if( x.compareTo( t.element ) > 0 ) { t.right = insert( x, t.right ); if( height( t.right ) - height( t.left ) == 2 ) if( x.compareTo( t.right.element ) > 0 ) t = rotateWithRightChild( t ); else t = doubleWithRightChild( t ); } /*4*/else ; // Duplicate; do nothing t.height = max( height( t.left ), height( t.right ) ) + 1; return t; } insert method

private static AvlNode rotateWithLeftChild( AvlNode k2 ) { AvlNode k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max( height( k2.left ), height( k2.right ) ) + 1; k1.height = max( height( k1.left ), k2.height ) + 1; return k1; } rotateWithLeftChild method

private static AvlNode rotateWithRightChild( AvlNode k1 ) { AvlNode k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max( height( k1.left ), height( k1.right ) ) + 1; k2.height = max( height( k2.right ), k1.height ) + 1; return k2; } rotateWithRightChild method

private static AvlNode doubleWithLeftChild( AvlNode k3 ) { k3.left = rotateWithRightChild( k3.left ); return rotateWithLeftChild( k3 ); } doubleWithLeftChild method

private static AvlNode doubleWithRightChild( AvlNode k1 ) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); } doubleWithRightChild method