Problem of the Day

51
Problem of the Day You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank). You only have one match; what do you light 1 st ?

description

Problem of the Day. You are trapped alone in a dark room with: Candle; Wood stove ; and Gas lamp (with full tank). You only have one match; what do you light 1 st ?. Problem of the Day. You are trapped alone in a dark room with: Candle; Wood stove ; and Gas lamp (with full tank). - PowerPoint PPT Presentation

Transcript of Problem of the Day

Page 1: Problem of the Day

Problem of the Day

You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank).

You only have one match; what do you light 1st?

Page 2: Problem of the Day

Problem of the Day

You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank).

You only have one match; what do you light 1st?

Page 3: Problem of the Day

Problem of the Day

You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank).

You only have one match; what do you light 1st?

Page 4: Problem of the Day

Problem of the Day

You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank).

You only have one match; what do you light 1st?

The match!

Page 5: Problem of the Day

LECTURE 37:COMPLETE BINARY TREES & HEAPS

CSC 212 – Data Structures

Page 6: Problem of the Day

Heaps

Binary-tree implementation with add & remove Still structured using parent-child

relationship At most 2 children & 1 parent for each node

in tree Heaps must also satisfy 2 additional

properties Parent’s value smaller than its children’s

values Structure must form a complete binary tree

2

95

67

Page 7: Problem of the Day

BinaryTree

Picturing Linked BinaryTree

B

CA

D

B

A C

D

BinaryTreeroot

size4

Page 8: Problem of the Day

Legal

Complete Binary Tree

Specific way to organize a BinaryTree Add & remove location defined so can be

discussed For this idea, trees must maintain

specific shape Fill lowest level first, then can start new

level below it2

95

67

Illegal

2

95

7

6

Page 9: Problem of the Day

Complete Binary Tree

Specific way to organize a BinaryTree Add & remove location defined so can be

discussed For this idea, trees must maintain

specific shape Fill lowest level first, then can start new

level below it Lowest level must be filled in from left-to-

right

Legal

2

95

67

Illegal

2

95

67

Page 10: Problem of the Day

What Is Purpose of a Heap?

Root has critical element that we always access Element at root always has smallest value in

heap O(1) access time to root without any real effort

Complete binary trees makes adding data easy Create leftmost child on lowest level When a level completes, start next one

Useful when:

Page 11: Problem of the Day

Reheapify

Insertion may violate heap-order property Reheapify immediately after adding new

element Goes from new node to restore heap’s

order Compare priority of node & its parent If out of order, swap node's elements Continue reheapify with parent node

Stop only when either case occurs: Found properly ordered node & parent Binary tree's root is reached

Page 12: Problem of the Day

6

addElement() in a Heap

2

5

79

Page 13: Problem of the Day

6

1

addElement() in a Heap

2

5

79

Page 14: Problem of the Day

6

1

Start Your Reheapify!

2

5

79

Page 15: Problem of the Day

6

1

Start Your Reheapify!

2

5

79

Page 16: Problem of the Day

1

6

Start Your Reheapify!

2

5

79

Page 17: Problem of the Day

1

6

Reheapify Must Continue

2

5

79

Page 18: Problem of the Day

1

6

Reheapify Must Continue

2

5

79

Page 19: Problem of the Day

2

6

Reheapify Sounds Icky

1

5

79

Page 20: Problem of the Day

2

6

Check If We Should Continue

1

5

79

Page 21: Problem of the Day

2

6

Stop At The Root

1

5

79

Page 22: Problem of the Day

2

6

addElement() Once Again

1

5

79 3

Page 23: Problem of the Day

2

6

Reheapify Begins Anew

1

5

79 3

Page 24: Problem of the Day

2

6

Heap-Order Property Maintained

1

5

79 3

Page 25: Problem of the Day

2

6

Done With This Reheapify!

1

5

79 3

Page 26: Problem of the Day

Removing From a Heap

removeMin() must kill element at heap’s root For a complete tree, must remove last node

