0% found this document useful (0 votes)
788 views2 pages

CSE 247 Exam 1 Cheat Sheet

The document discusses priority queues and heaps. It provides pseudocode for heap operations including decreasing a value, extracting the minimum value, and heapifying a tree after removing the minimum. The decrease operation bubbles a value down the heap if it is less than its parent. Extracting the minimum replaces the root with the last element and sinks it down, shrinking the heap size. Heapifying a subtree ensures the min-heap property is maintained by comparing a node to its children and swapping if needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
788 views2 pages

CSE 247 Exam 1 Cheat Sheet

The document discusses priority queues and heaps. It provides pseudocode for heap operations including decreasing a value, extracting the minimum value, and heapifying a tree after removing the minimum. The decrease operation bubbles a value down the heap if it is less than its parent. Extracting the minimum replaces the root with the last element and sinks it down, shrinking the heap size. Heapifying a subtree ensures the min-heap property is maintained by comparing a node to its children and swapping if needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

LISTS, QUEUES & STACKS

You can "shift" sums over by a constant


amount, as long as you shift the variable inside
the summation in the opposite direction.

PRIORITY QUEUES & TREES

ASYMPTOTIC NOTATION
// There is effectively a hole at the root, at
location 1 now.
// Fix up the heap as described in lecture.
// Be sure to store null in an array slot if it is no
longer
// part of the active heap
//
// FIXME
//

// replaces the value located at 1 with the


greatest current value in the array
// this guarantees that, as heapify proceeds
recursively on this value, pushing
// it all the way to the end of the tree and
resorting every value along the way
array[1] = array[size];
array[1].loc = 1;
array[size] = null;
size = size -1;
heapify(1);
return ans;
}
private void heapify(int where) {
if(where*2>size() || array[where*2]==null){
return;
}

public MinHeap(int maxSize) { if(array[where*2].getValue().compareTo(array[where].getValue())>


this.array = new Decreaser[maxSize+1]; =0
this.size = 0; && array[where*2+1]!=null
} &&
void decrease(int loc) { array[where*2+1].getValue().compareTo(array[where].getValue())
// >=0){
// As described in lecture return;
// }
Decreaser<T> temp;
// if the value being decreased is in the first if(array[where*2+1]==null){
position of the heap array, then it doesn't need to be moved temp = array[2*where];
if(loc<=1){ array[2*where] = array[where];
return; array[where] = temp;
} array[2*where].loc = 2*where;
array[where].loc = where;
// if the value at loc is less than the value at loc's where = 2*where;
parent and loc is not already the root }
else
if(array[where*2].getValue().compareTo(array[where*2+1].getValu
e()) < 0){
while(array[loc].getValue().compareTo(array[(int)loc/2].getValue() temp = array[2*where];
) < 0){ array[2*where] = array[where];
Decreaser <T> temp = array[loc]; array[where] = temp;
// updating values by switching the array[2*where].loc = 2*where;
parent and child array[where].loc = where;
array[loc] = array[(int)loc/2]; where = 2*where;
array[(int)loc/2] = temp; }
// updating locations else
array[loc].loc = loc; if(array[where*2].getValue().compareTo(array[where*2+1].getValu
array[(int)loc/2].loc = (int)loc/2; e()) > 0){
loc = (int)loc/2; temp = array[2*where+1];
// if the value array[2*where+1] = array[where];
if(loc<=1){ array[where] = temp;
break; array[2*where+1].loc = 2*where+1;
} array[where].loc = where;
} where = 2*where+1;
return; }
} heapify(where);
public T extractMin() { }
T ans = array[1].getValue();
//

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy