Binary Heap - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Binary Heap

Description:

Essentially, PQ is an Abstract Data Type where access is restricted to the minimum item only. ... Create a 'hole' in the next available location. Where? Bottom ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 46
Provided by: petrusm6
Category:
Tags: binary | create | heap | property

less

Transcript and Presenter's Notes

Title: Binary Heap


1
Binary Heap
2
Review
  • Complete binary tree the tree is completely
    filled, with possible exception of the bottom
    level, which is filled from left to right.
  • Array Implementation

3
Outline
  • Priority Queue
  • Definition
  • Operations
  • Binary Heap
  • Operations
  • Properties
  • Representation
  • Heap Sort

4
A Priority Queue
  • A special kind of queue that features slightly
    different rules
  • Enqueue operation doesnt always add new items to
    the rear of the queue.
  • Instead, it insert each new item into the queue
    as determined by its priority.
  • Remember Assignment 1 Passport Processing?
  • Essentially, PQ is an Abstract Data Type where
    access is restricted to the minimum item only.

5
Binary Heap
  • Tree-based data structure where access is
    restricted to the minimum item only.
  • Basic operations
  • Find the minimum item in constant time
  • Insert a new item in logarithmic worst-case time
  • Delete the minimum item in logarithmic worst-case
    time

6
Properties
  • Structural Property
  • Data is stored in a complete binary tree
  • The tree is balanced, so all operations are
    guaranteed O(log n) worst case
  • Can be implemented as array or list
  • Ordering Property
  • Heap Order
  • Parent ? Child

7
Which Of These Are Binary Heaps?
1
1
2
2
2
2
3
3
1
3
2
2
8
Heap Representation
  • Root at location 0
  • Left child of i is at location 2i 1
  • Right child of i is at location 2i 2 2(i 1)
  • Parent of i is at location (i - 1) / 2

0
1
43
3
3
2
65
58
40
-1
42
4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
9
Find Minimum
  • How to access the minimum element?
  • Simple! Return root. Constant time operation ?

13
21
16
26
19
68
31
65
26
32
10
Insertion
  • Create a hole in the next available location.
    Where?
  • Bottom level, rightmost leaf.
  • Move it up towards root to maintain heap order
  • This is called percolating up

11
Insertion
  • Insert 2 (Percolate Up)

12
Insertion
  • Insert 2 (Percolate Up)

-1
0
1
43
2
8
5
65
3
40
42
58
4
0
1
43
5
3
8
65
58
40
-1
42
4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
13
Insertion
  • Insert 14

13
21
16
24
19
68
31
65
26
32
14
Insertion
  • Insert 14

13
21
16
24
19
68
14
65
26
32
15
Insertion
  • Insert 14

13
14
16
24
19
68
21
65
26
32
16
Delete Minimum
  • Only the root can be deleted. Why?
  • Access is restricted to minimum item!
  • Replace hole in the root with former last item.
  • Percolate down towards leaf to maintain heap
    order
  • Compare with smallest child, recurse.

17
Delete Minimum
  • Percolate Down

-1
0
1
43
3
2
3
65
58
40
42
4
0
1
43
3
3
2
65
58
40
-1
42
4
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
18
Delete Minimum
4
0
1
43
3
2
3
65
58
40
42
0
1
43
3
3
2
65
58
40
4
42
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
19
Delete Minimum Completed
0
3
1
43
3
2
4
65
58
40
42
3
1
43
4
3
2
65
58
40
0
42
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
20
Delete Min (Alternative)
13
14
16
19
19
68
21
65
26
32
31
21
Delete Min (Alternative)
  • Percolate Down

14
16
19
19
68
21
65
26
32
31
22
Delete Min (Alternative)
14
16
19
19
68
21
65
26
32
31
23
Delete Min (Alternative)
14
19
16
19
68
21
65
26
32
31
24
Delete Min (Alternative)
14
19
16
26
19
68
21
65
32
31
25
Delete Min (Alternative)
Instead of lots of swapping, simply store value
at the beginning, move hole around, and plug
value in at the end.
14
19
16
26
19
68
21
65
31
32
26
Heap
  • Operation sequence
  • Insert 40, 20, 5, 55, 76, 31, 3
  • Delete Min
  • Delete Min
  • Insert 10, 22
  • Delete Min
  • Delete Min

