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

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

Algorithms. AVL Tree

CSI33 Data Structures

Data Structures Lesson 7

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

More BSTs & AVL Trees bstdelete

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

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

Data Structures in Java

AVL Trees. See Section 19.4of the text, p

CS350: Data Structures Red-Black Trees

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

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

CS350: Data Structures AVL Trees

3137 Data Structures and Algorithms in C++

COMP171. AVL-Trees (Part 1)

8. Binary Search Tree

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

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

Balanced Search Trees. CS 3110 Fall 2010

CISC 235: Topic 4. Balanced Binary Search 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

Search Trees - 1 Venkatanatha Sarma Y

Section 4 SOLUTION: AVL Trees & B-Trees

CS350: Data Structures AA Trees

Ch04 Balanced Search Trees

Binary Trees, Binary Search Trees

Fundamental Algorithms

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

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

Binary Search Trees. Analysis of Algorithms

Analysis of Algorithms

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

Analysis of Algorithms

Part 2: Balanced Trees

CSCI2100B Data Structures Trees

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

Dynamic Access Binary Search Trees

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

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

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

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

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

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.

Search Structures. Kyungran Kang

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

COMS 3137 Class Notes

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

CHAPTER 10 AVL TREES. 3 8 z 4

CS 261 Data Structures. AVL Trees

AVL Trees Heaps And Complexity

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

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

COMP Analysis of Algorithms & Data Structures

CS350: Data Structures B-Trees

CS Transform-and-Conquer

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications

Balanced Search Trees

AVL Trees (10.2) AVL Trees

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

Splay Trees. Splay Trees 1

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Data Structures and Algorithms

Chapter 10: Search Trees

COMP Analysis of Algorithms & Data Structures

Dynamic Access Binary Search Trees

Splay Trees Goodrich, Tamassia, Dickerson. Splay Trees 1

Lesson 21: AVL Trees. Rotation

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary search trees (chapters )

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

Self-Balancing Search Trees. Chapter 11

LECTURE 18 AVL TREES

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

CS200: Balanced Search Trees

CSI33 Data Structures

COMP Analysis of Algorithms & Data Structures

Module 4: Dictionaries and Balanced Search Trees

Augmenting Data Structures

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications

CS 350 : Data Structures B-Trees

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

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

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

Multi-Way Search Trees

Trees. Prof. Dr. Debora Weber-Wulff

Multi-Way Search Trees

Lecture 6: Analysis of Algorithms (CS )

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

AVL trees and rotations

What is a Multi-way tree?

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

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

Practical session No. 6. AVL Tree

Midterm solutions. n f 3 (n) = 3

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Transcription:

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

Outline I. Balanced Trees I. AVL Trees I. Balance Constraint II. Examples III. Searching IV. Insertions V. Removals

Balancing a Tree Binary trees lack a depth constraint. As a result the worst case insertion, removal and retrieval times are O(n).

Balanced Trees: Time Complexity Implications If a tree is balanced in some sense. The time complexity of insertions, removals and retrievals will have a logarithmic bound.

AVL Trees AVL trees Binary Search Tree with added balance constraint Adelson, Veliskii and Landis Definition: AVL constraint For any node in the tree, the height of the left and subtrees can differ in height only by one. Uses weak notion of balance Sufficient to ensure logarithmic depth in worst case!

Example AVL trees

Logarithmic Depth AVL trees are guaranteed to have logarithmic depth. As a result, the worst case time complexities for insert, removal and retrieve is O(log 2 n). Proof idea: Prove the height can be represented as a logarithmic function of n the number of nodes. By definition, the minimum number of nodes is in AVL tree of height h is defined by recurrence AVL h : AVL h = AVL h 1 + AVL h 2 + 1 AVL 0 = 0 AVL 1 = 1 Solving and bounding this recurrence we get : h < 1.4 log 2 AVL h + 2 See Appendix A.5 Thus worst case operations based on traversing the height h of the tree are O(log 2 AVL h )

The Valid State of A Balanced Tree Operations on an AVL tree may result in an invalid state. Insert Nodes along the path from the inserted node to the root might violate the balance condition. Remove Nodes along the path from the replacement node to the root might violate the balance condition. Re-balancing must be incorporated into these operations.

Insert into AVL Tree Assume node thisnode is the deepest node that violates the balance constraint and must be rebalanced. The violation may occur as a result of 1 of 4 Cases: Case 1: Insertion into left subtree of the leftchild of thisnode Case 2: Insertion into the right subtree of the leftchild of thisnode Case 3: Insertion into the left subtree of the rightchild of thisnode Case 4: Insertion into right subtree of the rightchild of thisnode

