Data Structures and Algorithms

Similar documents
CSI33 Data Structures

Algorithms. AVL Tree

Data Structures in Java

lecture17: AVL 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

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

CS350: Data Structures AVL Trees

AVL trees and rotations

CSCI2100B Data Structures Trees

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

COMP171. AVL-Trees (Part 1)

Data Structures Lesson 7

CS 261 Data Structures. AVL Trees

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

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

More BSTs & AVL Trees bstdelete

CISC 235: Topic 4. Balanced Binary Search Trees

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

CS Transform-and-Conquer

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Trees. Reading: 9.2

CS 206 Introduction to Computer Science II

Lecture 16 Notes AVL Trees

AVL Trees Heaps And Complexity

Analysis of Algorithms

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

DATA STRUCTURES AND ALGORITHMS

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

Binary search trees (chapters )

LECTURE 18 AVL TREES

CSI33 Data Structures

AVL Trees. See Section 19.4of the text, p

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

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

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

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

Binary search trees (chapters )

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

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

3137 Data Structures and Algorithms in C++

Fundamental Algorithms

AVL trees and rotations

Part 2: Balanced Trees

CMPE 160: Introduction to Object Oriented Programming

CS350: Data Structures Red-Black Trees

CSE 373: AVL trees. Michael Lee Friday, Jan 19, 2018

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

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

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

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

Balanced Binary Search Trees. Victor Gao

Balanced Search Trees. CS 3110 Fall 2010

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

Trees. Eric McCreath

Lecture 16 AVL Trees

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

Balanced Binary Search Trees

Section 4 SOLUTION: AVL Trees & B-Trees

Dynamic Access Binary Search Trees

MA/CSSE 473 Day 20. Recap: Josephus Problem

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

Search Structures. Kyungran Kang

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

Data Structures and Algorithms(12)

Lecture 13: AVL Trees and Binary Heaps

CE 221 Data Structures and Algorithms

Dynamic Access Binary Search Trees

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

Search Trees. Chapter 11

AVL Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

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

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

Ch04 Balanced Search Trees

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

Week 3 Web site:

Module 4: Priority Queues

Data Structures and Algorithms

Advanced Tree Data Structures

AVL Tree. Idea. The performance (Search, Insertion, Deletion):

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

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.

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

CS 206 Introduction to Computer Science II

Data Structures Week #6. Special Trees

When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N).

Self-Balancing Search Trees. Chapter 11

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

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.

Data Structures Week #6. Special Trees

A dictionary interface.

COMP Analysis of Algorithms & Data Structures

CS 310: Tree Rotations and AVL Trees

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

COMP Analysis of Algorithms & Data Structures

Balanced Binary Search Trees

CSC 421: Algorithm Design Analysis. Spring 2013

Data Structures and Algorithms Lecture 7 DCI FEEI TUKE. Balanced Trees

Algorithms and Data Structures

CSC148 Week 7. Larry Zhang

Data Structures Week #6. Special Trees

Transcription:

Data Structures and Algorithms Spring 2009-2010

Outline BST Trees (contd.) 1 BST Trees (contd.)

Outline BST Trees (contd.) 1 BST Trees (contd.)

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: M D N B Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Q

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: M AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder D

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

The bad news about BSTs... Problem with BSTs is that there is nothing to stop them going badly skewed An AVL (Adelson-Velskii and Landis) tree is a BST with a height balance condition (There exist other balancing strategies based on the weight of a tree) Cannot just insist that the root be balanced: Also, cannot insist that every node have an equal height left and right subtree: AVL requirement is that the height of every left and right subtree can differ by at most 1 Height information is kept in tree_node object Insertions and deletions now become harder

AVL Tree Example BST Trees (contd.) M M M D N D N D N B G B G B G O H H AVL Tree Not an AVL Tree AVL Tree In practise an AVL tree on n nodes has height of about O(log(n + 1)) + 0.25 Compare to best BST (CBBST) which has height k = log(n + 1) 1 The worst the height can be is 1.44 log n found by reasoning on what is the minimum number of nodes that a tree of height, h, can have; we will return to this problem later

: Insertions When we insert a node in to an AVL tree we need to ensure that the tree remains balanced after the insertion Consider inserting values 1 to 7 in to an empty AVL Inserting values 1 and 2 are easy, but 3... Inserting 6:

