Heaps Flashcards

1
Q

What is a heap?

A
  • A hash table is used when the ordering of elements is not important and we want quick insert, delete and search operations.
  • A heap is a tree-like data structure that can be implemented as an array
  • Each node of the tree corresponds to an element of the array and every node must satisfy the heap properties
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Binary Heap

A

The simplest kind of heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A binary heap can be seen as a binary tree with two additional constraints:

A

o The shape property the tree is either a full tree or if the last level is not full, the nodes are filled from left to right, i.e. the last level is left-filled
o The heap property each node is greater than or equal (or smaller than or equal) to each of its children

Like AVL trees, an operation on a heap can destroy one of the properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Heap property

A

§ A binary heap is a complete binary tree implemented as an array

§ Heaps satisfy the heap property: let v and u be nodes of a heap, such that u is a child of v,
o if v.data >= u.data we have a MAX HEAP

o if v.data <= u.data we have a MIN HEAP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

MIN Heap

A

Min heap is a complete binary tree where the key of each node is less than the keys of its children

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

MAX Heap

A

Max heap is a complete binary tree where the key of each node is greater than the keys of its children

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Heap implementation

A

Although a heap can be built using a dynamic tree structure, it is most often implemented using an array

This implementation is possible because the heap is, by definition, complete
–>The relationship between a node and its children is fixed and can be calculated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you store the heap in an array?

A

Level-order traversal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Heap array implementation (parent)

A

PARENT(i)

return ⎣i/2⎦

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Heap array implementation (left child)

A

-The left of a node located at index i is located at 2i

LEFT(i)
return 2i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Heap array implementation (right child)

A

-The right of a node located at index i is located at 2i + 1

RIGHT(i)
return 2i+1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A.length vs A.heap.size

A

A.length = number of elements in the array

A.heap-size = number of elements in the heap stored within A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Basic operations

A

-The basic operations performed on a heap are:
◦ BUILD-MAX-HEAP/ BUILD-MIN-HEAP
—Create a max-heap/min-heap from an unordered array
◦ INSERT A NODE
—New nodes are always inserted at the bottom level (left to right)

◦ DELETE A NODE
—Nodes are removed from the bottom level (right to left)

◦ HEAPSORT
—Sort an array in place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For insert and delete we need:

A

To implement the insert and delete operations, we need two basic operations to maintain/restore the heap property

o HEAPIFY

  • –HEAPIFY UP or “percolate up”
  • –HEAPIFY DOWN or “percolate down”

o Heapify is the key to maintaining the heap property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Building a max heap O(nlogn)

-Uses Heapify-Down O(log n)

A

§We can build a max-heap in a bottom-up manner by using HEAPIFY-DOWN

◦ Walk backwards through the array from n/2 to 1, calling HEAPIFY-DOWN() on each node
◦ Order of processing guarantees that the children of node i are heaps when i is processed

BUILD-MAX-HEAP(A)
heap-size(A) ← length(A)
for i ← ⎣length(A)/2⎦ downto 1
HEAPIFY-DOWN(A, i)

BUILD-MIN-HEAP(A)
heap-size(A) ← length(A)
for i ← ⎣length(A)/2⎦downto 1 do
HEAPIFY-DOWN**(A, i)

** Modify Heapify-Down to work on a min-heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Other Heap Operations are

A
  • HEAPIFY-DOWN (logn)
  • HEAPIFY-UP
  • HEAPSORT (nlogn)
  • EXTRACT MAX/EXTRACT MIN
  • GET MAX/ GET MIN
17
Q

What are heaps useful for?

A

§ Sorting
◦ Heapsort

§To implement priority queues

18
Q

Heapsort

A

Goal: Sort an array using heap implementation

§ Idea:
◦ Build a max-heap from the array
◦ Swap the root (the maximum element) with the last element in the array
◦ “Discard” this last node by decreasing the heap size
◦ Call HEAPIFY-DOWN on the new root
◦ Repeat this process until only one node remains

19
Q

Heapsort Pseudocode O(nlogn)

  • In place? Yes
  • Stable? No
A
HEAPSORT(A)
  BUILD-MAX-HEAP(A)
  for i ← length(A) downto 2
     swap A[1] ↔ A[i]
     heap-size(A) ← heap-size(A)    – 1
     HEAPIFY-DOWN(A,1)