heap data structure
Post on 01-Mar-2016
9 Views
Preview:
DESCRIPTION
TRANSCRIPT
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 1/18
SC417: DS & AThe heap data structure
Dhammika Elkaduwe
Department of Computer Engineering
Faculty of Engineering
University of Peradeniya
Dhammika Elkaduwe SC417: DS & A The heap data structure
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 2/18
Heap data structure
Heap in few words:
Something between a priority queue and binary search tree.
Recall the simple implementation of priority queue: inserts are O (N ) and removal was O (1) (or inserts are O (1) and removal was O (N )) Can we do better?
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 3/18
Heap basics: a min-heap
A heap looks like a binary tree (each nodehas two children)
Each node has a value smaller than both itschildren.
Every level of the heap is full, except forpossibly the last level The shape matters: more examples later on
Elements can be removed or accessed fromthe root node
So the element with the minimum value willbe removed first
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 4/18
Min-heap example
Example of 2 possible heaps on numbers from 0 to 8.
Min-heap means the minimum value is always at root So, minimum value will leave the heap
shape make sure that tree is balanced
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 5/18
Min-heap operations: Adding
Enter value 5 to the heap.
Put the new value in the next position at the bottom of theheap (do not worry about the ordering now)
To maintain min-heap “bubble” or “shift” the value up theheap by swapping the value with its parent if required.
Note that adding (or inserting) is O (log (N )). (height of thetree)
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 6/18
Building a min-heap
Building a heap by inserting following numbers: 4, 8, 3, 1, 5, 2
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 7/18
Min-heap operations: Removing
Calling remove function on a heap:
Only the root node can be removed from the heap. But, it has to be replaced with the correct value. Algorithm
for this is follows: Replace the root with the last element Bubble that value down by swapping till you re-establish the
heap properties.
Removing from a heap is O (log (N ))Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 8/18
Heap implementation: using an array
Since the heap dense , we can implement using an array Of cause we need to know how many elements are expected (some language provides methods to re-size arrays (how?))
For the element at heap [i ]: Left child at: heap [2i + 1] Right child at: heap [2i + 2]
Parent at: heap [(i −
1)/2] (integer division)Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 9/18
Heap implementation: in C (preliminaries)
typedef int data_t;
#define MAX 1000
struct heap {
data_t data[MAX];
int size;
};
typedef struct heap * heap_t;
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 10/18
Heap implementation: in C (preliminaries...)
int isFull(heap_t h) { return h -> size == MAX;}
int isEmpty(heap_t h) { return h -> size == 0; }int isRoot(int index) { return index == 0; }
int leftChild(int index) { return 2*index + 1;}
int rightChild(int index){ return 2*index + 2;}
int parent(int index) { return (int)((index - 1) / 2);}
int hasLeftChild(heap_t h, int index) {
return leftChild(index) < h -> size;}
int hasRightChild(heap_t h, int index) {
return rightChild(index) < h -> size;
}
void swap(heap_t h, int i, int j){
data_t tmp = h -> data[i];
h -> data[i] = h -> data[j];
h -> data[j] = tmp;
}Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 11/18
Heap implementation: in C – create/add functions
heap_t create_heap(void)
{
heap_t tmp = malloc(sizeof(struct heap));
if(tmp) tmp -> size = 0;
return tmp;
}
int add(heap_t h, data_t d)
{
if(isFull(h)) return -1;
h -> data[h -> size ++] = d;
bubble_up(h);
return 0;
}
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 12/18
Heap implementation: in C – bubble up
Recall: parent is smaller than children, compare and swap
void bubble_up(heap_t h)
{
int *data = h -> data;
int i = h -> size - 1;
while(!isRoot(i)) {
if(data[parent(i)] < data[i]) break;
swap(h, parent(i), i);
i = parent(i);
}
}
What is the run-time complexity?
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 13/18
Heap implementation: in C – Remove a value
You task is to implement the remove function. Algorithm:
replace the root with the last element bubble (down) till heap property is satisfied.
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 14/18
Heap implementation: in C – Remove a value
Bubble down idea:
If you have two children and the root is big replace it with thesmaller of the two children
If there is only one child (should be left) and root is biggerreplace it with child
void bubble_down(heap_t h)
{
int *data = h -> data;
int i = 0 ;
int last = h -> size - 1;/* implement the rest! */
}
Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 15/18
Heap implementation: in C – bubble down
void bubble_down(heap_t h)
{
int j, *data = h -> data, i = 0, last = h -> size - 1;
while(i < last) {
if(hasLeftChild(h, i) && hasRightChild(h, i) &&
/* has two children */
(data[leftChild(i)] < data[i] || data[rightChild(i)]
< data[i])) {/* and data is bigger than one */
if(data[leftChild(i)] < data[rightChild(i)]) j =
leftChild(i);
else j = rightChild(i);
swap(h, j, i);i = j ;
continue;
} // has two children
.....Dhammika Elkaduwe SC417: DS & A: The heap
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 16/18
Heap implementation: in C – bubble down
..... // from previous slide
if(hasLeftChild(h, i) && data[leftChild(i)] <
data[i]) {
swap(h, leftChild(i), i);
i = leftChild(i);continue;
}
break;
}}
Dhammika Elkaduwe SC417: DS & A: The heap
Th h
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 17/18
The heap sort
We can sort a given data set using a heap: add all elementsto the heap, and then remove. Elements will be removed insorted order.
Each addition/removal takes O (log (N )) and we need N
additions and removals. So total time complexity of heap sortis O (Nlong (N ))
Additional memory O (N ) for constructing the heap
Dhammika Elkaduwe SC417: DS & A: The heap
H i J
7/18/2019 Heap data Structure
http://slidepdf.com/reader/full/heap-data-structure 18/18
Heaps in Java
Home work:
find an API for a heap implmentation in Java Use that to implement heap sort
Dhammika Elkaduwe SC417: DS & A: The heap
top related