reading data into sorted list
DESCRIPTION
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) - PowerPoint PPT PresentationTRANSCRIPT
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:
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Trees good general purpose structure– Sorted– O(logN)* add/remove/find
• ArrayList better at random access:
0 1 2 3 4 5
C F G J P Y
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
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
0 1 2 3 4 5
C F G J P Y
Tree->Array
• Transform to Vector
• BigO:– T(n) = 2T(n/2) + 1…
O(n)– Each node processed once
ArrayTree
• Transform to Tree0 1 2 3 4 5
C F G J P Y
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
0 1 2 3 4 5
C F G J P Y
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(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 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
MyIteratorStack:nullptr
Back of vector== Top of stack
Iteration
• Iterator needs– Stack of node pointers• nullptr = end
– Start by sliding left and addingeach to stack
MyIteratorStack:CGnullptr
Iteration
• Iterator needs– Stack of node pointers• nullptr = end
– Start by sliding left and addingeach to stack
– Top of stack is current location
MyIteratorStack:CGnullptr
Iteration
• 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:
MyIteratorStack:CGnullptr
Iteration
• Stack = list of ancestorswe still need to process– Once processed, pop
MyIteratorStack:CGnullptr
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!
MyIteratorStack:CGnullptr
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
MyIteratorStack:CGnullptr
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
MyIteratorStack:Gnullptr
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
MyIteratorStack:FGnullptr
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
MyIteratorStack:Gnullptr
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
MyIteratorStack:Gnullptr
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
MyIteratorStack: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
MyIteratorStack:Pnullptr
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
MyIteratorStack:JPnullptr
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
MyIteratorStack:Pnullptr
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
MyIteratorStack:Pnullptr
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
MyIteratorStack: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
MyIteratorStack:Ynullptr
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
MyIteratorStack: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
MyIteratorStack: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
MyIteratorStack: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