1 / 52

Reading data into sorted list

Reading data into sorted list. Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards Option 2 : read directly into array based list, inserting each item into correct location (sort as we go)

cirila
Télécharger la présentation

Reading data into sorted list

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. Reading data into sorted list Want to suck text file in and produce sorted list of the contents: • Option 1 : read directly into array based list, sort afterwards • Option 2 : read directly into array based list, inserting each item into correct location (sort as we go) • Option 3 : read into BST, extract list from it Which option is asymptotically optimal?

  2. Tree  List &BST Iterators

  3. Right Tool • Trees good general purpose structure • Sorted • O(logN)* add/remove/find *When balanced!

  4. Right Tool • Trees good general purpose structure • Sorted • O(logN)* add/remove/find • ArrayList better at random access:

  5. Tree->Array • Trees good general purpose structure • Sorted • O(logN)* add/remove/find • ArrayList better at random access:

  6. Tree->Array • Print in order : InOrder Traversal • Left • Current • Right

  7. Tree->Array • Transform to Vector • Make vector • InOrder Traversal, addingcurrent node to vector Recursive helper

  8. Tree->Array • Transform to Vector

  9. Tree->Array • Transform to Vector

  10. Tree->Array • Transform to Vector

  11. Tree->Array • Transform to Vector

  12. Tree->Array • Transform to Vector

  13. Tree->Array • Transform to Vector

  14. Tree->Array • Transform to Vector • BigO: • T(n) = 2T(n/2) + 1…O(n) • Each node processed once

  15. ArrayTree • Transform to Tree

  16. ArrayTree • Transform to list to tree • Start with empty tree • For each item in list O(n) • Add to tree O(logn)If already sorted this won't be O(logn) without extra work • O(nlogn)

  17. Treesort • TreeSort: Sort a list by turning it into a tree then back into a list: • Put data into BST : O(n*logn) • Copy out : O(n) • Final Big O: O(nlogn + n) = O(nlogn)

  18. Reading data into sorted list Want to suck text file in and produce sorted list of the contents: • Option 1 : read directly into array based list, sort afterwards • Option 2 : read directly into array based list, inserting each item into correct location (sort as we go) • Option 3 : read into BST, extract list from it

  19. Reading data into sorted list Want to suck text file in and produce sorted list of the contents: • Option 1 : read directly into array based list, sort afterwardsRead: n * O(1) = O(n), Sort: O(nlogn) : Total = O(nlogn) • Option 2 : read directly into array based list, inserting each item into correct location (sort as we go)Read: n * O(n) : Total = O(n2) • Option 3 : read into BST, extract list from itRead: n * O(logn), Extract: n*O(1) : Total = O(nlogn)

  20. Iterators • Why iterators? • Provide protected, efficient access • How would we traverse fromoutside???

  21. Iteration • Given root • How do we find the first node?

  22. Iteration • Given root • How do we find the first node? • Slide left as far as possible

  23. Iteration • Given a node • Where do I go next?

  24. Iteration • Given a node • Where do I go next? • Right child, if exists…

  25. Iteration • Given a node • Where do I go next? • Right child, if exists… • Otherwise depends

  26. Iteration • Given a node • Where do I go next? • Right child, if exists… • Otherwise depends • I am left child • Back to parent • I am right child • Back up chain until find a left childStop at their parent

  27. Iteration • Given a node • Where do I go next? • Right child, if exists…??? • Otherwise depends • I am left child • Back to parent • I am right child • Back up chain until find a left childStop at their parent

  28. Iteration • Given a node • Where do I go next? • If right child exists • Go right 1 • Slide left as far as you can • Otherwise depends • I am left child • Back to parent • I am right child • Back up chain until find a left childStop at their parent

  29. Iteration • Iterator needs state • Maintain idea of where we are • Find our way to next node

  30. Iteration • Iterator needs • Stack of node pointers • nullptr = end MyIterator Stack: nullptr Back of vector== Top of stack

  31. Iteration • Iterator needs • Stack of node pointers • nullptr = end • Start by sliding left and addingeach to stack MyIterator Stack: C G nullptr

  32. Iteration MyIterator Stack: C G nullptr • Iterator needs • Stack of node pointers • nullptr = end • Start by sliding left and addingeach to stack • Top of stack is current location

  33. Iteration MyIterator Stack: C G nullptr • Iterator needs • Stack of node pointers • nullptr = end • Start by sliding left and addingeach to stack • Top of stack is current location • Iterators == if have same top:

  34. Iteration MyIterator Stack: C G nullptr • Stack = list of ancestorswe still need to process • Once processed, pop

  35. Iteration MyIterator Stack: C G nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise depends • Top of stack has next node!

  36. Iteration MyIterator Stack: C G nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C

  37. Iteration MyIterator Stack: G nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C

  38. Iteration MyIterator Stack: FG nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C

  39. Iteration MyIterator Stack: G nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F

  40. Iteration MyIterator Stack: G nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F

  41. Iteration MyIterator Stack: nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G

  42. Iteration MyIterator Stack: Pnullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G

  43. Iteration MyIterator Stack: JPnullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G

  44. Iteration MyIterator Stack: Pnullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J

  45. Iteration MyIterator Stack: Pnullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J

  46. Iteration MyIterator Stack: nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J P

  47. Iteration MyIterator Stack: Y nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J P

  48. Iteration MyIterator Stack: nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J P Y

  49. Iteration MyIterator Stack: nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing C F G J P Y

  50. Iteration MyIterator Stack: nullptr • Where do I go next? • Pop top of stack • If right child exists • Go right 1 • Slide left as far as you canadding each to stack • Otherwise • Nothing • Nullptr – must be done! C F G J P Y

More Related