AVL Trees.

Slides:



Advertisements
Similar presentations
1 Binary SearchTrees continued - AVL Trees Definition and construction via running example Lecture 16 ADS2 Lecture 16.
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.
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
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.
1 AVL Trees Drozdek Section pages
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.
AVL Trees / Slide 1 Delete Single rotation Deletion.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
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.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
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.
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 Tree: Balanced Binary Search Tree 9.
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
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.
Balancing Binary Search Trees
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.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
CS 201 Data Structures and Algorithms
AVL Search Trees Introduction What is an AVL Tree?
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Balanced Trees (AVL and RedBlack)
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 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
CSE 373 AVL trees read: Weiss Ch. 4, section
Chapter 6 Transform and Conquer.
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: Data Structures and Algorithms
Copyright © Aiman Hanna All rights reserved
Data Structures & Algorithms
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
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
CE 221 Data Structures and Algorithms
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 (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Trees (Adelson – Velskii – Landis)
INSERT THE TITLE OF YOUR PRESENTATION HERE:
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
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
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

AVL Trees

Look at emailOfDoom.txt data set Why do we want binary search trees? What property do we want to maintain? how can we do that? Look at emailOfDoom.txt data set

AVL Trees G. Adelson-Velskii and E.M. Landis, "An algorithm for the organization of information." Doklady Akademii Nauk SSSR, 146:263–266, 1962 (Russian). English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962. Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 458–475 of section 6.2.3: Balanced Trees. Note that Knuth calls AVL trees simply "balanced trees".

AVL Trees Proposed in 1962, Adelson-Velskii and Landis Trying to devise a better data structure for a chess engine! Depth ensured to be O(log n) But not always perfectly balanced Recursive definition: The sub-trees of every node differ in height by at most one. Every sub-tree is an AVL tree. Height of an empty tree is -1 Height of a leaf node is 0

Operations on AVL trees Search, as for BSTs Insert is the same as for BST but May have to fix the tree after insertion to restore the balance condition Can always do this via a modification known as a rotation Single rotation or double rotation Similarly for deletion Note that the balance condition ensures that search,insert and delete operations are all O(log n), where n is the number of nodes balancing is O(1) ADS2 Lecture 96

What do we mean by imbalance? x The imbalance of x is abs(height(left.x) – height(right.x)) If imbalance(x) > 1 we need to restore balance by a restructuring of the tree. This is called a “rotation”. There are 4 kinds of rotation

There are 4 cases to consider (after an insertion)

There are 4 cases to consider (after an insertion) Symmetry

Single Rotations Case 1 Case 4 Symmetry

LL Rotation k2 k1 X Y Z k1 k2 X Y Z

LL Rotation k2 k1 X Y Z k1 k2 X Y Z NOTE: X < k1 & X < Y & X < k2 & X < Z Y > X && Y > k1 & Y < k2 & Y < Z k1 > X && k1 < Y & k1 < k2 & k1 < Z k2 > X & k2 > k1 & k2 > Y & k2 < Z Z > X & Z > k1 & Z > Y & Z > k2

LL Rotation k2 k1 X Y Z k1 k2 X Y Z NOTE: X < k1 & X < Y & X < k2 & X < Z Y > X && Y > k1 & Y < k2 & Y < Z k1 > X && k1 < Y & k1 < k2 & k1 < Z k2 > X & k2 > k1 & k2 > Y & k2 < Z Z > X & Z > k1 & Z > Y & Z > k2 NOTE: X < k1 & X < Y & X < k2 & X < Z Y > X && Y > k1 & Y < k2 & Y < Z k1 > X && k1 < Y & k1 < k2 & k1 < Z k2 > X & k2 > k1 & k2 > Y & k2 < Z Z > X & Z > k1 & Z > Y & Z > k2

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x)

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x)

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x)

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 X Y Z

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 X Y Z k1 k2 X Y Z

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 X Y Z k1 k2 X Y Z NOTE: EI(X) < k1 < EI(Y) < k2 < EI(Z) where EI(T) means “Everything In tree T”

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 k1 k2 Z X X Y Y Z left.k2 = right.k1 right.k1 = k2

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 k1 k2 Z X X Y Y Z left.k2 = right.k1 right.k1 = k2

