Section 4 SOLUTION: AVL Trees & B-Trees

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

CS350: Data Structures Red-Black Trees

Recall: Properties of B-Trees

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

Analysis of Algorithms

Data Structures in Java

CS350: Data Structures B-Trees

Data Structures and Algorithms

Self-Balancing Search Trees. Chapter 11

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

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

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

The priority is indicated by a number, the lower the number - the higher the priority.

CS 380 ALGORITHM DESIGN AND ANALYSIS

Balanced Binary Search Trees. Victor Gao

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Binary Search Trees. Analysis of Algorithms

CS 350 : Data Structures B-Trees

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Trees. Eric McCreath

CS102 Binary Search Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Sample Exam 1 Questions

9. Heap : Priority Queue

What is a Multi-way tree?

Algorithms. AVL Tree

Balanced Search Trees

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

UNIT III BALANCED SEARCH TREES AND INDEXING

CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES WEEK - 7

Balanced Binary Search Trees

CS350: Data Structures AVL Trees

Properties of red-black trees

CS 206 Introduction to Computer Science II

Augmenting Data Structures

COMP171. AVL-Trees (Part 1)

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

Algorithms. Deleting from Red-Black Trees B-Trees

CSE 326: Data Structures B-Trees and B+ Trees

Chapter 20: Binary Trees

Programming II (CS300)

CSCI2100B Data Structures Heaps

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

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 Heaps and Priority Queues

Data Structures and Algorithms

Search Trees. COMPSCI 355 Fall 2016

CS 171: Introduction to Computer Science II. Binary Search Trees

AVL trees and rotations

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

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

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

Final Examination CSE 100 UCSD (Practice)

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Trees Heaps And Complexity

Priority Queues and Binary Heaps

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

Balanced search trees

Sample Solutions CSC 263H. June 9, 2016

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Binary Trees, Binary Search Trees

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

13.4 Deletion in 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

Binary Trees

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

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

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

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

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

13.4 Deletion in red-black trees

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

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

Motivation for B-Trees

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CH 8. HEAPS AND PRIORITY QUEUES

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

Data Structures and Algorithms

Sorting and Searching

Trees. A tree is a directed graph with the property

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

CS F-11 B-Trees 1

CH. 8 PRIORITY QUEUES AND HEAPS

Lecture 13: AVL Trees and Binary Heaps

Priority Queues (Heaps)

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CSC 421: Algorithm Design Analysis. Spring 2013

Lecture 6: Analysis of Algorithms (CS )

CSCI2100B Data Structures Trees

TREES. Trees - Introduction

Priority Queues Heaps Heapsort

Programming Abstractions

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

CS S-08 Priority Queues Heaps 1

Transcription:

Section 4 SOLUTION: AVL Trees & B-Trees 1. What 3 properties must an AVL tree have? a. Be a binary tree b. Have Binary Search Tree ordering property (left children < parent, right children > parent) c. Be a balanced tree: n.left.height n.right.height 1 for all nodes n in the tree. 2. Draw an AVL tree of height 4 that contains the minimum possible number of nodes. Construct a minimum size AVL tree of height h by creating a new root, and making one of its children a minimum AVL tree of height h-1, and the other a minimum AVL tree of h-2. So to get a minimum AVL tree of height 4, we need to build up minimum AVL trees of heights 0-3 first. The image below shows each of these, and finally a minimum AVL tree of height 4. Values are left out here, but any valid BST values could be filled in. Note that there are many different possible orderings of branches (left versus right); the ones shown below are one way to do it. The minimum number of nodes is 12. 3. In a typical BST, inserting keys in order result in a worst-case height. Show the result when an initially empty AVL tree has keys 1 through 7 inserted in order. 4 2 6 1 3 5 7 4. For the following AVL tree: a. What values could you insert to cause a right-right imbalance, and at which node does the imbalance occur? Inserting any value greater than 20 causes a right-right imbalance at node, the root, because the root s left child now has height 1 and the right child has height 3. b. How about a right-left imbalance? At which node does the imbalance occur? Inserting any value GREATER than and LESS than 20 causes a right-left imbalance also at the root, for the same reason.

