B-Tree Insertions, Intro to Heaps

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Sorting With Priority Queue In-place Extra O(N) space
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
CSE373: Data Structures & Algorithms
Trees Chapter 15.
AA Trees.
Data Structures and Algorithms for Information Processing
Heaps (8.3) CSE 2011 Winter May 2018.
UNIT III TREES.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
October 30th – Priority QUeues
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
November 1st – Exam Review
Heaps.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
(edited by Nadia Al-Ghreimil)
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Data Structures and Algorithms
Part-D1 Priority Queues
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
A Kind of Binary Tree Usually Stored in an Array
Heaps 11/27/ :05 PM Heaps Heaps.
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Hassan Khosravi / Geoffrey Tien
Data Structures and Algorithms
(edited by Nadia Al-Ghreimil)
CSE 373: Data Structures and Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Lecture 9: Self Balancing Trees
CSE 12 – Basic Data Structures
CSE 373 Priority queue implementation; Intro to heaps
Lecture 13: Computer Memory
Heaps By JJ Shepherd.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
B-Trees.
Lecture 10: BST and AVL Trees
Lecture 9: Intro to Trees
Priority Queues Binary Heaps
Lecture 16: Midterm recap + Heaps ii
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

B-Tree Insertions, Intro to Heaps CSE 373 SP 18 - Kasey Champion B-Tree Insertions, Intro to Heaps Data Structures and Algorithms

Warm Up What operations would occur in what order if a call of get(24) was called on this b-tree? What is the M for this tree? What is the L? If Binary Search is used to find which child to follow from an internal node, what is the runtime for this get operation? 12 6 20 27 34 1 2 3 6 4 8 5 9 10 7 12 8 14 9 16 10 17 11 20 12 22 13 24 14 27 15 28 16 32 17 34 18 38 19 39 20 41 21 CSE 373 SP 18 - Kasey Champion

Administrivia 1. Midterm grades will be published by Friday 2. HW #4 is due Friday 3. 1 partner must fill out partner form by Friday 4. HW Grade Review Requests coming next week CSE 373 SP 18 - Kasey Champion

Review: B-Trees Has 3 invariants that define it 1. B-trees must have two different types of nodes: internal nodes and leaf nodes An internal node contains M pointers to children and M – 1 sorted keys. M must be greater than 2 Leaf Node contains L key-value pairs, sorted by key. 2. B-trees order invariant For any given key k, all subtrees to the left may only contain keys that satisfy x < k All subtrees to the right may only contain keys x that satisfy k >= x 3. B-trees structure invariant If n<= L, the root is a leaf If n >= L, root node must be an internal node containing 2 to M children All nodes must be at least half-full CSE 373 SP 18 - Kasey Champion

Put() for B-Trees Build a new b-tree where M = 3 and L = 3. Insert (3,1), (18,2), (14,3), (30,4) where (k,v) When n <= L b-tree root is a leaf node No space for (30,4) ->split the node Create two new leafs that each hold ½ the values and create a new internal node 3 1 18 2 wrong -> 14 3 18 <- use smallest value in larger subset as sign post 2. B-trees order invariant For any given key k, all subtrees to the left may only contain keys that satisfy x < k All subtrees to the right may only contain keys x that satisfy k >= x 3 1 14 18 2 30 4 CSE 373 SP 18 - Kasey Champion

You try! Try inserting (32, 5) and (36, 6) into the following tree 18 14 18 2 30 4 32 5 36 6 32 5 CSE 373 SP 18 - Kasey Champion

Splitting internal nodes Try inserting (15, 7) and (16, 8) into our existing tree 18 32 Make a new internal node! 3 1 14 15 7 16 8 18 2 30 4 32 5 36 6 15 7 18 Make a new internal node! 15 32 3 1 14 15 7 16 8 18 2 30 4 32 5 36 6 CSE 373 SP 18 - Kasey Champion

