Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when...
-
Upload
ginger-nichols -
Category
Documents
-
view
214 -
download
0
Transcript of Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when...
![Page 1: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/1.jpg)
Problem of the Day
At low tide, a ship docks in the harbor Ship is 9’ above the water line when it
docks Over the side hangs a rope ladder w/ rungs
1’ apart Tide rises at a rate of 9” per hour
After 6 hourswhat length of ladder will still be above
water?
![Page 2: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/2.jpg)
Problem of the Day
At low tide, a ship docks in the harbor Ship is 9’ above the water line when it
docks Over the side hangs a rope ladder w/ rungs
1’ apart Tide rises at a rate of 9” per hour
After 6 hourswhat length of ladder will still be above
water?
9’ – the ladder will rise with the boat!
![Page 3: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/3.jpg)
LECTURE 39:IMPLEMENTING HEAPS (& TREES & BINARYTREES)
CSC 212 – Data Structures
![Page 4: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/4.jpg)
Priority Queue
Priority queue uses strict ordering of data Values assigned priority when added to the
queue Priorities used to process in completely
biased order Return element only from find() & remove()
![Page 5: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/5.jpg)
PRIORITYQUEUE Operations
DEQUE QUEUE PRIORITYQUEUE
addFirst()addLast()
enqueue() addElement()
peekFirst()peekLast()
first() find()
removeFirst()removeLast()
dequeue() remove()
![Page 6: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/6.jpg)
Elements in a PriorityQueue
PriorityQueues use more to hold data Details not specified, implementations may
differ Data stored has 2 items defining how it
is used PQ will only use priority – the importance
of element Element important data program actually
cares about
![Page 7: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/7.jpg)
Is PRIORITYQUEUE Linear?
PriorityQueue yet another Collection Prioritize each element contained in the
collection PQ is organized from lowest to highest
priority Implementation not required: this could
be ADT Often use HEAP (it is faster) & order is
theoretical
![Page 8: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/8.jpg)
Is PRIORITYQUEUE Linear?
PriorityQueue yet another Collection Prioritize each element contained in the
collection PQ is organized from lowest to highest
priority Implementation not required: this could
be ADT Often use HEAP (it is faster) & order is
theoretical
![Page 9: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/9.jpg)
Is PRIORITYQUEUE Linear?
PriorityQueue yet another Collection Prioritize each element contained in the
collection PQ is organized from lowest to highest
priority Implementation not required: this could
be ADT Often use HEAP (it is faster) & order is
theoretical
![Page 10: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/10.jpg)
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 11: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/11.jpg)
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 12: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/12.jpg)
Reheapify Up
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 13: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/13.jpg)
Reheapify Down
removeMin() removes & returns root’s element Must remove last node to remain complete
tree Last added node’s element swapped with
root’s Then remove node from the complete tree
Reheapify process restores heap’s order Starts at root and works down heap If out-of-order, swap with smaller child
Stop at leaf or when node is legal
![Page 14: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/14.jpg)
BinaryTree
Picturing LinkedBinaryTree
B
B
A C
LinkedBinaryTree
root
size4
D
A C
D
![Page 15: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/15.jpg)
BinaryTree
Picturing LinkedHeap
B
B
A C
LinkedHeap
root
size4
D
A C
D
![Page 16: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/16.jpg)
Nodes in a LinkedHeap
B
B
A CD
A C
D
![Page 17: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/17.jpg)
B
References in a LinkedHeap
B
D
A C
A C
D
![Page 18: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/18.jpg)
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree B root of this subtree D root of this subtree
Recursive methods common Can be used going up tree Required going down the tree Often easiest way to define actions
A
B DC
G HE F
I J K
![Page 19: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/19.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
![Page 20: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/20.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
Base case: Start with check for base case
![Page 21: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/21.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
Base case: Solution is simple
![Page 22: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/22.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
Recursive Step: • Take 1 step to solution
![Page 23: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/23.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
Recursive Step: • Take 1 step to solution• Make 1 or more recursive calls
![Page 24: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/24.jpg)
Common pattern for recursive methods in Java:public static int factorial(int i) { if (i <= 1) {
return 1; } else {
int nextI = i – 1; int result = factorial(nextI); return i * result; }
}
Writing Recursive Methods
Recursive Step: • Take 1 step to solution• Make 1 or more recursive calls• (Simple process computes result)
![Page 25: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/25.jpg)
Your Turn
Get into your groups and complete activity
![Page 26: Problem of the Day At low tide, a ship docks in the harbor Ship is 9’ above the water line when it docks Over the side hangs a rope ladder w/ rungs.](https://reader030.fdocuments.us/reader030/viewer/2022013004/56649d015503460f949d3a54/html5/thumbnails/26.jpg)
For Next Lecture
Week #12 assignment due tomorrow Programming Assignment #2 due ???
Will send out if enough reviews received by 5PM today
BinaryTree, Heap, & PriorityQueue Quiz Wed.