Heaps: Erin Keith
Heaps: Erin Keith
ERIN KEITH
23_HEAPS 1
Topics
1. Characteristics
2. ADT Operations
3. Array Implementation
4. Heap Sort
23_HEAPS 2
Heaps
Are complete binary trees that
◦ are empty OR
◦ have a root which contains a value >= (or <=) each of its
children
◦ and has heaps as subtrees
Complete
◦ every level, except possibly the last, is completely filled
and all nodes are as far left as possible
23_HEAPS 3
Heaps
◦ Maxheap
◦ the root contains the largest value
◦ Minheap
◦ the root contains the smallest value
23_HEAPS 4
Heap ADT
Operations
• isEmpty(): boolean
• getNumberOfNodes(): int
• getHeight(): int
• add(newEntry: ItemType): boolean
• remove(): boolean
• clear(): boolean
• peekTop(): ItemType
23_HEAPS 5
Heap Interface
#ifndef HEAP_INTERFACE
#define HEAP_INTERFACE
template<class ItemType>
class HeapInterface {
public:
virtual bool isEmpty() const = 0;
virtual int getHeight() const = 0;
virtual int getNumberOfNodes() const = 0;
virtual ItemType peekTop() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove() = 0;
virtual void clear() = 0;
virtual ~HeapInterface() { }
};
#endif
23_HEAPS 6
Array Implementation
Any approach for implementing a binary tree can be used.
23_HEAPS 7
Array Implementation
A complete tree of height h is full to level h-1 and has level
h filled from left to right, so we can use the sequential nature
of indexes to manage node placement.
0 items
Level 1 Qiang 0 Qiang
1 Jose
1 2
2 Mia
Level 2
Jose Mia
3 Elisa
4 Deepak
3 4 5
5 Anton
Level 3Deepak
Elisa Anton
6
23_HEAPS 8
Array Implementation
Simple arithmetic will help us find the child or parent nodes:
◦ Left child, if exists = items[2 * i + 1]
◦ Right child, if exists = items[2 * i + 2]
◦ Parent, if exists = items[(i-1) / 2]
0 items
Level 1 Qiang 0 Qiang
1 Jose
1 2
2 Mia
Level 2
Jose Mia
3 Elisa
4 Deepak
3 4 5
5 Anton
Level 3Deepak
Elisa Anton
6
23_HEAPS 9
Array Implementation
#ifndef ARRAY_MAXHEAP
#define ARRAY_MAXHEAP
#include "HeapInterface.h"
template<class ItemType>
class ArrayMaxHeap: public HeapInterface<ItemType>{
private:
static const int MAX_ITEMS = 100;
ItemType items[MAX_ITEMS];
int itemCount;
int maxItems;
23_HEAPS 10
Array Implementation
public:
ArrayMaxHeap();
~ArrayMaxHeap();
};
#include "ArrayMaxHeap.cpp"
#endif
23_HEAPS 11
Array Implementation
bool add(const ItemType& newData);
◦ To preserve completeness
23_HEAPS 12
Array Implementation
bool add(const ItemType& newData);
◦ To preserve completeness, add the new item to the
“bottom” of the tree (the end of the array)
◦ new items needs to “bubble up” to its correct position
23_HEAPS 13
Array Implementation
bool add(const ItemType& newData);
SET items at itemCount to newData
SET newDataIndex to itemCount
SET inPlace to FALSE
WHILE newDataIndex >= 0 AND inPlace is FALSE
GET parentIndex
IF items at newDataIndex <= items at parentIndex
THEN newData is inPlace
ELSE
swap items, update newDataIndex
ENDIF
23_HEAPS 14
Array Implementation
bool remove(const ItemType& data);
◦ In this example, removing the root results in “disjoint
heaps”
23_HEAPS 15
Array Implementation
bool remove(const ItemType& data);
◦ How have we solved this before…?
23_HEAPS 16
Bag Implementation
After we add items, how do we remove them?
◦ remove item?
◦ shuffle?
◦ swap?
23_HEAPS 17
Bag Implementation
After we add items, how do we remove them?
◦ remove item?
◦ shuffle?
◦ swap?
◦ because order doesn’t matter in a Bag!
23_HEAPS 18
Array Implementation
bool remove(const ItemType& data);
◦ Swap (preserves completeness)
◦ Restore (heapRebuild function)
23_HEAPS 19
Array Implementation
void heapRebuild(int nodeIndex);
IF current item is not a leaf THEN
GET leftChildIndex
GET rightChildIndex
SET largerChildIndex to larger of leftChildIndex or
rightChildIndex
IF current item < items at largerChildIndex THEN
swap items
CALL heapRebuild with largerChildIndex
ENDIF
ENDIF
23_HEAPS 20
Heap Sort
Uses a Heap to sort an array of items
◦ Transforms the array into a heap
◦ Partitions array into heap and sorted regions
23_HEAPS 21
Heap Sort
Uses a Heap to sort an array of items
◦ Transforms the array into a heap
◦ Partitions array into heap and sorted regions
◦ Root is largest item
◦ get it (peekTop, remove)
◦ store in sorted region
◦ rebuild heap region
23_HEAPS 22
Heap Sort
23_HEAPS 23
Heap Sort
23_HEAPS 24
Heap Sort
23_HEAPS 25
Next Class
Dictionaries
Textbook:
• Chapters 18
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-11-dictionaries-and-impleme
ntations.pdf
23_HEAPS 26