Data Structures in Java

Similar documents
CS350: Data Structures AVL Trees

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

CS 206 Introduction to Computer Science II

CS350: Data Structures Red-Black Trees

Algorithms. AVL Tree

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

Self-Balancing Search Trees. Chapter 11

Data Structures and Algorithms

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

COMS 3137 Class Notes

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

COMP171. AVL-Trees (Part 1)

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

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

Section 4 SOLUTION: AVL Trees & B-Trees

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

CS350: Data Structures AA Trees

CHAPTER 10 AVL TREES. 3 8 z 4

Data Structures Lesson 7

Balanced Binary Search Trees

CSI33 Data Structures

Balanced Search Trees. CS 3110 Fall 2010

AVL Trees (10.2) AVL Trees

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

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

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

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

Lecture 16 Notes AVL Trees

Data Structure - Advanced Topics in Tree -

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

AVL trees and rotations

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

Search Trees - 1 Venkatanatha Sarma Y

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Recall: Properties of B-Trees

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

Chapter 12 Advanced Data Structures

Lecture 16 AVL Trees

Introduction to Programming: Lecture 13

Data Structures and Algorithms for Engineers

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

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

CS 206 Introduction to Computer Science II

Algorithms. Deleting from Red-Black Trees B-Trees

More BSTs & AVL Trees bstdelete

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CS Transform-and-Conquer

DATA STRUCTURES AND ALGORITHMS

Analysis of Algorithms

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

CISC 235: Topic 4. Balanced Binary Search Trees

Multi-Way Search Trees

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Multi-Way Search Trees

Trees. Eric McCreath

Binary Search Trees. Analysis of Algorithms

CS 380 ALGORITHM DESIGN AND ANALYSIS

LECTURE 18 AVL TREES

Balanced Binary Search Trees. Victor Gao

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

AVL Trees Heaps And Complexity

Part 2: Balanced Trees

Advanced Set Representation Methods

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

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

Data Structures in Java

Programming II (CS300)

CS350: Data Structures B-Trees

Recall from Last Time: AVL Trees

Data Structures and Algorithms

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

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

Binary search trees (chapters )

CE 221 Data Structures and Algorithms

Lesson 21: AVL Trees. Rotation

Balanced search trees

9. Heap : Priority Queue

AVL Trees. Reading: 9.2

COMP Analysis of Algorithms & Data Structures

Chapter 10: Search Trees

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

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

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

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

13.4 Deletion in red-black trees

CS 350 : Data Structures B-Trees

DATA STRUCTURES AND ALGORITHMS

Augmenting Data Structures

COMP Analysis of Algorithms & Data Structures

Ch04 Balanced Search Trees

Search Trees. COMPSCI 355 Fall 2016

CS 261 Data Structures. AVL Trees

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

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

AVL Trees. See Section 19.4of the text, p

Binary search trees (chapters )

Balanced Binary Search Trees

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

Data Structures (CS 1520) Lecture 23 Name:

Transcription:

Data Structures in Java Lecture 10: AVL Trees. 10/1/015 Daniel Bauer

Balanced BSTs Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero children). Then the height of the tree will be O(log N) and all BST operations will run in O(log N).

AVL Tree Condition An AVL Tree is a Binary Search Tree in which the following balance condition holds after each operation: For every node, the height of the left and right subtree differs by at most 1. 3 5 7 3 1 1 1 4 7 8 1 0 3 3 5 1 1 1 0 1 1 4 8 not an AVL tree

AVL Trees Height of an AVL tree is at most ~ 1.44 log(n+)-1.38 = O(log N) How to maintain the balance condition? Rebalance the tree after each change (insertion or deletion). Rebalancing must be cheap.

Outside Imbalance node k violates the balance condition k k k1 z x k1 x y y z left subtree of left child too high right subtree of right child too high Solution: Single rotation

Inside Imbalance node k violates the balance condition k k k1 z x k1 x y Y z right subtree of left child too high left subtree of right child too high Solution: Double rotation

Single Rotation k x k1 y z Maintain BST property: x is still left subtree of k1. z is still right subtree of k. For all values v in y: k1 < v < k so y becomes new left subtree of k.