c. Insert 18 into the following AVL tree. What type of imbalance does it cause? Show the result after balancing. Inserting 18 causes a right-left imbalance at the root node, because its left child is of height 1 5 20 2 8 15 25 13 17 23 18 and its right child is of height 3, meaning the difference is 2, which is greater than the allowed difference of 1. To fix this node, you need to do a double rotation. First, rotate grandchild of with child of, 5 15 2 8 13 17 20 25 18 23 that is, rotate 15 with 20 clockwise, making sure to re-assign 13, subtree starting at 17, and subtree starting at 25 correctly. 15 20 5 13 17 25 2 8 23

Now do your second rotation, rotate the node counterclockwise where the imbalance was found (node ) with its imbalance inducing child 15. 5. AVL tree balance violation cases: a. Insert the following keys, in order, into an initially empty AVL tree: 12, 8, 9, 20,, 15, 3, 11, 5. 12 9 20 5 15 3 8 11 b. Find a key we could insert into your resulting tree that would result in a case 1 balance violation (left-left). There are several keys we could insert to get a case 1 rotation; inserting 1, for instance, will cause a height imbalance to be detected at the root. 6. Given a binary search tree, describe how you could convert it into an AVL tree with worst-case time O(nlogn) and best case O(n). We can traverse the tree in O(n) time and insert each element into an initially empty AVL tree; this will take O(nlogn) time overall. To get O(n) best-case performance we can do something that s a bit of a hack: try to verify that the BST is a valid AVL tree, which we can do in O(n) time; if it is, return it as it is. Otherwise, create a new AVL tree as described. It s questionable as to whether this is really a bestcase result, as we don t actually do any conversion, only verification. 7. Say you work for a software company, and your boss wants you to implement priority-queue-like functionality for a project you are working on, and he suggests you just use the AVL tree you already have implemented, since you can findmin/insert/deletemin all run in O(logn) time guaranteed anyway. a. Give a couple of reasons why you may want to go with a binary min heap instead. Some reasons: i. Since we re implementing a priority queue, finding a min on a heap is always O(1), whereas finding the min on a AVL tree is O(logn). ii. Although they have a O(logn) insert as well, binary heaps have an average O(1) expected time for inserts, since ½ of the elements are leaves, and most elements are in the bottom two levels. iii. We ll likely get better caching performance from the binary heap, as it s stored as an array iv. AVL tree pointers take up additional memory

b. Regarding the situation in part (a), under what additional circumstances may the AVL be preferable? If we want to perform other operations, like finding a patient record based on their name rather than their priority, etc., an AVL tree that used patient names as the key would be useful. Also, an AVL tree will use extra the memory it needs, whereas a (potentially large) portion of the array for a binary heap may go unused; depending on the circumstances, it s possible that the binary heap could use more memory. 8. B-Tree a. What constraints do the following values impose on a B-Tree: M=32, L=16? Each node can have at most M children, and must have a minimum of M/2 children, and each leaf can have at most L data items, and at least L/2 data items. (rounding-up) So, a tree with M=32 and L=16 must have 16-32 children at each internal node, and must have 8-16 items at each leaf. (excepting the first 7 insertions b. Insert the following into an empty B tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20. c. Delete 17, 12, 22, 5 & 36 9. Given the following parameters for a B-tree with M= 11 and L = 8 Key Size = bytes Pointer Size = 2 bytes Data Size = 16 bytes per record (includes the key) Assuming that M and L were chosen appropriately, what is the likely size of a disk block on the machine where this implementation will be deployed? Give a numeric answer and a short justification based on two equations using the parameter values above.

Internal Node size = M * pointer + (M - 1) * key = 11 * 2 + (11-1) * = 22 + 0 = 122 Leaf Node Size = L * data = 8 * 16 = 128 Max of these is 128 (also a power of 2) so disk block is likely to be 128 bytes