Computer Science II - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Computer Science II

Description:

Heaps. What is a heap? A tree. T={R,T0,T1,...Tn-1} where. Every subtree is a heap, and ... Building a Heap. First, using an array, turn the array into a heap ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 25
Provided by: joe49
Category:
Tags: computer | heap | science

less

Transcript and Presenter's Notes

Title: Computer Science II


1
Computer Science II
  • Heaps

2
What is a heap?
  • A tree TR,T0,T1,...Tn-1where
  • Every subtree is a heap, and
  • The root of T is greater than or equal to the
    root of every subtree of T.
  • (Note An alternate definition states that R is
    less than or equal to each of its children)

3
Perfect Trees
  • This is a perfect tree
  • Its also a heap!

10
7
5
4
1
2
3
4
Complete Trees
  • This is a complete tree, but its not perfect
  • Its a complete binary tree!

10
7
5
4
1
5
Array Representation
1
  • Number the nodes, level by level

10
2
3
7
5
5
4
4
1
6
Array Representation
1
  • Create an array, where the node values are placed
    in matching index positions

10
2
3
7
5
5
4
4
1
1
2
3
4
5
10
7
5
4
1
7
Array Representation
1
  • Note how for any node, the index of its left
    child is 2i, and for its right child its 2i1

10
2
3
7
5
5
4
4
1
1
2
3
4
5
10
7
5
4
1
8
Building a Heap
  • Q Given the following complete binary tree, how
    would turn it into a heap?

1
3
2
3
7
9
5
4
4
10
9
Building a Heap
  • Q How about this tree?

1
3
2
3
7
9
5
7
4
6
4
10
12
6
10
Building a Heap
  • Heapify(A,i)
  • 1. l ? 2i
  • 2. r ? 2i 1
  • 3. If l ? HeapSize and Al gt Ai
  • 4. then largest ? l
  • 5. else largest ? i
  • 6. if r ? HeapSize and Ar gt Alargest
  • 7. then largest ? r
  • 8. if largest ? i
  • 9. then Ai ? Alargest
  • 10. Heapify(A, largest)

11
Building a Heap
  • Build-Heap(A)
  • 1. HeapSize ? length(A)
  • 2. for i ? ?length(A)/2? downto 1
  • 3. do Heapify(A,i)

12
Building a Heap
1
12
  • First, using an array, turn the array into a heap
  • That is, for each node i, make sure that it is
    larger than nodes 2i and 2i1

2
3
10
9
4
6
5
7
4
7
3
6
13
Building a Heap
1
12
  • Swap first and last elements

2
3
10
9
4
6
5
7
4
7
3
6
14
Building a Heap
1
6
  • Eliminate last element from process

2
3
10
9
4
6
5
7
4
7
3
12
15
Building a Heap
1
6
  • You now have an almost-heap
  • Percolate the new root down to where it belongs
    to form a new heap

2
3
10
9
4
6
5
7
4
7
3
12
1
2
4
5
3
6
7
6
10
4
7
9
3
12
16
Building a Heap
1
10
  • You now have an almost-heap
  • Percolate the new root down to where it belongs
    to form a new heap

2
3
6
9
4
6
5
7
4
7
3
12
1
2
4
5
3
6
7
10
6
4
7
9
3
12
17
Building a Heap
1
10
  • You now have an almost-heap
  • Percolate the new root down to where it belongs
    to form a new heap

2
3
7
9
4
6
5
7
4
6
3
12
1
2
4
5
3
6
7
10
7
4
6
9
3
12
18
Building a Heap
1
10
  • Swap first and last elements

2
3
7
9
4
6
5
7
4
6
3
12
1
2
4
5
3
6
7
10
7
4
6
9
3
12
19
Building a Heap
1
3
  • Eliminate last element from process

2
3
7
9
4
6
5
7
4
6
10
12
1
2
4
5
3
6
7
3
7
4
6
9
10
12
20
Building a Heap
1
3
  • And so on

2
3
7
9
4
6
5
7
4
6
10
12
1
2
4
5
3
6
7
3
7
4
6
9
10
12
21
Heapsort
  • Heapsort(A)
  • 1. Build-Heap(A)
  • 2. for i ? length(A) downto 2
  • 3. do A1 ? Ai
  • 4. HeapSize ? HeapSize - 1
  • 5. Heapify(A,1)

22
Questions?
  • How many comparisons does it take to build a
    heap?
  • Best case?
  • Worst case?
  • Average case?

23
Questions?
  • How many comparisons does it take perform a
    heapsort?
  • Best case?
  • Worst case?
  • Average case?

24
Questions?
  • How would you insert an element into a Heap?
  • How would you delete an element?
Write a Comment
User Comments (0)
About PowerShow.com