Recall: Properties of B-Trees

Similar documents
Binary Heaps. CSE 373 Data Structures Lecture 11

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

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Administration CSE 326: Data Structures

Data Structures and Algorithms

CSCI2100B Data Structures Heaps

Sorting and Searching

Sorting and Searching

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

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

CS350: Data Structures Heaps and Priority Queues

Priority Queues Heaps Heapsort

Priority Queues. Binary Heaps

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Priority Queues Heaps Heapsort

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

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Data Structures and Algorithms

Section 4 SOLUTION: AVL Trees & B-Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

Priority Queues (Heaps)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

UNIT III BALANCED SEARCH TREES AND INDEXING

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Heap Model. specialized queue required heap (priority queue) provides at least

Binary Heaps in Dynamic Arrays

CSE 373: Data Structures and Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COMP : Trees. COMP20012 Trees 219

Course Review. Cpt S 223 Fall 2009

Algorithms and Data Structures

CE 221 Data Structures and Algorithms

Binary Trees, Binary Search Trees

Trees. A tree is a directed graph with the property

Exam Data structures DIT960/DAT036

9. Heap : Priority Queue

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Analysis of Algorithms

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

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

Data Structures and Algorithms for Engineers

Why Do We Need 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

CS F-11 B-Trees 1

Data Structures Lesson 9

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

Chapter 6 Heapsort 1

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

CSE 373 OCTOBER 25 TH B-TREES

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

Priority Queues (Heaps)

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CS350: Data Structures B-Trees

Midterm solutions. n f 3 (n) = 3

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

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues and Heaps

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

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

Balanced Binary Search Trees. Victor Gao

Priority Queues. 04/10/03 Lecture 22 1

Data Structures in Java

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

Algorithms and Theory of Computation. Lecture 7: Priority Queue

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Algorithms. AVL Tree

Algorithms and Data Structures

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

Priority Queues and Binary Heaps

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

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

CSE373: Data Structures & Algorithms Priority Queues

CMSC 341 Lecture 15 Leftist Heaps

Figure 1: A complete binary tree.

3137 Data Structures and Algorithms in C++

Course Review for Finals. Cpt S 223 Fall 2008

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

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

Data Structures in Java

CS 350 : Data Structures B-Trees

CMSC 341 Lecture 15 Leftist Heaps

Dynamic Access Binary Search Trees

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

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Programming II (CS300)

Dynamic Access Binary Search Trees

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

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

Analysis of Algorithms

Priority queues. Priority queues. Priority queue operations

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Binary Search Trees Treesort

Topic: Heaps and priority queues

CSE 530A. B+ Trees. Washington University Fall 2013

CompSci 201 Tree Traversals & Heaps

Transcription:

CSE 326 Lecture 10: B-Trees and Heaps It s lunch time what s cookin? B-Trees Insert/Delete Examples and Run Time Analysis Summary of Search Trees Introduction to Heaps and Priority Queues Covered in Chapters 4 and 6 in the text 1 Recall: Properties of B-Trees k 1... ki-1 k i... k M-1 T T T 1... i... M All keys in first subtree T 1 < k 1 All keys in subtree T i must be between k i-1 and k i k i-1 T i < k i All keys in last subtree T M k M-1 2

Inserting Items in B-Trees Insert X: Do a Find on X and find appropriate leaf node If leaf node is not full, fill in empty slot with X. E.g. Insert 5 in the tree below If leaf node is full, split leaf node and adjust parents up to root node. E.g. Insert 9 in the tree below 2-3 Tree 6:11 3 4-6 7 8 11 12-13 14-17 18-3 Deleting Items in B-Trees Delete X: Do a Find on X and delete value from leaf node May have to combine leaf nodes and adjust parents up to root node if number of data items falls below L/2 = 2 E.g. Delete 17 in the tree below 6:11 13:- 17:- 13:- 17:- 3 4-6 7 8 11 12-13 14-17 18-4

Run Time Analysis of B-Tree Operations For a B-Tree of order M 1. Each internal node has up to M-1 keys to search 2. Each internal node has between M/2 and M children 3. Each leaf stores between L/2 and L data items Depth d of B-Tree storing N data items is: log M/2 (N/L) - 1 d < log M/2 (N/L) i.e. d = O(log M/2 (N/L)) = O(log N) (Why? Hint: Draw a B-tree with minimum children at each node. Count its leaves as a function of depth) Find: Run time includes: O(log M) to binary search which branch to take at each node Total time to Find an item is O(depth*log M) = O(log N) 5 What about Insert/Delete? For a B-Tree of order M Depth of B-Tree storing N items is O(log M/2 N) Insert and Delete: Run time is: O(M) to handle splitting or combining keys in nodes Total time is O(depth*M) = O((log N/log M/2 )*M) = O((M/log M)*log N) How do we select M? 6

