Data structures and algorithms lab10

16
DATA STRUCTURES AND ALGORITHMS LAB 10 Bianca Tesila FILS, April 2014

Transcript of Data structures and algorithms lab10

Page 1: Data structures and algorithms lab10

DATA STRUCTURES AND ALGORITHMS

LAB 10

Bianca Tesila

FILS, April 2014

Page 2: Data structures and algorithms lab10

OBJECTIVES

Heap Heap Sort

Page 3: Data structures and algorithms lab10

HEAP: INTRODUCTION

What is a heap - a data structure that: is stored as an array in memory can be represented/viewed as an almost

complete binary tree respects the heap property

Page 4: Data structures and algorithms lab10

HEAP: INTRODUCTION

What is an almost complete binary tree - a binary tree such that: all the leaves are at the bottom level or the

bottom 2 levels all the leaves are in the leftmost possible

positions all the levels are completely filled with nodes

(except for the bottom level)

Page 5: Data structures and algorithms lab10

HEAP: INTRODUCTION

The heap property:

Max heaps For each node (except for the root): A[i] <= A[parent(i)] Conclusion: The root (A[1]) contains the largest element

Min heaps For each node (except the root): A[i] >= A[parent(i)] Conclusion: The root (A[1]) contains the smallest

element

!! Heap Sort uses max heaps

Page 6: Data structures and algorithms lab10

HEAP: DATA STRUCTURE

Store the elements in an array - the advantage of this method over using the usual pointers and nodes is that there is no wasting of space due to storing two pointer fields in each node. 

Start by taking the nodes level by level from the top down, left to right.  Then, store them in an array.

Page 7: Data structures and algorithms lab10

HEAP: DATA STRUCTURE

An array A[0..n] can be represented as a binary tree: A[0] –root of the tree Parent of A[i] = parent(i) = A[(i-1)/2] Left child of A[i] = left(i) = A[2*i+1] Right child of A[i] = right(i) = A[2*(i+ 1)]

Height of the heap: Θ(log n) Number of nodes from the root to the farthest leaf

Page 8: Data structures and algorithms lab10

HEAP: BASIC OPERATIONS

Insert a new element: add it as the last element in the heap however, the heap property may be broken if the added element is larger than its parent,

you need to find its correct position in the heap => filter up

Filter up an element: compare it with its parent if the parent is smaller than it, stop else, swap it with the parent and continue

Page 9: Data structures and algorithms lab10

HEAP: BASIC OPERATIONS

Insert E in this heap

1. Place E in the next available position:

2. Filter up E:

Page 10: Data structures and algorithms lab10

HEAP: BASIC OPERATIONS

Delete ( extract min/max): simply remove A[0] (the root element) however, the heap is broken as it has no root

element need to find a new root move the last element in the heap as the new root now, the heap property is lost (almost certainly)

=> need to filter down

Filter down an element: compare it to its children if it is larger than both children, stop else, swap it with the largest child and continue

Page 11: Data structures and algorithms lab10

HEAP: BASIC OPERATIONS

Remove C from this heap:

Page 12: Data structures and algorithms lab10

HEAP SORT

convert the array you want to sort into a heap

remove the root item (the smallest), readjust the remaining items into a heap, and place the removed item at the end of the heap (array)

remove the new item in the root (the second smallest), readjust the heap, and place the removed item in the next to the last position and so on

Page 13: Data structures and algorithms lab10

HEAP SORT

Example: HeapSort.pdf

Page 14: Data structures and algorithms lab10

HEAP

!! Exercise:Implement the functions for returning the parent and the childs of a given node, using the following signatures:

template <typename T>int Heap<T>::parent(int index){ // TODO } template <typename T>int Heap<T>::leftSubtree(int index){ // TODO } template <typename T>int Heap<T>::rightSubtree(int index){ // TODO }

!! Use heap.h

Page 15: Data structures and algorithms lab10

HEAP SORT

!! Exercise:Implement the heapsort and test it for the following array:

25, 17, 36, 2, 3, 100, 1, 19, 17

!! Use heap.h

Page 16: Data structures and algorithms lab10

HOMEWORK

Finish all the lab assignments.