COMP171. AVL-Trees (Part 1)

Similar documents
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)

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

Algorithms. AVL Tree

Dynamic Access Binary Search Trees

CS350: Data Structures AVL Trees

Dynamic Access Binary Search Trees

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

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

CSI33 Data Structures

CISC 235: Topic 4. Balanced Binary Search Trees

Data Structures Lesson 7

AVL Trees (10.2) AVL Trees

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

CS350: Data Structures Red-Black Trees

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

Ch04 Balanced Search Trees

More BSTs & AVL Trees bstdelete

Data Structures in Java

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

Recall from Last Time: AVL Trees

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

Balanced Search Trees. CS 3110 Fall 2010

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

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

Analysis of Algorithms

lecture17: AVL Trees

Search Structures. Kyungran Kang

CS 206 Introduction to Computer Science II

Fundamental Algorithms

AVL trees and rotations

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

Data Structure - Advanced Topics in Tree -

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

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

Lecture 6: Analysis of Algorithms (CS )

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

Binary Search Trees. Analysis of Algorithms

Data Structures Week #6. Special Trees

Data Structures and Algorithms

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

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

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

Binary Trees, Binary Search Trees

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

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

CE 221 Data Structures and Algorithms

Advanced Set Representation Methods

CHAPTER 10 AVL TREES. 3 8 z 4

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

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

Data Structures Week #6. Special Trees

AVL Trees Heaps And Complexity

Binary search trees (chapters )

Balanced Binary Search Trees

Part 2: Balanced Trees

CS 261 Data Structures. AVL Trees

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

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

DATA STRUCTURES AND ALGORITHMS

AVL trees and rotations

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

Balanced Binary Search Trees. Victor Gao

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

Data Structures Week #6. Special Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS 310: Tree Rotations and AVL Trees

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

Search Trees. Chapter 11

CS Transform-and-Conquer

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

Self-Balancing Search Trees. Chapter 11

Section 4 SOLUTION: AVL Trees & B-Trees

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

CSCI2100B Data Structures Trees

Chapter 10: Search Trees

Lecture: Analysis of Algorithms (CS )

Binary search trees (chapters )

CSI33 Data Structures

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.

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.

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2.

13.4 Deletion in red-black trees

Advanced Tree Structures

Red-black trees (19.5), B-trees (19.8), trees

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

CPSC 223 Algorithms & Data Abstract Structures

Advanced Tree Data Structures

Algorithms and Data Structures

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

Chapter 2: Basic Data Structures

CMPE 160: Introduction to Object Oriented Programming

Balanced search trees

AVL Trees. Reading: 9.2

Red-Black, Splay and Huffman Trees

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

CS 206 Introduction to Computer Science II

Transcription:

COMP11 AVL-Trees (Part 1)

AVL Trees / Slide 2 Data, a set of elements Data structure, a structured set of elements, linear, tree, graph, Linear: a sequence of elements, array, linked lists Tree: nested sets of elements, Binary tree Binary search tree Heap

AVL Trees / Slide 3 Binary Search Tree Review of insertion and deletion for BST Sequentially insert 3, 2, 1, 4, 5, 6 to an BST Tree If we continue to insert, 16, 15, 14, 13, 12, 11, 10, 8, 9

AVL Trees / Slide 4 Balance Binary Search Tree Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a tree with small height Height of a binary tree with N node is at least (log N) Goal: keep the height of a binary search tree O(log N) Balanced binary search trees Examples: AVL tree, red-black tree

AVL Trees / Slide 5 Balanced Tree? Suggestion 1: the left and right subtrees of root have the same height Doesn t force the tree to be shallow Suggestion 2: every node must have left and right subtrees of the same height Only complete binary trees satisfy Too rigid to be useful Our choice: for each node, the height of the left and right subtrees can differ at most 1

AVL Trees / Slide 6 AVL Tree An AVL (Adelson-Velskii and Landis 1962) 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 tree AVL property violated here

AVL Trees / Slide AVL Tree with Minimum Number of Nodes N 0 = 1 N 1 = 2 N 2 =4 N 3 = N 1 +N 2 +1=

AVL Trees / Slide 8 Smallest AVL tree of height Smallest AVL tree of height 8 Smallest AVL tree of height 9

AVL Trees / Slide 9 Height of AVL Tree Denote N h the minimum number of nodes in an AVL tree of height h N 0 =0, N 1 =2 (base) N h = N h-1 + N h-2 +1 (recursive relation) N > N h = N h-1 + N h-2 +1 >2 N h-2 >4 N h-4 > >2 i N h-2i If h is even, let i=h/2 1. The equation becomes N>2 h/2-1 N 2 N>2 h/2-1 x4 h=o(logn) If h is odd, let i=(h-1)/2. The equation becomes N>2 (h-1)/2 N 1 N>2 (h-1)/2 x2 h=o(logn) Thus, many operations (i.e. searching) on an AVL tree will take O(log N) time

AVL Trees / Slide 10 Insertion in AVL Tree Basically follows insertion strategy of binary search tree But may cause violation of AVL tree property Restore the destroyed balance condition if needed 6 8 Original AVL tree 6 Insert 6 Property violated Restore AVL property

