Algorithms. AVL Tree

Similar documents
CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Ch04 Balanced Search Trees

CHAPTER 10 AVL TREES. 3 8 z 4

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Analysis of Algorithms

Search Trees (Ch. 9) > = Binary Search Trees 1

Search Trees - 1 Venkatanatha Sarma Y

COMP171. AVL-Trees (Part 1)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Data Structures Lesson 7

Self-Balancing Search Trees. Chapter 11

CS350: Data Structures AVL Trees

Chapter 10: Search Trees

Recall from Last Time: AVL Trees

Balanced Search Trees. CS 3110 Fall 2010

Advanced Set Representation Methods

Algorithms. Deleting from Red-Black Trees B-Trees

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

AVL Trees (10.2) AVL Trees

Binary Search Trees. Analysis of Algorithms

Dynamic Access Binary Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Part 2: Balanced Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Dynamic Access Binary Search Trees

Fundamental Algorithms

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Binary search trees (chapters )

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

Balanced Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

LECTURE 18 AVL TREES

Data Structures and Algorithms

CSCI2100B Data Structures Trees

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Chapter 4: Trees. 4.2 For node B :

CSI33 Data Structures

ECE250: Algorithms and Data Structures AVL Trees (Part A)

Data Structures in Java

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

COMP Analysis of Algorithms & Data Structures

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees. Eric McCreath

Multi-Way Search Trees

Balanced Binary Search Trees

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

COMP Analysis of Algorithms & Data Structures

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Balanced Binary Search Trees. Victor Gao

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary search trees (chapters )

DATA STRUCTURES AND ALGORITHMS

Motivation for B-Trees

Multiway Search Trees. Multiway-Search Trees (cont d)

CISC 235: Topic 4. Balanced Binary Search Trees

Augmenting Data Structures

Data Structures Week #6. Special Trees

Lecture 16 Notes AVL Trees

Data Structures Week #6. Special Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Lesson 21: AVL Trees. Rotation

CS Transform-and-Conquer

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Chapter 2: Basic Data Structures

CE 221 Data Structures and Algorithms

Binary Search Trees, etc.

Search Trees. Chapter 11

Balanced search trees

CS 261 Data Structures. AVL Trees

CMPE 160: Introduction to Object Oriented Programming

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

AVL Trees Heaps And Complexity

AVL Trees. See Section 19.4of the text, p

Advanced Tree Data Structures

Lecture 6: Analysis of Algorithms (CS )

Binary Trees, Binary Search Trees

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Multi-Way Search Trees

AVL trees and rotations

Lecture 13: AVL Trees and Binary Heaps

Lecture No. 10. Reference Variables. 22-Nov-18. One should be careful about transient objects that are stored by. reference in data structures.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Lecture 16 AVL Trees

Analysis of Algorithms

CS 310: Tree Rotations and AVL Trees

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

CSE 326: Data Structures Lecture #8 Binary Search Trees

Transcription:

Algorithms AVL Tree

Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.

Binary Search Tree - Best Time All BST operations are O(h), where d is tree depth minimum d is h log for a binary tree 2N with N nodes What is the best case tree? What is the worst case tree? So, best case running time of BST operations is O(log N)

Binary Search Tree - Worst Time Worst case running time is O(N) What happens when you Insert elements in ascending order? Insert: 2, 4, 6, 8, 10, 12 into an empty BST Problem: Lack of balance : compare depths of left and right subtree Unbalanced degenerate tree

Balanced and unbalanced BST 1 4 4 2 3 4 5 2 5 1 3 Is this balanced? 2 6 6 1 3 5 7 7

Approaches to balancing trees Don't balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Pretty good balance Only allow a little out of balance Adjust on access Self-adjusting

Balancing Binary Search Trees Many algorithms exist for keeping binary search trees balanced Adelson-Velskii and Landis (AVL) trees (height-balanced trees) Splay trees and other self-adjusting trees B-trees and other multiway search trees

AVL Tree is Named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search tree with balance condition in which the sub-trees of each node can differ by at most 1 in their height

Definition of a balanced tree Ensure the depth = O(log N) Take O(log N) time for searching, insertion, and deletion Every node must have left & right sub-trees of the same height

An AVL tree has the following properties: 1. Sub-trees of each node can differ by at most 1 in their height 2. Every sub-trees is an AVL tree

AVL tree? YES Each left sub-tree has height 1 greater than each right sub-tree NO Left sub-tree has height 3, but right sub-tree has height 1

AVL tree 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 height of its children plus 1 Note that this definition of height is different from the one we defined previously (we defined the height of a leaf as zero previously).

AVL Trees 10 10 5 20 5 20 3 3 43 2 1 1 3

AVL Trees 12 8 16 4 10 14 2 6

AVL Tree -1 0 0 0-1 1 0 0 0 0 AVL Tree -2 AVL Tree 1 0 0-1 0 Not an AVL Tree

Height of an AVL Tree n(2) 3 4 n(1) Fact: The height of an AVL tree storing n keys is O(log n). Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), (by induction), n(h) > 2 i n(h-2i) Solving the base case we get: n(h) > 2 h/2-1 Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n)

AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node

Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basis N(0) = 1, N(1) = 2 Induction N(h) = N(h-1) + N(h-2) + 1 h Solution (recall Fibonacci analysis) N(h) > h ( 1.62) h-1 h-2

Height of an AVL Tree N(h) > h ( 1.62) Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) n > h hence log n > h (relatively well balanced tree!!) h < 1.44 log 2 n (i.e., Find takes O(log n))

Insertion Insert 6 Imbalance at 8 Perform rotation with 7

Deletion Delete 4 Imbalance at 3 Perform rotation with 2 Imbalance at 5 Perform rotation with 8

