0% found this document useful (0 votes)
53 views26 pages

Heaps: Erin Keith

This document discusses heaps and their implementation using arrays. It begins by defining heaps as complete binary trees where the root node has a value greater than or equal to its children. It describes the operations of a heap abstract data type including add, remove, and peek operations. It then discusses implementing a max heap using an array where parent and child nodes can be calculated based on their indexes. The removal operation involves swapping the root value with the last value and rebuilding the heap. Heap sort is also summarized, which uses a heap to iteratively remove the maximum value and build a sorted array.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views26 pages

Heaps: Erin Keith

This document discusses heaps and their implementation using arrays. It begins by defining heaps as complete binary trees where the root node has a value greater than or equal to its children. It describes the operations of a heap abstract data type including add, remove, and peek operations. It then discusses implementing a max heap using an array where parent and child nodes can be calculated based on their indexes. The removal operation involves swapping the root value with the last value and rebuilding the heap. Heap sort is also summarized, which uses a heap to iteratively remove the maximum value and build a sorted array.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Heaps

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.

Because it is a complete binary tree, we can use a simpler


array-based implementation that saves memory (no array
nodes).

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;

int getLeftChildIndex(int nodeIndex) const;


int getRightChildIndex(int nodeIndex) const;
int getParentIndex(int nodeIndex) const;
bool isLeaf(int nodeIndex) const;
void heapRebuild(int nodeIndex);
void heapCreate();

23_HEAPS 10
Array Implementation
public:
ArrayMaxHeap();

bool isEmpty() const;


int getHeight() const;
int getNumberOfNodes() const;
ItemType peekTop() const;
bool add(const ItemType& newData);
bool remove(const ItemType& data);
void clear();

~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?

banana apple egg egg bread

◦ remove item?
◦ shuffle?
◦ swap?

23_HEAPS 17
Bag Implementation
After we add items, how do we remove them?

banana apple egg egg bread

◦ 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

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