MIT1_204S10_lec08

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

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

View Full Document Right Arrow Icon
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 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Heap: constructors public class Heap { // Max heap: largest element at top private Comparable[] data; private int size; // Actual number of elements in heap private int capacity; private static final int DEFAULT_CAPACITY= 30; public Heap(int capacity) { data = new Comparable[capacity]; this.capacity= capacity; } public 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.
Background image of page 4
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)) { data[i] = data[(i-1)/2]; // Move parent item down
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2011 for the course ESD 1.204 taught by Professor Georgekocur during the Spring '10 term at MIT.

Page1 / 11

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

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online