Case 1 Single Rotation LL Rotation x Identifying Case 1: imbalance(x) = 2 height(left.x) > height(right.x) height(left.left.x) > height(right.left.x) Restore balance by a LL Rotation k2 k1 X Y Z k1 k2 X Y Z left.k2 = right.k1 right.k1 = k2 Need to update height and parent information … obviously

RR Rotation Z Y X k2 k1 k1 k2 Z Y X Symmetric to LL Rotation

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x)

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x)

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x)

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation k1 k2 Z Y X

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation Z Y X k2 k1 k1 k2 Z Y X NOTE: EI(X) < k1 < EI(Y) < k2 < EI(Z) where EI(T) means “Everything In tree T”

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation Z Y X k2 k1 k1 k2 Z Y X right.k1 = left.k2 left.k2 = k1

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation Z Y X k2 k1 k1 k2 Z Y X right.k1 = left.k2 left.k2 = k1

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation Z Y X k2 k1 k1 k2 Z Y X right.k1 = left.k2 left.k2 = k1

Case 4 Single Rotation RR Rotation X Identifying Case 4: imbalance(x) = 2 height(right.x) > height(left.x) height(right.right.x) > height(left.right.x) Restore balance by a RR Rotation Z Y X k2 k1 k1 k2 Z Y X right.k1 = left.k2 left.k2 = k1 Need to update height and parent information … obviously

Double Rotation

Double Rotation Case 2 Case 3 Symmetry

Case 2 Double Rotation LR Rotation x Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x) Restore balance by a LR (Double) Rotation k3 k1 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 Need to update height and parent information … obviously

Case 2 Double Rotation LR Rotation x Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x) Restore balance by a LR (Double) Rotation k3 k1 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 Need to update height and parent information … obviously

Case 2 Double Rotation LR Rotation x Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x) Restore balance by a LR (Double) Rotation k3 k1 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 Need to update height and parent information … obviously

Case 2 Double Rotation LR Rotation x Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x) Restore balance by a LR (Double) Rotation k3 k1 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 Need to update height and parent information … obviously

Case 2 Double Rotation LR Rotation x Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x) Restore balance by a LR (Double) Rotation k3 k1 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 Need to update height and parent information … obviously

RL Rotation

Case 3 Double Rotation RL Rotation x Identifying Case 3: imbalance(x) = 2 height(right.x) > height(left.x) height(left.right.x) > height(right.right.x) Restore balance by a RL (Double) Rotation k1 k3 A D B C k2 k2 A D B C k3 k1 left.k3 = right.k2 right.k1 = left.k2 left.k2 = k1 right.k2 = k3 NOTE: same steps as in LR Rotation Need to update height and parent information … obviously

We have our own demo program Test We also have links to demos off site. Nice demo is to build tree with AVL and compare with BTree But now … the code

NOTE: I have made a different engineering decision from that in BTree. In BTree most of the work is done in the Node class. Here most of the code/work is done in the BTree class. What do you think?

height is a new property Node height is a new property get*, set*, is*

BTree

BTree

BTree

BTree Previously we did this stuff in class Node NOTE: iterative Here’s the AVL stuff

BTree There is a bug in insert. Can you see it? Hint: could we increment size twice?

BTree Recursive, but edited out Compare & contrast?

BTree We need this

BTree

BTree

BTree

Restore balance by a LL Rotation BTree x

Restore balance by a LL Rotation BTree x k2 k1 X Y Z k1 k2 X Y Z

Restore balance by a LR (double) Rotation BTree Identifying Case 2: imbalance(x) = 2 height(left.x) > height(right.x) height(right.left.x) > height(left.left.x)

Restore balance by a LR (double) Rotation BTree k3 k1 A D B C k2 k2 A D B C k3 k1

Restore balance by a LR (double) Rotation BTree k3 k1 A D B C k2 k2 A D B C k3 k1

BTree

… and obviously we have code for cases3 and 4, code for RR-rotation and RL (double) rotation

Experiments … with “the email of doom”

emailOfDoom.txt

emailOfDoom.txt

emailOfDoom.txt

emailOfDoom.txt

emailOfDoom.txt BTree v AVLTree run this and get stats

Still need to implement delete under the AVL property

There are quite a few different descriptions of AVL I have tried to keep mine simple & clear