heaps - Heaps, Heapsort, Priority Queues Heaps: A data...

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

View Full Document Right Arrow Icon
Heaps, Heapsort, Priority Queues
Background image of page 1

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

View Full DocumentRight Arrow Icon
Heaps: A data structure and associated algorithms, NOT GARBAGE COLLECTION A heap data structure is an array of objects than can be viewed as a complete binary tree such that: 1. Each tree node corresponds to elements of the array 2. The tree is complete except possibly the lowest level, filled from left to right The heap property is defined as an ordering relation R between each node and its descendants. For example, R could be smaller than or bigger than. In the examples that follow, we will use the bigger than relation. Example: Given array [22 13 10 8 7 6 2 4 3 5] Note that the elements are not sorted, only max element at root of tree. The height of a node in the tree is the number of edges on the longest simple downward path from the node to a leaf; e.g. height of node 6 is 0, height of node 4 is 1, height of node 1 is 3. The height of the tree is the height from the root. As in any complete binary tree of size n, this is lg n. There are 2 h nodes at level h and 2 h+1 - 1 total nodes in a complete binary tree. We can represent a heap as an array A that has two attributes: 1 Length(A) – Size of the array 2 HeapSize(A) - Size of the heap
Background image of page 2
The property Length(A) HeapSize(A) must be maintained. The heap property is stated as A[parent(i)] A[i] The root of the tree is A[1]. Formula to compute parents, children in an array: Parent(i) = A[ i /2 ] Left Child(I) = A[2i] Right Child(I) = A[2i+1] Where might we want to use heaps? Consider the Priority Queue problem: Given a sequence of objects with varying degrees of priority, and we want to deal with the highest-priority item first. Managing air traffic control - want to do most important tasks first.
Background image of page 3

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

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

Page1 / 13

heaps - Heaps, Heapsort, Priority Queues Heaps: A data...

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

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