Balancing by Single Rotation Case 1 // rotate AVL tree single rotation // solution for Case 1: Insertion into left subtree of the leftchild of thisnode // performs rotation and returns new root of subtree, oldleftchild function rotatewithleftchild thisnode oldleftchild thisnode. leftchild thisnode. leftchild oldleftchild. rightchild oldleftchild. rightchild thisnode return oldleftchild

Balancing by Single Rotation Case 4 // rotate AVL tree single rotation // solution for Case 4: Insertion into right subtree of the rightchild of thisnode // performs rotation and returns new root of subtree, oldrightchild function rotatewithrightchild thisnode oldrightchild thisnode. rightchild thisnode. rightchild oldrightchild. leftchild oldrightchild. leftchild thisnode return oldrightchild

Balancing by Single Rotation

Single Rotation fails when inner subtree is largest

Double Rotations. Example: Double rotation balances case 2 (and 3) Rotate between thisnode s child and grandchild Rotate between thisnode and its new child

Double rotate with left child: Case 2 // rotate AVL tree single rotation // solution for Case 2: // performs double rotation and returns new root of subtree function doublerotatewithleftchild thisnode thisnode. leftchild rotatewithrightchild thisnode. leftchild return rotatewithleftchild thisnode

Example: Balancing by Double Rotation

Double rotate with right child: Case 3 3 // rotate AVL tree single rotation // solution for Case 3 // performs double rotation and returns new root of subtree function doublerotatewithrightchild thisnode thisnode. rightchild rotatewithleftchild thisnode. rightchild return rotatewithrightchild thisnode

Insert into AVL Tree // insert key in AVL tree rooted at root function insertavl(&root, key) if root is NULL // base case: insert node here! root new node root. key key if root. key == key, then return // assume no duplicates if root. key > key // continue down left subtree insertavl(root. leftchild, key) if height root. leftchild height root. rightchild == 2 if root. leftchild. key > key // case 1 root rotatewithleftchild(root) else // case 2 root doublerotatewithleftchild(root) if root. key < key // continue down right subtree insertavl(root. rightchild, key) if height root. leftchild height root. rightchild == 2 if root. leftchild. key < key // case 4 root rotatewithrightchild(root) else // case 3 root doublerotatewithrightchild(root) Note: root must be passed by reference in this implementation! This allows for appropriate linking to parents for base case and rotations Code facilitating insertion shown in blue Code enforcing balance shown in orange

Try this at home Note: we must check the height of each subtree to check for balance. Change the insert pseudocode so we do not need to recompute the height during each step of traversal for an insert! In the pseudocode provided, this was done by calling a height function This is slow! How slow? Instead a height attribute -- AKA balance factor -- should be maintained at each node and updated during any insertion or removal.

Balance Factor Balance Factor. Stored at each node in AVL tree balancefactor = height of right subtree less height of left subtree Valid values are -1, 0, and +1. During insertion and removal, all affected balance factors must be updated Balance factors are then checked to determine if rotation is necessary

AVL Tree: Remove Removal is similar to insertion: perform alteration, then rebalance Rebalance using single and double rotations Difference: imbalance may propagate upwards, rotations at multiple nodes along the path to the root may be necessary

Cases for Removal from AVL tree Similar to cases for standard BST Delete node from AVL tree Case 0: 0 children. delete node. Case 1: 1 child. Connect child to parent. Case 2: 2 children. Choose replacement node (e.g. min val in right subtree) and make the appropriate substitution. re-balance as needed Case 0: all nodes from removed node to root must be checked Case 1: all nodes from removed node to root must be checked Case 2: all nodes from deleted replacement node to root must be checked Can rebalance while returning along the path.

Remove Example

Try at home Create pseudocode for AVL node removal Cases and reconnection of separated tree similar to BST node removal Height condition checks and rotations similar to AVL insert

Time Complexity including Balance Search: Θ(log 2 n) Note: Height Balancing only adds constant factor to complexity since Balancing is performed while simultaneously traversing the tree for insert or remove ie the traversal is free, and each balance case in done in a constant number of operations. Should keep height attribute at each node; otherwise complexity may increase Insertion: Θ(log 2 n) Removal: Θ(log 2 n)

Summary: AVL Trees Efficient Constraint ensures logarithmic depth Theoretically fast, but rebalancing will decrease execution by a constant factor. Other Notes: For large trees, the log time will be a huge savings But there are practical issues related to cache, memory, and other delays to consider For example linear search time (without any memory delays) may be better than log search time (with memory delays). B-Trees