520 likes | 804 Views
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)
E N D
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?
Right Tool • Trees good general purpose structure • Sorted • O(logN)* add/remove/find *When balanced!
Right Tool • Trees good general purpose structure • Sorted • O(logN)* add/remove/find • ArrayList better at random access:
Tree->Array • Trees good general purpose structure • Sorted • O(logN)* add/remove/find • ArrayList better at random access:
Tree->Array • Print in order : InOrder Traversal • Left • Current • Right
Tree->Array • Transform to Vector • Make vector • InOrder Traversal, addingcurrent node to vector Recursive helper
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector
Tree->Array • Transform to Vector • BigO: • T(n) = 2T(n/2) + 1…O(n) • Each node processed once
ArrayTree • Transform to Tree
ArrayTree • 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)
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)
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
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)
Iterators • Why iterators? • Provide protected, efficient access • How would we traverse fromoutside???
Iteration • Given root • How do we find the first node?
Iteration • Given root • How do we find the first node? • Slide left as far as possible
Iteration • Given a node • Where do I go next?
Iteration • Given a node • Where do I go next? • Right child, if exists…
Iteration • Given a node • Where do I go next? • Right child, if exists… • Otherwise depends
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
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
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
Iteration • Iterator needs state • Maintain idea of where we are • Find our way to next node
Iteration • Iterator needs • Stack of node pointers • nullptr = end MyIterator Stack: nullptr Back of vector== Top of stack
Iteration • Iterator needs • Stack of node pointers • nullptr = end • Start by sliding left and addingeach to stack MyIterator Stack: C G nullptr
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
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:
Iteration MyIterator Stack: C G nullptr • Stack = list of ancestorswe still need to process • Once processed, pop
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!
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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