Binary heaps - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Binary heaps

Description:

1. Binary heaps. binary tree that satisfy two properties. structural ... (overkill?) sorted list (unsorted) list. deleteMin. insert. 16. Other PQ Operations ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 18
Provided by: uw3
Category:
Tags: binary | heaps | overkill

less

Transcript and Presenter's Notes

Title: Binary heaps


1
Binary heaps
  • binary tree that satisfy two properties
  • structural property (is a complete tree)
  • heap-ordering property (minimum item on top)
  • Can have maximizing heaps too.

2
Defining complete trees
  • Perfect binary tree all leaves are at the same
    depth.

height h 2h1 1 nodes 2h 1 non-leaves 2h
leaves
11
21
5
25
16
9
2
30
7
10
1
3
13
19
22
3
Defining complete trees (2)
  • Complete trees, informally A perfect binary tree
    of height h-1 with leaves added at height h in
    the leftmost positions.

N-node complete tree of height h h ?log
N? 2h ? N ? 2h1 - 1
4
Complete binary tree of height h
  • For h 0, just a single node.
  • For h 1, left child or two children.
  • For h ? 2, either
  • the left subtree of the root is perfect with
    height h-1 and the right is complete with height
    h-1, OR
  • the left is complete with height h-1 and the
    right is perfect with height h-2.

5
Heap Order Property
  • Heap order property For every non-root node X,
    the key in the parent of X is less than (or equal
    to) the key in X.

10
10
80
20
80
20
99
60
40
85
15
30
50
700
not a heap
6
Heap Operations
  • findMin
  • addElement bubble up.
  • removeMin replace root and
  • bubble down.

10
80
20
99
60
40
85
50
700
65
7
addElement bubble up
10
80
20
99
60
40
85
50
700
65
15
10
80
15
99
20
40
85
50
700
65
60
8
removeMin bubble down
10
15
20
99
60
40
85
50
700
65
15
65
20
99
60
40
85
50
700
9
buildHeap
  • Build a heap from N items.
  • Idea put them into an array (in any order) and
    then fix it up.
  • for (i N/2 i gt 0 i--)
  • bubbleDown(i)

45
6
18
21
18
14
6
60
14
32
45
60
21
32
10
Representing Complete Binary Trees
1
A
From node i, left child right child parent
2
3
C
B
6
7
4
5
G
F
E
D
9
8
10
11
12
J
K
H
I
L
implicit (array) implementation
11
Why is it better?
  • no pointers (space).
  • 2, /2, are faster operations than
    dereferencing a pointer.
  • can get to parent easily
  • Can we use the implicit representation for binary
    search trees?

12
Analysis of buildHeap
  • An item that starts h nodes from a leaf moves
    down at most h nodes during its bubbling phase.
  • Let S be the sum of the heights of all the nodes
    in a perfect binary tree.
  • h 2(h-1) 4(h-2) 2h-1 1
  • 2S 2h 4(h-1) 2h-1 2 2h
    1
  • S h 2 4 8 2h-1 2h
  • 2h1 1 (h1) ? N

13
Heaps (summary)
  • addElement bubble up. O(log N) time.
  • removeMin bubble down. O(log N) time.
  • buildHeap for N items, O(N) time.
  • Heapsort
  • buildHeap on N items O(N)
  • N removeMin ops O(N log N)

14
Priority Queue ADT
  • Checkout line at the supermarket
  • Printer queues
  • operations addElement, removeMin

6 2 15 23 12 18 45 3 7
addElement
removeMin
15
Simple implementationsof the Priority Queue ADT
16
Other PQ Operations
  • decreaseKey(p, ?) bubble up
  • increaseKey(p, ?) bubble down
  • remove(p) decreaseKey(p, ?)
  • deleteMin()
  • Running time O(log N)
  • findMax O(N).

17
d-heaps
  • Same as binary heaps, except d children instead
    of 2.
  • array implementation
  • addElement O(logd N)
  • removee O(d logd N)
  • Why d-heaps?

10
16
12
80
78
27
45
15
95
23
87
95
90
59
54
Write a Comment
User Comments (0)
About PowerShow.com