27
Heap Class Definition Constructor
  • public class MyHeap implements PriorityQueue
  • // Array implementation
  • private int data
  • int currentSize
  • // Constructor
  • public MyHeap(int maxSize)
  • data new intmaxSize
  • currentSize 0

28
Heap Indexing Methods
  • // Returns index of an elements parent
  • private int parentOf(int i)
  • return (i - 1) / 2
  • // Returns index of an elements left child
  • private int leftChildOf(int i)
  • return 2 i 1
  • // Returns index of an elements right child
  • private int rightChildOf(int i)
  • return 2 i 2
  • // Returns minimum value (i.e. heap root), a.k.a.
    findMin
  • public int peek()
  • return data0

29
Removal Insertion
  • public int poll() // a.k.a. remove, dequeue
  • int minVal peek()
  • data0 data--currentSize
  • if(currentSize gt 1) percolateDown(0)
  • return minVal
  • public void add(int value) // a.k.a. offer
  • datacurrentSizevalue
  • percolateUp(currentSize-1)

(Actually, still need to check if polling empty
array, adding to full array)
30
Percolate Up
  • protected void percolateUp (int leaf)
  • int parent parentOf(leaf)
  • int value dataleaf
  • while(leafgt0 valueltdataparent)
  • dataleafdataparent
  • leaf parent
  • parent parentOf(leaf)
  • dataleafvalue

31
Percolate Down
  • private void percolateDown (int hole)
  • int value datahole
  • while(leftChildOf(hole) lt size())
  • int child leftChildOf(hole)
  • if(rightChildOf(hole) lt size()
  • datarightChildOf(hole) lt
    datachild)
  • child rightChildOf(hole)
  • if(datachild lt value)
  • datahole datachild
  • hole child
  • datahole value

32
Fix Heap / Heapify
  • The fixHeap operation takes a complete tree that
    does not have heap order and reinstates it.
  • N insertions could be done in O(n log n)
  • But we can make a fixHeap that takes O(n) time!

33
fixHeap Algorithm
  • Inefficient version
  • fixHeap left subtree
  • fixHeap right subtree
  • percolateDown root
  • However, we dont need the recursive calls! Why?
  • If we call percolateDown in reverse level order
    from the bottom (non-leaves) upwards, by the time
    we call percolateDown on node n, its children
    are already guaranteed to be heaps!

34
Fix Heap / Heapify
  • Call percolateDown(6)

92
47
21
20
12
45
63
61
17
55
37
25
64
83
73
35
Fix Heap / Heapify
  • Call percolateDown(5)

92
47
21
20
12
45
63
61
17
55
37
25
64
83
73
36
Fix Heap / Heapify
  • Call percolateDown(4)

92
47
21
20
12
25
63
61
17
55
37
45
64
83
73
37
Fix Heap / Heapify
  • Call percolateDown(3)

92
47
21
20
12
25
63
61
17
55
37
45
64
83
73
38
Fix Heap / Heapify
  • Call percolateDown(2)

92
47
21
17
12
25
63
61
20
55
37
45
64
83
73
39
Fix Heap / Heapify
  • Call percolateDown(1)

92
47
21
17
12
25
63
61
20
55
37
45
64
83
73
40
Fix Heap / Heapify
  • Call percolateDown(0)

92
12
21
17
37
25
63
61
20
55
47
45
64
83
73
41
Max Heap
  • Heaps can also store maximum item (instead of
    min).

97
53
59
26
58
31
41
16
21
36
53
59
26
41
58
31
16
21
36
97
0
1
2
3
4
5
6
7
8
9
10
11
12
13
42
Heap after the first deleteMax
59
53
58
26
36
31
41
16
21
97
53
58
26
41
36
31
16
21
97
59
0
1
2
3
4
5
6
7
8
9
10
11
12
13
43
Heap after second deleteMax
58
53
36
26
21
31
41
16
97
59
53
36
26
41
21
31
16
59
97
58
0
1
2
3
4
5
6
7
8
9
10
11
12
13
44
Heap Sort
  • Create a heap tree
  • Remove the root elements from the heap one at a
    time, recomposing the heap

45
Summary
  • Priority queue can be implemented using binary
    heap
  • Properties of binary heap
  • structural property complete binary tree
  • ordering property Parent ? Child
  • Operations of binary heap
  • insertion O(log n)
  • find min O(1)
  • delete min O(log n)
  • heapify O(n)
  • Binary heap can be used for sorting
Write a Comment
User Comments (0)
About PowerShow.com