Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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: 012345 CFGJPY

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

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

7 Tree->Array Transform to Vector – Make vector – InOrder Traversal, adding current node to vector Recursive helper –

8 Tree->Array Transform to Vector 012345 CFGJPY

9 Tree->Array Transform to Vector 012345 CFGJPY

10 Tree->Array Transform to Vector 012345 CFGJPY

11 Tree->Array Transform to Vector 012345 CFGJPY

12 Tree->Array Transform to Vector 012345 CFGJPY

13 Tree->Array Transform to Vector 012345 CFGJPY

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 012345 CFGJPY

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 012345 CFGJPY 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 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)

20 Iterators Why iterators? – Provide protected, efficient access How would we traverse from outside???

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 child Stop 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 child Stop 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 child Stop 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 adding each to stack MyIterator Stack: C G nullptr

32 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

33 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

34 Iteration Stack = list of ancestors we still need to process – Once processed, pop MyIterator Stack: C G nullptr

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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!

51 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

52 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


Download ppt "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."

Similar presentations


Ads by Google