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

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

CSE205 Concepts of Computer Science and Data Structure

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

View Full Document
Outline Heaps Heap Implementation Heap Sort
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

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

View Full Document
An Example Maxheap
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

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

View Full Document
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
Adding an Element to a Heap

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

View Full Document
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
Removing the Largest Element from a Heap

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

View Full Document
Outline Heaps Heap Implementation Heap Sort
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

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online