1 / 26

Problem of the Day

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 hours what length of ladder will still be above water?. Problem of the Day.

lemuel
Download Presentation

Problem of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 1’ apart • Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water?

  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 1’ apart • Tide rises at a rate of 9” per hour After 6 hours what length of ladder will still be above water? 9’ – the ladder will rise with the boat!

  3. CSC 212 – Data Structures Lecture 39:Implementing Heaps (& Trees & BinaryTrees)

  4. 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()

  5. PriorityQueueOperations

  6. 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

  7. Is PriorityQueue Linear? • PriorityQueueyet 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

  8. Is PriorityQueue Linear? • PriorityQueueyet 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

  9. Is PriorityQueue Linear? • PriorityQueueyet 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

  10. 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 5 9 7 6

  11. 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 Illegal 2 2 5 9 5 9 7 6 7 6

  12. 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 reheapifywith parent node • Stop only when either case occurs: • Found properly ordered node & parent • Binary tree's root is reached

  13. Reheapify Down • removeMin()removes & returns root’s element • Mustremove 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

  14. D C B A Picturing LinkedBinaryTree B A C      D

  15. D C B A Picturing LinkedHeap B A C      D

  16. D C B A Nodes in a LinkedHeap B A C      D

  17. References in a LinkedHeap B B A C      D A C D

  18. 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 D B C E G H F K I J

  19. Writing Recursive Methods • Common pattern for recursive methods in Java: public static intfactorial(int i) { if (i <= 1) { return 1; } else {intnextI = i – 1;int result = factorial(nextI); return i * result; } }

  20. Writing Recursive Methods Base case: Start with check for base case • Common pattern for recursive methods in Java: public static intfactorial(inti) { if (i <= 1) { return 1;} else {intnextI = i – 1;int result = factorial(nextI); return i * result; } }

  21. Writing Recursive Methods Base case: Solution is simple • Common pattern for recursive methods in Java: public static intfactorial(inti) {if (i <= 1) { return 1; } else {intnextI = i – 1;int result = factorial(nextI); return i * result; } }

  22. Writing Recursive Methods • Recursive Step: • Take 1 step to solution • Common pattern for recursive methods in Java: public static intfactorial(inti) { if (i <= 1) { return 1; } else {intnextI = i – 1;int result = factorial(nextI); return i * result; } }

  23. Writing Recursive Methods • Recursive Step: • Take 1 step to solution • Make 1 or more recursive calls • Common pattern for recursive methods in Java: public static intfactorial(inti) { if (i <= 1) { return 1; } else {intnextI = i – 1;int result = factorial(nextI);return i * result; } }

  24. Writing Recursive Methods • Recursive Step: • Take 1 step to solution • Make 1 or more recursive calls • (Simple process computes result) • Common pattern for recursive methods in Java: public static intfactorial(inti) { if (i <= 1) { return 1; } else {intnextI = i – 1;int result = factorial(nextI);return i * result; } }

  25. Your Turn • Get into your groups and complete activity

  26. For Next Lecture • Week #12 assignment due tomorrow • Programming Assignment #2due ??? • Will send out if enough reviews received by 5PM today • BinaryTree, Heap, & PriorityQueue Quiz Wed.

More Related