reading data into sorted list

52
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

Upload: cirila

Post on 20-Jan-2016

42 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: 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)

• Option 3 : read into BST, extract list from it

Which option is asymptotically optimal?

Page 2: Reading data into sorted list

Tree List &BST Iterators

Page 3: Reading data into sorted list

Right Tool

• Trees good general purpose structure– Sorted– O(logN)* add/remove/find

*When balanced!

Page 4: Reading data into sorted list

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

Page 5: Reading data into sorted list

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

Page 6: Reading data into sorted list

Tree->Array

• Print in order : InOrder Traversal– Left– Current– Right

Page 7: Reading data into sorted list

Tree->Array

• Transform to Vector– Make vector– InOrder Traversal, adding

current node to vector

Recursive helper–

Page 8: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 9: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 10: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 11: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 12: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 13: Reading data into sorted list

Tree->Array

• Transform to Vector

0 1 2 3 4 5

C F G J P Y

Page 14: Reading data into sorted list

Tree->Array

• Transform to Vector

• BigO:– T(n) = 2T(n/2) + 1…

O(n)– Each node processed once

Page 15: Reading data into sorted list

ArrayTree

• Transform to Tree0 1 2 3 4 5

C F G J P Y

Page 16: Reading data into sorted list

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)

Page 17: Reading data into sorted list

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)

Page 18: 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)

• Option 3 : read into BST, extract list from it

Page 19: 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

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)

Page 20: Reading data into sorted list

Iterators

• Why iterators?– Provide protected, efficient access

• How would we traverse fromoutside???

Page 21: Reading data into sorted list

Iteration

• Given root• How do we find the first node?

Page 22: Reading data into sorted list

Iteration

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

Page 23: Reading data into sorted list

Iteration

• Given a node• Where do I go next?

Page 24: Reading data into sorted list

Iteration

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

Page 25: Reading data into sorted list

Iteration

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

Page 26: Reading data into sorted list

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

Page 27: Reading data into sorted list

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

Page 28: Reading data into sorted list

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

Page 29: Reading data into sorted list

Iteration

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

Page 30: Reading data into sorted list

Iteration

• Iterator needs– Stack of node pointers• nullptr = end

MyIteratorStack:nullptr

Back of vector== Top of stack

Page 31: Reading data into sorted list

Iteration

• Iterator needs– Stack of node pointers• nullptr = end

– Start by sliding left and addingeach to stack

MyIteratorStack:CGnullptr

Page 32: Reading data into sorted list

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

Page 33: Reading data into sorted list

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

Page 34: Reading data into sorted list

Iteration

• Stack = list of ancestorswe still need to process– Once processed, pop

MyIteratorStack:CGnullptr

Page 35: Reading data into sorted list

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

Page 36: Reading data into sorted list

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

Page 37: Reading data into sorted list

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

Page 38: Reading data into sorted list

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

Page 39: Reading data into sorted list

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

Page 40: Reading data into sorted list

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

Page 41: Reading data into sorted list

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

Page 42: Reading data into sorted list

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

Page 43: Reading data into sorted list

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

Page 44: Reading data into sorted list

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

Page 45: Reading data into sorted list

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

Page 46: Reading data into sorted list

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

Page 47: Reading data into sorted list

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

Page 48: Reading data into sorted list

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

Page 49: Reading data into sorted list

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

Page 50: Reading data into sorted list

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!

Page 51: Reading data into sorted list

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

Page 52: Reading data into sorted list

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