Heap - CSE205 Concepts of Computer Science and Data...

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

View Full Document Right Arrow Icon
CSE205 Concepts of Computer Science and Data Structure
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Heaps Heap Implementation Heap Sort
Background image of page 2
Heaps A heap is a complete binary tree in which each element is greater than or equal to both of its children Two interesting extensions to a binary tree in a heap definition it is a complete tree the constraint on the relationship between elements As with binary search trees, there are many possible heap configurations for a given set of elements Our definition above is really a maxheap
Background image of page 3

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

View Full DocumentRight Arrow Icon
An Example Maxheap
Background image of page 4
Heaps A maxheap keeps the largest value of a set of elements readily available Three primary operations for maxheaps (or the minheaps) add new element to the heap find the maximum/minimum value remove the maximum/minimum value
Background image of page 5

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

View Full DocumentRight Arrow Icon
Adding an Element to a Heap To add an element to the heap, add the element as a leaf, keeping the tree complete Then, move the element up toward the root, exchanging positions with its parent, until the relationship among the elements is appropriate This will guarantee that the resulting tree will conform to the heap criteria
Background image of page 6
Adding an Element to a Heap
Background image of page 7

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

View Full DocumentRight Arrow Icon
Removing the Largest Element from a Heap We know that the largest element in a heap can be found at the root Thus, we remove the root and reconstruct the heap from the two disjoint sub-trees remaining Move the last leaf of the tree to be the new root of the tree Then, move it down the tree as needed (compare to the children) until the relationships among the elements is appropriate
Background image of page 8
Removing the Largest Element from a Heap
Background image of page 9

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

View Full DocumentRight Arrow Icon
Outline Heaps Heap Implementation Heap Sort
Background image of page 10
Heap Implementation Our implementation commits at the interface level whether our collection is a min-heap or max-heap The interface contains methods for adding a new element to the heap obtaining the highest value in the heap and removing the highest value in the heap Because a heap is a binary tree, our implementation can be based on the LinkedBinaryTree class Please see the implementation in your book on page 710-716
Background image of page 11

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

View Full DocumentRight Arrow Icon
Implementation Notes The LinkedMaxHeap class inherits the root reference from the LinkedBinaryTree It also keeps track of the “last node” in the heap – the last leaf in the complete tree The HeapNode class inherits the left and right reference variables from the BTNode class A HeapNode object also keeps a reference to its parent object (node), allowing us to move up the tree path
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

Heap - CSE205 Concepts of Computer Science and Data...

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

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