B-tree Run Time Time to find correct leaf Time to insert into leaf Time to split leaf Time to split leaf’s parent internal node Number of internal nodes we might have to split All up worst case runtime: Height = logm(n)log2(m) = tree traversal time Θ(L) Θ(L) Θ(M) Θ(logm(n)) Θ(L + Mlogm(n)) CSE 373 SP 18 - Kasey Champion

New Topic: Heaps CSE 373 SP 18 - Kasey Champion

Priority Queue ADT Imagine you have a collection of data from which you will always ask for the extreme value Min Priority Queue ADT removeMin() – returns the element with the smallest priority, removes it from the collection state behavior Set of comparable values - Ordered based on “priority” peekMin() – find, but do not remove the element with the smallest priority insert(value) – add a new element to the collection Max Priority Queue ADT removeMax() – returns the element with the largest priority, removes it from the collection state behavior Set of comparable values - Ordered based on “priority” peekMax() – find, but do not remove the element with the largest priority insert(value) – add a new element to the collection CSE 373 SP 18 - Kasey Champion

Implementing Priority Queue Idea Description removeMin() runtime peekMin() runtime insert() runtime Unsorted ArrayList Linear collection of values, stored in an Array, in order of insertion O(n) O(1) Unsorted LinkedList Linear collection of values, stored in Nodes, in order of insertion Sorted ArrayList Linear collection of values, stored in an Array, priority order maintained as items are added Sorted Linked List Linear collection of values, stored in Nodes, priority order maintained as items are added Binary Search Tree Hierarchical collection of values, stored in Nodes, priority order maintained as items are added AVL tree Balanced hierarchical collection of values, stored in Nodes, priority order maintained as items are added O(logn) CSE 373 SP 18 - Kasey Champion

Let’s start with an AVL tree AVLPriorityQueue<E> removeMin() – traverse through tree all the way to the left, remove node, rebalance if necessary state behavior overallRoot peekMin() – traverse through tree all the way to the left insert() – traverse through tree, insert node in open space, rebalance as necessary What is the worst case for peekMin()? What is the best case for peekMin()? Can we do something to guarantee best case for these two operations? O(logn) O(1) CSE 373 SP 18 - Kasey Champion

Binary Heap A type of tree with new set of invariants 1. Binary Tree: every node has at most 2 children 2. Heap: every node is smaller than its child 3. Structure: Each level is “complete” meaning it has no “gaps” - Heaps are filled up left to right 1 8 9 10 2 4 5 3 6 7 1 2 3 22 4 5 36 47 8 9 10 CSE 373 SP 18 - Kasey Champion

Self Check - Are these valid heaps? Binary Heap Invariants: Binary Tree Heap Complete Self Check - Are these valid heaps? INVALID INVALID VALID 1 4 5 9 8 6 7 4 3 2 5 7 3 7 8 6 9 11 10 CSE 373 SP 18 - Kasey Champion

Implementing peekMin() 2 4 7 5 8 10 9 11 13 CSE 373 SP 18 - Kasey Champion

Implementing removeMin() Removing overallRoot creates a gap Replacing with one of its children causes lots of gaps What node can we replace with overallRoot that wont cause any gaps? 2 13 4 7 4 7 5 8 10 9 5 8 10 9 11 13 11 Structure maintained, heap broken CSE 373 SP 18 - Kasey Champion

Fixing Heap – percolate down Recursively swap parent with smallest child 13 4 4 13 5 7 5 13 11 8 10 9 11 13 percolateDoen(node) { while (node.data is bigger than its children) { swap data with smaller child } CSE 373 SP 18 - Kasey Champion

Self Check – removeMin() on this tree 5 18 9 10 9 18 11 17 14 11 18 13 20 19 16 15 24 22 18 CSE 373 SP 18 - Kasey Champion

Implementing insert() Insert a node to ensure no gaps Fix heap invariant percolate UP 2 4 7 3 10 5 8 4 9 3 11 13 3 7 CSE 373 SP 18 - Kasey Champion