CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.

Slides:



Advertisements
Similar presentations
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
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 Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
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 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
CS202 - Fundamental Structures of Computer Science II
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.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
CSE 326: Data Structures AVL Trees
Properties of BST delete We first do the normal BST deletion: – 0 children: just delete it – 1 child: delete it, connect child to parent – 2 children:
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Data Structures AVL Trees.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
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 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.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CO4301 – Advanced Games Development Week 11 AVL Trees
Week 7 - Friday CS221.
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.
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
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 Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
Data Structures and Algorithms
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
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
CS223 Advanced Data Structures and Algorithms
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Lecture 10: BST and AVL Trees
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS 261 – Data Structures AVL Trees.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CS261 Data Structures AVL Trees

Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees

Binary Search Tree: Balance

Binary Search Tree: Balance

Complete Binary Trees Very costly to maintain a complete binary tree AlexAbnerAngelaAdelaAliceAdamAbigail Add Adam to tree

Height-Balanced BST For each node, the height difference between the left and right subtrees is at most one Trees are locally balanced, but globally they can be slightly more unbalanced Null child height = -1 3 (3) 9 (0) 1 (0) 8 (2) 2 (1) 4 (0) 5 (1) 6 (0) Height-Balanced Tree

Quiz Are these trees height balanced? If not, which node is out of balance?

Quiz Are these trees height balanced? If not, which node is out of balance?

Height-Balanced Trees Mathematically, the longest path in a height- balanced tree has been shown to be, at worst, 44% longer than log n Therefore, algorithms on height-balanced trees that run in time proportional to the path length are still O(log n) So.....How do we maintain height balance??

AVL Trees Named after the inventors ’ initials: G.M. Adelson-Velskii, E.M. Landis Maintain the height balanced property of a BST through a series of rotations When unbalanced, performs a “ rotation ” to balance the tree 1 (2) Node data Height field 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate left Unbalanced node

Two Cases to Remember (Mirror for Right) After operation Heavy left child becomes heavy itself on the left! (Single Rot) Heavy left child becomes heavy itself on the left! (Single Rot) unbalanced Heavy left child becomes heavy itself on the right ! (Double Rot) Heavy left child becomes heavy itself on the right ! (Double Rot)

Fix with Rotations… 3 (0) 2 (1) 1 (2)

Case 1: Single Rotation 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate left Unbalanced “top” node

Case 1: Single Rotation Example 2 1 (0) 2 (3) 6 (0) 3 (0) 4 (2) 5 (1) 1 (0) 2 (1) 6 (0) 3 (0) 4 (2) 5 (1) Rotate left Unbalanced “ top ” node New “ top ” node

AVL Trees: Rotation Pseudocode Pseudocode to rotate current ( “ top ” ) node left: 1.New top node is the current top node’s right child 2.Current node ’ s new right child is the new top node ’ s left child 3.New top ’ s left child is the current node 4.Set height of current node 5.Set height of new top node 6.Return new top node 1 (0) 2 (3) 6 (0) 3 (0) 4 (2) 5 (1) 1 (0) 2 (1) 6 (0) 3 (0) 4 (2) 5 (1) Rotate left “Current” top node “New” top node

Case 2: Double Rotation Sometimes a single rotation will not fix the problem : – Can happen when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child 2 1 Unbalanced node 3 “ Heavy ” right child 2 (0) 3 (2) 1 (1) Rotate left Doesn ’ t work!!!

Case 2: Double Rotation Fortunately, this case is easily handled by rotating the child before the regular rotation: 1.First rotate the heavy right (or left) child to the right (or left) 2.Rotate the unbalanced node to the left (or right) 2 (0) 1 (2) Unbalanced node 3 (1) “ Heavy ” right child Rotate heavy child right 3 (0) 2 (1) 1 (2) 3 (0) 1 (0) 2 (1) Rotate unbalanced node left

Case 1: Single Rotation (another view) Have a node (2) that is heavy on the left (1) Operation makes (2) unbalanced and that heavy child heavy on left After Insertion

Case 1: Single Rotation Single rotation fixes the problem Single Rotation

Case 2: Double Rotation Have a node (3) with a heavy left child (1) Operation makes (3) unbalanced and (1) is heavy on the right After Deletion 2 2

Case 2: Double Rotation 1 3 First, rotate heavy child (1) to the left After Rotation

Case 2: Double Rotation Next, rotate unbalanced node (3) to the right After Rotation

AVL Trees: Balacing Pseudocode Balancing pseudocode (to rebalance an unbalanced node) : If left child is tallest (by more than 1): If left child is heavy on the right side: // Double rotation needed. Rotate the left child to the left Rotate unbalanced “top” node to the right Else if right child is tallest(by more than 1) If right child is heavy on the left side: // Double rotation needed. Rotate the right child to the right Rotate unbalanced “top” node to the left Return new “ top” node

Rotations Can be the result of additions or removals All cases hold for the “right” side as well, just replace all lefts with rights and all rights with lefts

3 (3) 9 (0) 1 (0) 8 (2) 2 (1) 4 (0) 5 (1) 6 (0) More Examples… Add data: 7 Balanced Tree “ Heavy ” left child Unbalanced Tree 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Added to right side of heavy left child Unbalanced “top”node

Tree Still Unbalanced 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Single rotation AVL Trees: Double Rotation Example Unbalanced Tree 3 (4) 1 (0) 2 (1) 7 (0) 6 (1) 9 (0) 8 (2) 5 (3) 4 (0) Unbalanced “ top ” node (still)

Tree Still Unbalanced, but … Rotate heavy child AVL Trees: Double Rotation Example “ Heavy ” left child 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (1) 7 (0) 6 (2) 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (2) 7 (0) 6 (1) Unbalanced Tree

Tree Now Balanced Rotate top node AVL Trees: Double Rotation Example Unbalanced Tree (after 1 st rotation) 3 (3) 1 (0) 2 (1) 7 (0) 3 (4) 9 (0) 1 (0) 8 (3) 2 (1) 4 (0) 5 (1) 7 (0) 6 (2) Unbalanced “ top ” node 4 (0) 5 (1) 6 (2) 9 (0) 8 (1)

Your Turn Worksheet: AVL Practice