How do we select M and L? If Tree & Data in internal (main) memory want M and L to be small to minimize search time at each node/leaf Typically M = 3 or 4 (e.g. M = 3 is a 2-3 tree) All N items stored in internal memory If Tree & Data on Disk Disk access time dominates! Choose M & L so that interior and leaf nodes fit on 1 disk block To minimize number of disk accesses, minimize tree height Typically M = 32 to 256, so that depth = 2 or 3 allows very fast access to data in large databases. See Textbook for more numbers and examples. 7 Summary of Search Trees Problem with Search Trees: Must keep tree balanced to allow fast access to stored items AVL trees: Insert/Delete operations keep tree balanced Splay trees: Sequence of operations produces balanced trees Multi-way search trees (e.g. B-Trees): More than two children per node allows shallow trees; all leaves are at the same depth keeping tree balanced at all times 8

A New Problem Instead of finding any item (as in a search tree), suppose we want to find only the smallest (highest priority) item quickly. Examples: Operating system needs to schedule jobs according to priority Doctors in ER take patients according to severity of injuries Event simulation (bank customers arriving and departing, ordered according to when the event happened) We want an ADT that can efficiently perform: FindMin (or DeleteMin) Insert 9 Using the Data Structures we know Suppose we have N items. Lists If sorted: DeleteMin is O(1) but Insert is O(N) If not sorted: Insert is O(1) but DeleteMin is O(N) Binary Search Trees (BSTs) Insert is O(log N) and DeleteMin is O(log N) BSTs look good but BSTs designed to be efficient for Find, not just FindMin We only need FindMin/DeleteMin We can do better than BSTs! O(1) FindMin and O(log N) Insert. How? 10

Heaps A binary heap is a binary tree that is: 1. Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right 2. Satisfies the heap order property: every node is smaller than (or equal to) its children Therefore, the root node is always the smallest in a heap 2 6 4 7 8 0-1 0 1 1 2 6 3 4 5 Which of these is not a heap? 11 Array Implementation of Heaps Since heaps are complete binary trees, we can avoid pointers and use an array Recall our Array Implementation of Binary Trees: Root node = A[1] Children of A[i] = A[2i], A[2i + 1] Keep track of current size N (number of nodes) 2 4 6 7 5 0 1 2 3 4 5 6 7 N = 5 2 4 6 7 5 12

Heaps: FindMin and DeleteMin Operations FindMin: Easy! Return root value A[1] Run time =? DeleteMin: Delete (and return) value at root node We now have a Hole at the root Need to fill the hole with another value Replace with smallest child? Try replacing 2 with smallest child and that node with its smallest child, and so on what happens? 2 4 3 7 5 8 9 11 9 6 10 13 DeleteMin Take 1 DeleteMin: Delete (and return) value at root node We now have a Hole at the root Need to fill the hole with another value Replace with smallest child? Try replacing 2 with smallest child and so on what happens? Tree is no longer complete! Let s try another strategy 3 4 8 7 5 9 11 9 6 10 14

DeleteMin (Take 2) DeleteMin: Delete (and return) value at root node We now have a Hole at the root Need to fill hole with another value Since heap is smaller by one node, we need to empty the last slot Steps: 1. Move last item to top; decrease size by 1 2. Percolate down the top item to its correct position in the heap 2 4 3 7 5 8 9 11 9 6 10 2 4 3 7 5 8 9 11 9 6 10 15 DeleteMin: Percolate Down 10 3 3 4 3 4 10 4 8 7 5 8 9 7 5 8 9 7 5 10 9 11 9 6 11 9 6 11 9 6 Keep comparing with children A[2i] and A[2i + 1] Replace with smaller child and go down one level Done if both children are item or reached a leaf node What is the run time? 16

DeleteMin: Run Time Analysis Run time is O(depth of tree) What is the depth of a complete binary tree of N nodes? 17 DeleteMin: Run Time Analysis Run time is O(depth of heap) A heap is a complete binary tree What is the depth of a complete binary tree of N nodes? At depth d, you can have: N = 2 d (one leaf at depth d) to 2 d+1-1 nodes (all leaves at depth d) So, depth d for a heap is: log N d log(n+1)-1 or Θ(log N) Therefore, run time of DeleteMin is O(log N) 18

Next Class: Up close and personal with Binomial Heaps To Do: Read Chapter 6 Homework # 2 (due this Friday) 19