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

Similar documents
COMP171. AVL-Trees (Part 1)

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

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

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Ch04 Balanced Search Trees

Algorithms. AVL Tree

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees

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

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

Recall from Last Time: AVL Trees

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

CISC 235: Topic 4. Balanced Binary Search Trees

Balanced Search Trees. CS 3110 Fall 2010

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

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

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

CS350: Data Structures AVL Trees

Data Structures in Java

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

Red-Black, Splay and Huffman Trees

CS Transform-and-Conquer

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

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

Balanced search trees

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

CHAPTER 10 AVL TREES. 3 8 z 4

CSI33 Data Structures

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

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

CS350: Data Structures Red-Black Trees

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

Data Structures Week #6. Special Trees

Analysis of Algorithms

Balanced Binary Search Trees

Data Structures and Algorithms

AVL Trees. See Section 19.4of the text, p

Data Structure - Advanced Topics in Tree -

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

AVL Trees (10.2) AVL Trees

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

CMPE 160: Introduction to Object Oriented Programming

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

Self-Balancing Search Trees. Chapter 11

Binary Search Trees. Analysis of Algorithms

Fundamental Algorithms

Search Structures. Kyungran Kang

Part 2: Balanced Trees

Chapter 12 Advanced Data Structures

Lecture 6: Analysis of Algorithms (CS )

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

Section 1: True / False (1 point each, 15 pts total)

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

More BSTs & AVL Trees bstdelete

Chapter 10: Search Trees

Data Structures Week #6. Special Trees

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

Balanced Binary Search Trees. Victor Gao

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

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

Data Structures Week #6. Special Trees

Week 3 Web site:

Chapter 2: Basic Data Structures

Section 4 SOLUTION: AVL Trees & B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Properties of red-black trees

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Inf 2B: AVL Trees. Lecture 5 of ADS thread. Kyriakos Kalorkoti. School of Informatics University of Edinburgh

CS 310: Tree Rotations and AVL Trees

Algorithms. Red-Black Trees

lecture17: AVL Trees

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

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

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.

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

13.4 Deletion in red-black trees

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Balanced Binary Search Trees

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Balanced Binary Search Trees

CSCI2100B Data Structures Trees

Search Trees. COMPSCI 355 Fall 2016

Chapter 22 Splay Trees

Lecture 16 Notes AVL Trees

Algorithms and Data Structures

AVL Trees. Reading: 9.2

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

Search Trees - 1 Venkatanatha Sarma Y

CS 206 Introduction to Computer Science II

Balanced Search Trees

Advanced Set Representation Methods

Lecture: Analysis of Algorithms (CS )

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.

Binary search trees (chapters )

10.2 AVL Trees. Definition of an AVL Tree. 438 Chapter 10. Search Trees

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

Search Trees. Chapter 11

Transcription:

AVL Trees (AVL Trees) Data Structures and Programming Spring 2017 1 / 17

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(logN) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree. (AVL Trees) Data Structures and Programming Spring 2017 2 / 17

AVL Tree (Georgy Adelson-Velsky and Evgenii Landis tree, 1962) 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) Data Structures and Programming Spring 2017 3 / 17

AVL Tree (Cont d) An AVL tree is a binary search tree in which for every node in the tree, the height of the left and right subtrees differ by at most 1. (AVL Trees) Data Structures and Programming Spring 2017 4 / 17

AVL Tree (Cont d) Let x be the root of an AVL tree of height h Let N h denote the minimum number of nodes in an AVL tree of height h Clearly, N i N i 1 by definition We have N h N h 1 + N h 2 + 1 2N h 2 + 1 > 2N h 2 By repeated substitution, we obtain the general form N h > 2 i N h 2i The boundary conditions are: N 1 = 1 and N 2 = 2. This implies that h = O(logN h ). More precisely, the height of an n-node AVL tree is approx. 1.44 log 2 n. Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(logN) time. (AVL Trees) Data Structures and Programming Spring 2017 5 / 17

Smallest AVL Tree (AVL Trees) Data Structures and Programming Spring 2017 6 / 17

Rotations When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. This is done using single rotations or double rotations. Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) and right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property. (AVL Trees) Data Structures and Programming Spring 2017 7 / 17

Insertion First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide]. For insertion, once we perform a rotation at a node x, we won t need to perform any rotation at any ancestor of x. (AVL Trees) Data Structures and Programming Spring 2017 8 / 17

Single rotation The AVL-property is violated at x height of left(x) is h+2 height of right(x) is h. (AVL Trees) Data Structures and Programming Spring 2017 9 / 17

Double rotation The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape (AVL Trees) Data Structures and Programming Spring 2017 10 / 17

Example - Rebalance after Insertion (AVL Trees) Data Structures and Programming Spring 2017 11 / 17

Another Example (AVL Trees) Data Structures and Programming Spring 2017 12 / 17

Another Example (Cont d) (AVL Trees) Data Structures and Programming Spring 2017 13 / 17

Deletion The single rotations for deletion can be divided into 4 cases In the following, a node is deleted in subtree C, causing the height to drop to h. The height of y is h+2. When the height of subtree A is h+1, the height of B can be h or h+1. Fortunately, the same single rotation can correct both cases. Imbalance may propagate upward so that many rotations may be needed. (AVL Trees) Data Structures and Programming Spring 2017 14 / 17

Example - Rebalance after Deletion (Single Rotation) (AVL Trees) Data Structures and Programming Spring 2017 15 / 17

Example - Rebalance after Deletion (Double Rotation) There are exactly two cases for double rotations (as in the case of insertion) Therefore, we can reuse exactly the same procedure for insertion to determine which rotation to perform (AVL Trees) Data Structures and Programming Spring 2017 16 / 17

Pros and Cons of AVL Trees Pros and Cons of AVL Trees 1 Search is O(log N) since AVL trees are always balanced. 2 Insertion and deletions are also O(log n) 3 The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: 1 Difficult to program and debug; more space for balance factor. 2 Asymptotically faster but rebalancing costs time. 3 Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 4 May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees). (AVL Trees) Data Structures and Programming Spring 2017 17 / 17