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?
Tree List & BST Iterators
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: CFGJPY
Tree->Array Trees good general purpose structure – Sorted – O(logN)* add/remove/find ArrayList better at random access: CFGJPY
Tree->Array Print in order : InOrder Traversal – Left – Current – Right
Tree->Array Transform to Vector – Make vector – InOrder Traversal, adding current node to vector Recursive helper –
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector CFGJPY
Tree->Array Transform to Vector BigO: – T(n) = 2T(n/2) + 1… O(n) – Each node processed once
Array Tree Transform to Tree CFGJPY
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 CFGJPY 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 afterwards Read: 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(n 2 ) Option 3 : read into BST, extract list from it Read: n * O(logn), Extract: n*O(1) : Total = O(nlogn)
Iterators Why iterators? – Provide protected, efficient access How would we traverse from outside???
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 child Stop 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 child Stop 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 child Stop 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 adding each to stack MyIterator Stack: C G nullptr
Iteration Iterator needs – Stack of node pointers nullptr = end – Start by sliding left and adding each to stack – Top of stack is current location MyIterator Stack: C G nullptr
Iteration Iterator needs – Stack of node pointers nullptr = end – Start by sliding left and adding each to stack – Top of stack is current location – Iterators == if have same top: MyIterator Stack: C G nullptr
Iteration Stack = list of ancestors we still need to process – Once processed, pop MyIterator Stack: C G nullptr
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise depends Top of stack has next node! MyIterator Stack: C G nullptr
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: C G nullptr C
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: G nullptr C
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: F G nullptr C
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: G nullptr C F
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: G nullptr C F
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: nullptr C F G
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: P nullptr C F G
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: J P nullptr C F G
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: P nullptr C F G J
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: P nullptr C F G J
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: nullptr C F G J P
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: Y nullptr C F G J P
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: nullptr C F G J P Y
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: nullptr C F G J P Y
Iteration Where do I go next? – Pop top of stack – If right child exists Go right 1 Slide left as far as you can adding each to stack – Otherwise Nothing MyIterator Stack: nullptr C F G J P Y Nullptr – must be done!
The BST BST provides – begin() : makes an iterator from the root node… Iterator constructor searches for smallest element, builds stack – end() : iterator with just null
Stacks & Trees Any iterative traversal of tree needs storage – Preorder / Inorder / Reverse : Stack – Postorder : Stack Store current node and state : rightNext vs done – Level order : Queue