// JUST A GRAPHICAL REPRESENTATION!!!

Similar documents
A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps. CSE 373 Data Structures

Cpt S 223. School of EECS, WSU

6 March Array Implementation of Binary Trees

Binary Heap Algorithms

Converting a Number from Decimal to Binary

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Algorithms and Data Structures Written Exam Proposed SOLUTION

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1/1 7/4 2/2 12/7 10/30 12/25

Analysis of Algorithms I: Binary Search Trees

Ordered Lists and Binary Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Chapter 14 The Binary Search Tree

Data Structures Fibonacci Heaps, Amortized Analysis

Algorithms and Data Structures

From Last Time: Remove (Delete) Operation

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structures and Algorithms

Why Use Binary Trees?

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Algorithms and Data Structures

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Trees and Huffman Encoding Binary Search Trees

CS711008Z Algorithm Design and Analysis

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Big Data and Scripting. Part 4: Memory Hierarchies

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

PES Institute of Technology-BSC QUESTION BANK

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

CompSci-61B, Data Structures Final Exam

Java Software Structures

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Analysis of a Search Algorithm

DATA STRUCTURES USING C

Data Structure [Question Bank]

Lecture Notes on Binary Search Trees

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

The ADT Binary Search Tree

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Algorithms Chapter 12 Binary Search Trees

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Binary Search Trees (BST)

International Journal of Software and Web Sciences (IJSWS)

Chapter 13: Query Processing. Basic Steps in Query Processing

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Symbol Tables. Introduction

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures and Data Manipulation

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Sample Questions Csci 1112 A. Bellaachia

Analysis of Binary Search algorithm and Selection Sort algorithm

Lecture Notes on Binary Search Trees

Full and Complete Binary Trees

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Exam study sheet for CS2711. List of topics

TREE BASIC TERMINOLOGIES

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Data Structures. Level 6 C Module Descriptor

APP INVENTOR. Test Review

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Algorithms. Margaret M. Fleck. 18 October 2010

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Module 2 Stacks and Queues: Abstract Data Types

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Rotation Operation for Binary Search Trees Idea:

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Node-Based Structures Linked Lists: Implementation

Binary search algorithm

Persistent Binary Search Trees

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Data Structures and Algorithms(5)

Unit Storage Structures 1. Storage Structures. Unit 4.3

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

6. Standard Algorithms

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

DATABASE DESIGN - 1DL400

Transcription:

HEAPS Heaps in Theory uses a graphical tree to represent un unsorted array the tree o is a RBT (Regular Binary Tree) so only 2 children o is completed from the top down, left to right (called complete) o each node in the tree represented in the corresponding array cannot have duplicates items are added to the array in order (or make a COMPLETE tree) order of inputs does have an effect on the overall order of the heap Tree Representation [1] [2] [3] Array Representation [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] -1 29 85 8 93 23 88 73 44 36 [4] [8] [9] [5] [6] [7] [0] [10] // JUST A GRAPHICAL REPRESENTATION!!! // Draw the value in the elements of the tree from the array representation Was does complete mean? 1

Minimum Binary Heap same constructions as a heap, but the minimum value of the entire tree is stored at the root the further down we go in the min heap, the value increases o parent will ALWAYS be less than or equal in value than the kids o this is called partial ordering The Min Heap Structure Notice 4 is the smallest value so far in this heap Anything below the parent (no matter where) is >= than the parent Notice the max value will be SOMEWHERE near the bottom 2

Initial class setup BinaryHeap code given uses an array o default size is 10 o calls buildheap() just to do that MinBH Construct(or) /** * Construct the binary heap given an array of items. */ public BinaryHeap( AnyType [ ] items ) { currentsize = items.length; array = (AnyType[]) new Comparable[ ( currentsize + 2 ) * 11 / 10 ]; } int i = 1; for( AnyType item : items ) { array[ i++ ] = item; } buildheap( ); 3