Single Rotation k1 k x y z Maintain BST property: x is still left subtree of k1. z is still right subtree of k. For all values v in y: k1 < v < k so y becomes new left subtree of k.

Single Rotation x k1 y k z Maintain BST property: x is still left subtree of k1. Modify 3 references: k.left = k1.right k1.right = k parent(k).left = k1 or parent(k).right = k1 or z is still right subtree of k. For all values v in y: k1 < v < k so y becomes new left subtree of k.

Maintaining Balance in an AVL Tree Assume the tree is balanced. After each insertion, find the lowest node k that violates the balance condition (if any). Perform rotation to re-balance the tree. Rotation maintains original height of subtree under k before the insertion. No further rotations are needed.

Single Rotation Example insert(3) 3

Single Rotation Example insert(3) insert() 3

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 3

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 3

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 3 insert(4) 4

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 3 insert(4) 4 insert(5) rotate_right(3) 5

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 4 insert(4) 3 5 insert(5) rotate_right(3)

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 1 4 insert(4) 3 5 insert(5) insert(6) rotate_right(3) rotate_right() 6

Single Rotation Example insert(3) 4 insert() insert(1) rotate_left(3) 5 insert(4) 1 3 6 insert(5) rotate_right(3) insert(6) rotate_right()

Single Rotation Example insert(3) 4 insert() insert(1) rotate_left(3) 5 insert(4) 1 3 6 insert(5) insert(6) rotate_right(3) rotate_right() 7 insert(7) rotate_right(5)

Single Rotation Example insert(3) insert() insert(1) rotate_left(3) 4 insert(4) 1 3 5 6 7 insert(5) insert(6) rotate_right(3) rotate_right() insert(7) rotate_right(5)

Single Rotation does not work for Inside Imbalance k k1 z x y

Single Rotation does not work for Inside Imbalance k1 x y k z Result is not an AVL tree. Now k1 is violates the balance condition. Problem: Tree y cannot move and it is too high.

Double Rotation (1) y is non-empty (imbalance due to insertion into y or deletion from z) so we can view y as a root and two sub-trees. k3 k1 z x y

Double Rotation (1) y is non-empty (imbalance due to insertion into y or deletion from z) so we can view y as a root and two sub-trees. k3 k1 z x k yl yr either yl or yr is two levels deeper than z (or both are empty).

Double Rotation () k3 k1 z Maintain BST property: x is still left subtree of k1. z is still right subtree of k3. x yl k yr For all values v in yl: k1 < v < k so yl becomes new right subtree of k1. For all values w in yr: k < w < k3 so yr becomes new left subtree of k3.

Double Rotation () k k1 k3 Maintain BST property: x yl yr z x is still left subtree of k1. z is still right subtree of k3. For all values v in yl: k1 < v < k so yl becomes new right subtree of k1. For all values w in yr: k < w < k3 so yr becomes new left subtree of k3.

Double Rotation () These are actually two single rotations: First at k1, then at k3. k3 k1 z x k yl yr

Double Rotation () These are actually two single rotations: First at k1, then at k3. k3 k z k1 yr x yl

Double Rotation () These are actually two single rotations: First at k1, then at k3. k k1 k3 x yl yr z

Double Rotation (3) Modify 5 references: k parent(k3).left = k or parent(k3).right = k k1 k3 k.left = k1 k.right = k3 k1.right = root(yl) x yl yr z k3.left = root(yr)

Double Rotation Example 4 6 1 3 5 7

Double Rotation Example insert(16) 4 6 1 3 5 7 16

Double Rotation Example insert(16) insert(7) rotate(7) 4 6 1 3 5 7 16 15

Double Rotation Example insert(16) insert(7) rotate(7) 4 6 k1 1 3 5 7 x k3 16 k z 15 yl yr

Double Rotation Example insert(16) insert(7) rotate(7) 4 insert(14) rotate(6) 6 1 3 5 15 7 16 14

Double Rotation Example insert(16) insert(7) rotate(7) 4 insert(14) rotate(6) 6 k1 1 3 5 15 x k3 7 16 k z 14 yl yr

Double Rotation Example insert(16) insert(7) rotate(7) 4 insert(14) rotate(6) 7 1 3 6 15 5 14 16