AVL Trees / Slide 11 Some Observations After an insertion, only nodes that are on the path from the insertion point to the root might have their balance altered Because only those nodes have their subtrees altered Rebalance the tree at the deepest such node guarantees that the entire tree satisfies the AVL property 6 8 6 Node 5,8, might Rebalance node have balance altered guarantees the whole tree be AVL

AVL Trees / Slide 12 Different Cases for Rebalance Denote the node that must be rebalanced α Case 1: an insertion into the left subtree of the left child of α Case 2: an insertion into the right subtree of the left child of α Case 3: an insertion into the left subtree of the right child of α Case 4: an insertion into the right subtree of the right child of α Cases 1&4 are mirror image symmetries with respect to α, as are cases 2&3

AVL Trees / Slide 13 Rotations Rebalance of AVL tree are done with simple modification to tree, known as rotation Insertion occurs on the outside (i.e., left-left or right-right) is fixed by single rotation of the tree Insertion occurs on the inside (i.e., left-right or right-left) is fixed by double rotation of the tree

AVL Trees / Slide 14 Insertion Algorithm 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 Note: once we perform a rotation at a node x, we won t need to perform any rotation at any ancestor of x.

AVL Trees / Slide 15 Single Rotation to Fix Case 1(left-left) k2 violates An insertion in subtree X, AVL property violated at node k2 Solution: single rotation

AVL Trees / Slide 16 Single Rotation Case 1 Example k2 k1 k1 X k2 X

AVL Trees / Slide 1 Single Rotation to Fix Case 4 (right-right) k1 violates An insertion in subtree Z Case 4 is a symmetric case to case 1 Insertion takes O(Height of AVL Tree) time, Single rotation takes O(1) time

AVL Trees / Slide 18 Single Rotation Example Sequentially insert 3, 2, 1, 4, 5, 6 to an AVL Tree 2 3 Insert 3, 2 2 1 4 2 3 2 1 3 1 Single rotation Insert 1 violation at node 3 2 1 4 2 1 3 Insert 4 4 2 1 3 Insert 5, violation at node 3 4 2 5 4 5 3 Single rotation 5 3 Insert 6, violation at node 2 5 6 1 3 Single rotation 6

AVL Trees / Slide 19 If we continue to insert, 16, 15, 14, 13, 12, 11, 10, 8, 9 4 4 2 5 2 6 1 3 6 1 3 5 Insert, violation at node 5 4 2 6 Single rotation 4 2 6 1 3 5 Insert 16, fine Insert 15 violation at node 15 16 1 3 5 Single rotation But. Violation remains 15 16

AVL Trees / Slide 20 Single Rotation Fails to fix Case 2&3 Case 2: violation in k2 because of insertion in subtree Y Single rotation result Single rotation fails to fix case 2&3 Take case 2 as an example (case 3 is a symmetry to it ) The problem is subtree Y is too deep Single rotation doesn t make it any less deep

AVL Trees / Slide 21 Double Rotation to Fix Case 2 (left-right) Facts Double rotation to fix case 2 The new key is inserted in the subtree B or C The AVL-property is violated at k 3 k 3 -k 1 -k 2 forms a zig-zag shape Solution We cannot leave k 3 as the root The only alternative is to place k 2 as the new root

AVL Trees / Slide 22 Double Rotation to fix Case 3(right-left) Facts Double rotation to fix case 3 The new key is inserted in the subtree B or C The AVL-property is violated at k 1 k 2 -k 3 -k 2 forms a zig-zag shape Case 3 is a symmetric case to case 2

AVL Trees / Slide 23 Restart our example We ve inserted 3, 2, 1, 4, 5, 6,, 16 We ll insert 15, 14, 13, 12, 11, 10, 8, 9 4 4 2 6 2 6 1 3 5 k1 1 3 5 15 k2 Insert 16, fine Insert 15 violation at node 16 k3 15 k2 Double rotation k1 16 k3

AVL Trees / Slide 24 4 4 2 6 A k1 2 k2 1 3 5 15 k3 1 3 6 k1 15 k3 k2 16 D 5 14 16 Insert 14 14 C Double rotation X 4 k1 2 k2 4 15 1 3 6 15 2 6 14 16 Insert 13 5 Y 13 14 Z 16 1 3 5 13 Single rotation

AVL Trees / Slide 25 4 15 4 15 2 6 14 16 2 6 13 16 1 3 5 13 1 3 5 12 14 Insert 12 12 Single rotation 4 15 4 13 2 6 13 16 2 6 12 15 1 3 5 12 14 Insert 11 11 1 3 5 11 14 16 Single rotation

AVL Trees / Slide 26 4 13 4 13 2 6 12 15 2 6 11 15 1 3 5 11 14 16 Insert 10 10 1 3 5 10 12 14 16 Single rotation 4 13 2 6 11 15 1 3 5 10 12 14 16 9 8 Insert 8, fine then insert 9 4 13 2 6 11 15 1 3 5 8 12 14 16 9 10 Single rotation