: Insertions When we insert a node in to an AVL tree we need to ensure that the tree remains balanced after the insertion Consider inserting values 1 to 7 in to an empty AVL Inserting values 1 and 2 are easy, but 3... Inserting 6:

: Insertions When we insert a node in to an AVL tree we need to ensure that the tree remains balanced after the insertion Consider inserting values 1 to 7 in to an empty AVL Inserting values 1 and 2 are easy, but 3... 1 2 2 Tree is unbalanced at node 1 Inserting 6: 3 1 3 Rotate tree about node 2 to accommodate node 3

: Insertions When we insert a node in to an AVL tree we need to ensure that the tree remains balanced after the insertion Consider inserting values 1 to 7 in to an empty AVL Inserting values 1 and 2 are easy, but 3... Inserting 6: 2 4 1 4 2 5 3 5 1 3 6 Unbalanced at node 2 6 Rotate tree about node 4 to accommodate node 6

: Single Rotations k 2 k 1 k 1 Z X k 2 X Y Y Z If the height of k 2 s left subtree is two more than the right subtree then there is a left imbalance and we perform a left rotation, giving the picture on the right If the height of k 1 s right subtree is two more than the left subtree then there is a right imbalance and we perform a right rotation, giving the picture on the left

: Insertions (contd.) After inserting 7, suppose we want to insert 15, 14,..., 8 Inserting 14: A single rotation will not repair the balancedness of the tree so we need to perform a deeper twist This is called a double rotation There are two types of double rotation: right-left rotations and left-right rotations (think in terms of imbalanced side) A double rotation is the composition of two single rotations

: Insertions (contd.) After inserting 7, suppose we want to insert 15, 14,..., 8 Inserting 14: Single Rotation 6 6 5 7 5 15 15 7 14 A single rotation will not repair the balancedness of the tree so we need to perform a deeper twist This is called a double rotation There are two types of double rotation: right-left rotations and left-right rotations (think in terms of imbalanced side) A double rotation is the composition of two single rotations 14

: Insertions (contd.) After inserting 7, suppose we want to insert 15, 14,..., 8 Inserting 14: A single rotation will not repair the balancedness of the tree so we need to perform a deeper twist 4 2 6 6 1 3 5 7 5 14 15 7 15 14 This is called a double rotation There are two types of double rotation: right-left rotations and left-right rotations (think in terms of imbalanced side) A double rotation is the composition of two single rotations

: Insertions (contd.) After inserting 7, suppose we want to insert 15, 14,..., 8 Inserting 14: A single rotation will not repair the balancedness of the tree so we need to perform a deeper twist 4 2 6 6 1 3 5 7 5 14 15 7 15 14 This is called a double rotation There are two types of double rotation: right-left rotations and left-right rotations (think in terms of imbalanced side) A double rotation is the composition of two single rotations

: Rotations (contd.) A left-right rotation is the composition of a right rotation on the subtree rooted at k 1 followed by a left rotation on the subtree rooted at k 3 (upper case on picture over) A right-left rotation is the composition of a left rotation on the subtree rooted at k 3 followed by a right rotation on the subtree rooted at k 1 (lower case on picture over)

: Rotations (contd.) A left-right rotation is the composition of a right rotation on the subtree rooted at k 1 followed by a left rotation on the subtree rooted at k 3 (upper case on picture over) A right-left rotation is the composition of a left rotation on the subtree rooted at k 3 followed by a right rotation on the subtree rooted at k 1 (lower case on picture over)

: Rotations (contd.) k 3 W k 1 k 2 Z Left Right double rotation X Y k 2 k 1 k 3 k 1 W X Y Z W k 2 k 3 Z Right Left double rotation X Y

AVL Tree: Deletions BST Trees (contd.) Just as with BSTs, deletions are similar to, but more tricky than, insertions The actual deletion is simply that described earlier for the BST However, shifting the right subtree s leftmost descendant to maintain the inorderedness may cause an imbalance Restoring the balance (through rotations) may cause imbalance further up the tree While it will never take more than one single or double rotation to rebalance a tree after an insertion, we may need h 2 rotations to rebalance a tree of height h after a deletion