added How to reconcile these two different

demands? Removed node's element moved to the

root Then remove node from the complete tree Heap's order preserved by going down

Page 27: Problem of the Day

Removing From a Heap

removeMin() must kill element at heap’s root For a complete tree, must remove last node

added How to reconcile these two different

demands? Removed node's element moved to the

root Then remove node from the complete tree Heap's order preserved by going down

Censored

Page 28: Problem of the Day

Removal Reheapify

Restores heap’s order during removeMin() Reheapify removal work starts at root Swap with smallest child, if out-of-order Process then continues with old smallest

child Stop at leaf or when node is legal

Page 29: Problem of the Day

5

Before removeMin() is called

1

2

97

Page 30: Problem of the Day

5

Move Last Element Up To Root

1

2

97

Page 31: Problem of the Day

5

Move Last Element Up To Root

1

2

97

Page 32: Problem of the Day

5

Move Last Element Up To Root

9

2

7

Page 33: Problem of the Day

5

Compare Parent W/Smaller Child

9

2

7

Page 34: Problem of the Day

5

Compare Parent W/Smaller Child

9

2

7

Page 35: Problem of the Day

5

Compare Parent W/Smaller Child

2

9

7

Page 36: Problem of the Day

5

Continue Going Down W/Node

2

9

7

Page 37: Problem of the Day

5

Continue Going Down W/Node

2

9

7

Page 38: Problem of the Day

5

Swap If Out Of Order

2

7

9

Page 39: Problem of the Day

5

Check If We Should Continue

2

7

9

Page 40: Problem of the Day

5

Stop When We Reach a Leaf

2

7

9

Page 41: Problem of the Day

Implementation Excuses

Reheapify steps will travel height of tree O(log n) running time for each of these Serves as bound for adding & removing

from Heap What drawbacks does heap have?

Coding and understanding heap can be confusing

Use unclear w/o PriorityQueue (hint: Monday)

Page 42: Problem of the Day

Array-based BinaryTree

Node at index specified for location in TREE Root node stored at index 0 Root’s left child at index 1 Right child of root at index 2 Left child’s right child at index 4 Right child’s left child at index 5 Node at index n’s left child is at index 2n + 1 Node at index n’s right child is at index 2n +

2

Page 43: Problem of the Day

Array-based BinaryTree

Node at index specified for location in TREE Root node stored at index 0 Root’s left child at index 1 Right child of root at index 2 Left child’s right child at index 4 Right child’s left child at index 5 Node at index n’s left child is at index 2n + 1 Node at index n’s right child is at index 2n +

2But how much space will

this need for to hold a heap?

Page 44: Problem of the Day

Array to Implement Heap

2

0

2

0

Page 45: Problem of the Day

Array to Implement Heap

2

9

0 1

2

0

1

9

Page 46: Problem of the Day

Array to Implement Heap

2

9 3

0 1 2

2

0

1

9

2

3

`

Page 47: Problem of the Day

Array to Implement Heap

2

9 3

99

0 1 2 3

2

0

1

9

2

3

3

99

`

Page 48: Problem of the Day

Array to Implement Heap

2

9 3

99

0 1 2 3

2

0

1

9

2

3

3

99

`

Add nodes to end of the SequenceSimilarly, remove node at end

NO space is wasted for this!

Page 49: Problem of the Day

Heaps

Binary-tree implementation with add & remove Still structured using parent-child

relationship At most 2 children & 1 parent for each node

in tree Heaps must also satisfy 2 additional

properties Parent’s value smaller than its children’s

values Structure must form a complete binary tree

2

95

67

Page 50: Problem of the Day

Your Turn

Get into your groups and complete activity

Page 51: Problem of the Day

For Next Lecture

Read 12.2 for Monday's lecture How does a heap relate to PriorityQueue? How do computers prioritize, & what does it

matter? How would we organize the patients in an

ER?

Week #13 posted today & due week from Tues.

Programming Assignment #2 available now