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.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
CS 261 – Recitation 9 & 10 Graphs & Final review
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Trees ---- Soujanya.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Binary Trees.
Searching: Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
Chapter 16 Tree Implementations
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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