Determining the relationships using code/array Determining who is parent/child of a certain node is easy!! o using array notation and structure!! Tree Representation [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [0] // JUST A GRAPHICAL REPRESENTATION!!! Remember this is using an ARRAY representation!!! To Find Formula Example Parent index floor((index)/2) [6]/2 = 3 6 s Parent is 3 Left Child index 2(index) 2*[3] = 6 3 s Left Child is 6 Right Child index 2(index) + 1 2*[3] + 1 = 7 3 s Right Child is 7 9 s Parent 2 s Left Child 4 s Parent 4

Building and Inserting into a Min Heap notice I do have to specify Minimum Binary Heap algorithm o place new node at END of array next available complete spot in BT o at end, could be in wrong order (parent is larger!) continuously swap with parent going up the tree until parent < new node this is called sift up or percolate up o notice that lighter values do bubble up, (maybe not to the root), but are in a higher position Inserting into an establish Min Heap Where will the next value be first placed, no matter the value?? 5

Inserting a 6 Start checking position!! (Check immediate Parent 17) Swap since Parent is > new node!! 6

Start checking position!! (Check immediate Parent 17) I will try this one: (answer on next page) 64, 12, 35, 28, 74, 24, 59 But 5 < 6, so we re good!! 7

Answer to in-class example Try these on your own, insert in the order given: 1. 56, 43, 12, 67, 92, 4, 87, 53, 44, 93 2. 61, 23, 57, 12, 68, 24, 14, 96, 75, 63 3. If I added 4 to #2, how many swaps would take place? Answers b : 8

Insert the function notice it checks the size of the array first o adds more if not enough temporarily spaces our value in [0] < 0 is not the value, but if there is a parent that is greater, then keep swapping Array version of Insert for MinBH /** * Insert into the priority queue, maintaining heap order. * Duplicates are allowed. * @param x the item to insert. */ public void insert( AnyType x ) { // check if size of array is enough to hold new node if( currentsize == array.length - 1 ) { enlargearray( array.length * 2 + 1 ); } // Percolate up int hole = ++currentsize; for( array[ 0 ] = x; x.compareto( array[ hole / 2 ] ) < 0; hole /= 2 ) { array[ hole ] = array[ hole / 2 ]; } } // now put our new value into the right place array[ hole ] = x; Why is it /= 2? 9

Finding the minimum in a MinBH super easy! minimum value will ALWAYS be the root o if everything percolated correctly o [1] not [0]!! Min is always at the top of a MinBH findmin the function /** * Find the smallest item in the priority queue. * @return the smallest item, or throw an UnderflowException if empty. */ public AnyType findmin( ) { if( isempty( ) ) { throw new UnderflowException( ); } return array[ 1 ]; } 10

Deleting in a MinBH deletion is ONLY authorized for the MINIMUM value o not any other value we are not deleting the node, just replace the data inside now replaced with the NEXT lowest value o which SHOULD be close to the top of the tree tree must maintain it s shape but we will delete the LAST complete node in the tree since o since now it will be empty Deleting a node, and re-heaping Deleting Min value 11

Replacing root with LAST value in tree Percolating Down - Comparison Comparing 91 (root) with whatever child is smallest 12

Percolating Down Match Percolating Down Swap 13

Percolating Down Comparison Percolating Down Match 14

Percolating Down Swap Done since no more immediate nodes to compare to. Delete the NEXT node using the result above. After you re done, click the link below for your answer: http://userpages.umbc.edu/~slupoli/notes/datastructures/videos/heaps/deleting%20fr om%20a%20heap%20-%20exercise.html 15

Delete the function(s) deletemin() and percolatedown() o deletemin is the bootstrap to get things started o percolatedown is iterative in comparing and swapping also called heapify deletemin() function /** * Remove the smallest item from the priority queue. * @return the smallest item, or throw an UnderflowException if empty. */ public AnyType deletemin( ) { if( isempty( ) ) { throw new UnderflowException( ); } AnyType minitem = findmin( ); array[ 1 ] = array[ currentsize- - ]; percolatedown( 1 ); } return minitem; percolatedown() function /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolatedown( int hole ) { int child; AnyType tmp = array[ hole ]; for( ; hole * 2 <= currentsize; hole = child ) { child = hole * 2; if( child!= currentsize && array[ child + 1 ].compareto( array[ child ] ) < 0 ) child++; if( array[ child ].compareto( tmp ) < 0 ) { array[ hole ] = array[ child ]; } else { break; } } array[ hole ] = tmp; 16

} Performance construction O( n ) o even if data is out of order, we place in heap with partial ordering o still stored in a simple array!! findmin O( 1 ) insert O( log n ) deletemin O( log n ) Heap Construction the function lays all items into array first, no matter order in construction o done in constructor then builds the heap (sorts, partially) in buildheap o notice that buildheap uses percolatedown starting at middle of the array o this is enough to have the real minimum value rise to the top of the heap neither of these functions are recursive BuildHeap function /** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */ private void buildheap( ) { for( int i = currentsize / 2; i > 0; i- - ) percolatedown( i ); } 17

Sorting a Heap MinBH given a list of n values, we can build and sort in O(n log n) o insert random values = O(n) o heapify = O(n) o repeatedly delete min and re-heapify O(log n) * n times heapify o re-ordering the values so Parent is <= it s kids in a MinBH delete o retrieves the CURRENT minimum node in a MinBH value is saved in another array o automatically calls percolatedown() this looped OVER and OVER will return a sorted list of items this means we will need another array of the same size, just to hold the cast offs o UNLESS, we store the values casted back in the deleted code s position o but this will have everything backwards!! Using a MinBH to sort 18

Take 26 (current Min), now heapify Take 31 (current Min), now heapify Perform the next two deletions on the heap above. Make sure to draw the tree AND the array 19

Sorting a Heap MaxBH here we avoid the backward issue now the parent is >= it s kids o so HIGHEST value is at the top of the heap Max Heap Example Deleting 97 (current Max), now heapify Perform the next two deletions on the heap above In it s entirety 20

http://nova.umuc.edu/~jarc/idsv/lesson3.html MinBH (using arrays) shortcomings sorting, wrong order merge o merging two arrays, no real shortcut o so n 1 + n 2 Leftist Min Heaps uses a BT!! merging heaps is much easier and faster o may use already established links to merge with a new node why so much faster o because we are using Binary Trees!! values STILL obey a heap order (partially ordered) uses a null path length to maintain the structure (covered later) o the null path of and node s left child is >= null path of the right child at every node, the shortest path to a non-full node is along the rightmost path this overall ADT supports o findmin = O(1) o deletemin = O(log n) o insert = O(log n) o construct = O(n) o merge = O(lg n) Example of a Leftist Heap 21

Null Path Length (npl) length of shortest path from current node (X) to a node without 2 children o value is store IN the node itself leafs = 0 nodes with only 1 child = 0 Determining the npl for a node Determine the npls for the trees below. Are the left-ist? 22

The Leftist Node the node will have many data members this time o links (left and right o element (data) o npl by default, the LeftistHeap sets and empty one as the root The leftist Node Class and Code private LeftistNode<AnyType> root; // root private static class LeftistNode<AnyType> { // Constructors LeftistNode( AnyType theelement ) { this( theelement, null, null ); } LeftistNode( AnyType theelement, LeftistNode<AnyType> lt, LeftistNode<AnyType> rt ) { element = theelement; left = lt; right = rt; npl = 0; } } AnyType element; // The data in the node LeftistNode<AnyType> left; // Left child LeftistNode<AnyType> right; // Right child int npl; // null path length 23

Building a Left-ist Heap value of node STILL matters, lowest value will be root, so still a min Heap data entered is random uses CURRENT npl of a node to determine where the next node will be placed algorithm o add new node to right-side of tree, in order o if new node is to be inserted as a parent (parent > children), make new node parent link children to it link grandparent down to new node o if leaf, attach to right of parent o if no left sibling, push to left (hence left-ist) why?? (answer in a second) o else left node is present, leave at right child o update all ancestors npls o check each time that all nodes left npl < right npls if not, swap children or node where this condition exists this is really using heaps and links!! 21, 14, 17, 10, 3, 23, 26, 8 Building a leftist Heap inserting 14 inserting 17 24

inserting 10 inserting 3 inserting 23 inserting 26 25

inserting 8 Why can we NOT have this?? Why does 26 HAVE to be moved left? we can have it where a node s left npl is greater than it s right npl o simply, we swap children Swapping children to save a leftist tree Just inserted 52 into the leftist heap 26

What s the issue now? Try creating these leftist heaps n your own: 75, 91, 97, 9, 39, 87, 34, 8, 86, 58 24, 80, 98, 30, 77, 35, 65, 2, 48, 92, 18, 37, 67, 96 71, 4, 13, 73, 52, 20, 50, 63, 85, 23, 1, 44, 32, 53, 14, 17, 82, 76, 27, 83, 11, 81, 90, 62 Answers b : 27

Inserting the function in the code, adding a single node is treated at merging a heap (just one node) with an established heap s root o and work from that root as we just went over we will go over merging whole heaps momentarily The Insert function /** * Insert into the priority queue, maintaining heap order. * @param x the item to insert. */ public void insert( AnyType x ) { root = merge( new LeftistNode<>( x ), root ); } 28

Merging Left-ist Heaps the heaps we are about to merge must be left-ist at end we will get a heap that is o a min-heap o left-ist algorithm o Start at the (sub) root, and finalize the node AND LEFT with the smallest value o REPEADLY, until no lists left unmerged. Start at the rightmost root of the sub-tree, and finalize the node AND LEFT with the next smallest value in leftist lists. Add to RIGHT of finalized tree. o Verify that it is a Min Heap!! (Parent < Children) o Verify a leftist heap! (left npl <= right npl) if not, swap troubled node with sibling I will try: Initial Left-ist Heaps Start at the root, and finalize the node AND LEFT with the smallest value 29

finalized option 1 option 2 30

Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree finalized option1 option2 Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree option 2 option 1 finalized Start at the root of the sub-tree, and finalize the node AND LEFT with the next smallest value. Add to RIGHT of finalized tree Verify that it is a Min Heap!! (Parent < Children) Yup 31

Verify a leftist heap! (left npl <= right npl) Switch problem node with sibling. (Start from root, work way to bottom). All links stay in same direction down. 32

Try these: #1 #2 DO THIS ONE AND STOP!!! Will go over together. a lot of work on this one #3 I know!! Heap is a MAX heap!! Try it anyway! (max first!) 33

Merging the function notice it is recursive! merge() o version 1 copy rhs to root o version 2 - is the function to set up the order between left and right heaps merge1() is the function to actually do the linking and swapping if left npl > right npl o notice npl is a private variable Merging Heaps /** * Merge rhs into the priority queue. * rhs becomes empty. rhs must be different from this. * @param rhs the other leftist heap. */ public void merge( LeftistHeap<AnyType> rhs ) { if( this == rhs ) // Avoid aliasing problems return; } root = merge( root, rhs.root ); rhs.root = null; /** * Internal method to merge two roots. * Deals with deviant cases and calls recursive merge1. */ private LeftistNode<AnyType> merge( LeftistNode<AnyType> h1, LeftistNode<AnyType> h2 ) { if( h1 == null ) return h2; if( h2 == null ) return h1; if( h1.element.compareto( h2.element ) < 0 ) return merge1( h1, h2 ); else return merge1( h2, h1 ); } /** 34

* Internal method to merge two roots. * Assumes trees are not empty, and h1's root contains smallest item. */ private LeftistNode<AnyType> merge1( LeftistNode<AnyType> h1, LeftistNode<AnyType> h2 ) { if( h1.left == null ) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge( h1.right, h2 ); if( h1.left.npl < h1.right.npl ) swapchildren( h1 ); h1.npl = h1.right.npl + 1; } return h1; } /** * Swaps t's two children. */ private static <AnyType> void swapchildren( LeftistNode<AnyType> t ) { LeftistNode<AnyType> tmp = t.left; t.left = t.right; t.right = tmp; } 35

So why did we do this? fast! o merge with two trees of size n O(log n), we are not creating a totally new tree!! some was used as the LEFT side! o inserting into a left-ist heap O(log n) same as before with a regular heap o deletemin with heap size n O(log n) remove and return root (minimum value) merge left and right subtrees real life application o priority queue homogenous collection of comparable items smaller value means higher priority 36

Answers: Inserting into a Heap Exercise #1 Inserting into a Heap Exercise #2 37

Leftist Heap Creation Exercise #1 Leftist Heap Creation Exercise #2 Leftist Heap Creation Exercise #3 38

Merging Left-ist Heaps #1 39

40

Merging Left-ist Heaps #2 41

Merging Left-ist Heaps #3 42

Sources: In General http://courses.cs.washington.edu/courses/cse332/13wi/lectures/cse332-13wi-lec04- BinMinHeaps-6up.pdf Maximum HeapSort http://www.cs.usfca.edu/~galles/visualization/heapsort.html Show and Tell Heap building http://www.cs.usfca.edu/~galles/visualization/heap.html Random Heapsort http://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingii/heapsort/heapsort.html http://nova.umuc.edu/~jarc/idsv/lesson3.html Building a Leftist Heap http://www.cs.usfca.edu/~galles/visualization/leftistheap.html 43