Key Points AVL tree remain balanced by applying rotations, therefore it guarantees O(log N) search time in a dynamic environment Tree can be re-balanced in at most O(log N) time

Searching AVL Trees Searching an AVL tree is exactly the same as searching a regular binary tree all descendants to the right of a node are greater than the node all descendants to the left of a node are less than the node

Inserting in AVL Tree Insertion is similar to regular binary tree keep going left (or right) in the tree until a null child is reached insert a new node in this position an inserted node is always a leaf to start with Major difference from binary tree must check if any of the sub-trees in the tree have become too unbalanced search from inserted node to root looking for any node with a balance factor of 2

Inserting in AVL Tree A few points about tree inserts the insert will be done recursively the insert call will return true if the height of the sub-tree has changed since we are doing an insert, the height of the sub-tree can only increase if insert() returns true, balance factor of current node needs to be adjusted balance factor = height(right) height(left) left sub-tree increases, balance factor decreases by 1 right sub-tree increases, balance factor increases by 1 if balance factor equals 2 for any node, the subtree must be rebalanced

Inserting in AVL Tree M(-1) M(0) E(1) P(0) insert(v) E(1) P(1) J(0) J(0) V(0) M(-1) M(-2) E(1) P(0) insert(l) E(-2) P(0) J(0) J(1) L(0) This tree needs to be fixed!

Re-Balancing a Tree To check if a tree needs to be rebalanced start at the parent of the inserted node and journey up the tree to the root if a node s balance factor becomes 2 need to do a rotation in the sub-tree rooted at the node once sub-tree has been re-balanced, guaranteed that the rest of the tree is balanced as well can just return false from the insert() method 4 possible cases for re-balancing only 2 of them need to be considered other 2 are identical but in the opposite direction

Insertions in AVL Trees Let the node that needs rebalancing be. There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of. 2. Insertion into right subtree of right child of. Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of. 4. Insertion into left subtree of right child of. The rebalancing is performed through four separate rotation algorithms.

AVL Insertion: Outside Case Consider a valid AVL subtree j k h h h Z X Y

AVL Insertion: Outside Case k j h Inserting into X destroys the AVL property at node j h+1 h Z Y X

AVL Insertion: Outside Case j Do a right rotation k h h+1 h Z Y X

Single right rotation j Do a right rotation k h h+1 h Z Y X

Outside Case Completed k Right rotation done! ( Left rotation is mirror symmetric) h+1 j h h X Y Z AVL property has been restored!

AVL Insertion: Inside Case Consider a valid AVL subtree j k h h h Z X Y

AVL Insertion: Inside Case Inserting into Y destroys the AVL property at node j k j Does right rotation restore balance? h h h+1 Z X Y

AVL Insertion: Inside Case h k j Right rotation does not restore balance now k is out of balance X h+1 h Z Y

AVL Insertion: Inside Case Consider the structure of subtree Y j k h X h h+1 Z Y

AVL Insertion: Inside Case Y = node i and subtrees V and W j k h h i h+1 Z X h or h-1 V W

AVL Insertion: Inside Case j We will do a left-right double rotation... k X i Z V W

Double rotation : first rotation j left rotation complete i k Z W X V

Double rotation : second rotation j Now do a right rotation i k Z W X V

Double rotation : second rotation right rotation complete i Balance has been restored k j h h or h-1 h X V W Z

AVL Trees Example

AVL Trees Example

AVL Trees Example

AVL Trees Example

AVL Trees Example

AVL Trees Example

Example Insert 3 into the AVL tree 11 11 8 20 4 20 4 16 27 3 8 16 27 3 5/22/2012

Example Insert 5 into the AVL tree 11 11 8 20 5 20 4 16 27 4 8 16 27 5 5/22/2012

AVL Trees: Exercise Insertion order: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 5/22/2012

Deletion X in AVL Trees Deletion: Case 1: if X is a leaf, delete X Case 2: if X has 1 child, use it to replace X Case 3: if X has 2 children, replace X with its inorder predecessor (and recursively delete it) Rebalancing 5/22/2012

Delete 55 (case 1) 60 20 70 10 40 65 85 5 15 30 50 80 90 55 5/22/2012

Delete 55 (case 1) 60 20 70 10 40 65 85 5 15 30 50 80 90 55 5/22/2012

Delete 50 (case 2) 60 20 70 10 40 65 85 5 15 30 50 80 90 55 5/22/2012

Delete 50 (case 2) 60 20 70 10 40 65 85 5 15 30 50 80 90 55 5/22/2012

Delete 60 (case 3) 60 20 70 10 40 65 85 5 15 30 50 prev 80 90 55 5/22/2012

Delete 60 (case 3) 55 20 70 10 40 65 85 5 15 30 50 80 90 5/22/2012

Delete 55 (case 3) 55 20 prev 70 10 40 65 85 5 15 30 50 80 90 5/22/2012

Delete 55 (case 3) 50 20 70 10 40 65 85 5 15 30 80 90 5/22/2012

Delete 50 (case 3) 50 20 prev 70 10 40 65 85 5 15 30 80 90 5/22/2012

Delete 50 (case 3) 40 20 70 10 30 65 85 5 15 80 90 5/22/2012

Delete 40 (case 3) 40 20 prev 70 10 30 65 85 5 15 80 90 5/22/2012

Delete 40 : Rebalancing 30 20 70 10 Case? 65 85 5 15 80 90 5/22/2012

Delete 40: after rebalancing 30 10 70 5 20 65 85 Single rotation is preferred! 15 80 90 5/22/2012

AVL Tree: analysis The depth of AVL Trees is at most logarithmic. So, all of the operations on AVL trees are also logarithmic. The worst-case height is at most 44 percent more than the minimum possible for binary trees. 5/22/2012