{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

MIT1_204S10_lec08

# MIT1_204S10_lec08 - 1.204 Lecture 8 Data structures heaps...

This preview shows pages 1–6. Sign up to view the full content.

1 1.204 Lecture 8 Data structures: heaps Priority Queues or Heaps Highest priority element at top Top “Partial sort” All enter at bottom, leave at top Applications: 1. Simulations: event list 2. Search, decision trees 3 Mi i i t Bottom 3. Minimum spanning tree 4. Shortest path (label setting) 5. And many others… Complexity: 1. Insertion, deletion: O(lg n) a b c d e

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Min Heap Modeled as Binary Tree c q d f e v t a Min Heap Modeled as Binary Tree c q d f e v t a a e 2
Min Heap Modeled as Binary Tree c q d f e v t a d a a e e Min Heap Modeled as Binary Tree c q d f e v t a d a c a a 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Heap: constructors public class Heap { // Max heap: largest element at top private Comparable[] data; p rivate int size ; // Actual number of elements in hea p private int capacity; private static final int DEFAULT_CAPACITY= 30; public Heap(int capacity) { data = new Comparable[capacity]; this.capacity= capacity; } publi blic Heap() { () { this(DEFAULT_CAPACITY); } public Heap(Comparable[] c) { data= c; heapify(data); capacity= size= data.length; } (Max) Heap insertion 4 (a) (b) (c) (d) (e) (f) (g) 40 40 40 40 40 80 80 90 90 90 90 90 90 80 80 40 40 45 45 35 35 50 50 70 70 80 80 80 80 40 40 40 45 45 45 35 35 35 50 50 35 40 90 35 80 80 35 Figure by MIT OpenCourseWare.
i 1 d dat o e e t te do Heap: insert() public void insert(Comparable item) { if (size == 0) { // Empty heap, first element being added size= 1; data[0]= item; } else { if (size == data.length) grow(); int i = size++; // Increase no of elements while (i > 0 && (data[(i-1)/2].compareTo(item) < 0)) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}