Lists A List ADT Types of Lists Using ordered lists – Tournament Maker

Slides:



Advertisements
Similar presentations
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 15 Lists. Chapter Scope Types of list collections Using lists to solve problems Various list implementations Comparing list implementations Java.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
OrderedListADT. Overview OrderedListADT: not the same as linked lists The OrderedListADT: operations and sample applications An ArrayList implementation.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
The Java Collections Framework (JCF) Introduction and review 1.
Chapter 18 Java Collections Framework
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Using the Java Collection Libraries COMP 103 # T2
The List ADT.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Building Java Programs
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Efficiency of in Binary Trees
JAVA COLLECTIONS LIBRARY
CSE 373: Data Structures and Algorithms
Building Java Programs Chapter 14
Top Ten Words that Almost Rhyme with “Peas”
Welcome to CSE 143!.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
List Data Structure.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Building Java Programs
Java Software Structures: John Lewis & Joseph Chase
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Collections Not in our text.
Computer Science and Engineering
Lecture 13: ArrayLists AP Computer Science Principles
Collections Framework
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Parsing JSON, Using Libraries, Java Collections, Generics
Recursive Objects Singly Linked Lists.
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
TCSS 143, Autumn 2004 Lecture Notes
structures and their relationships." - Linus Torvalds
Chapter 20 Lists, Stacks, Queues, and Priority Queues
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Lists A List ADT Types of Lists Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing lists with arrays and links Lists in Java Collections API Analysis of library list implementations Reading L&C 6.1-6.6

A List ADT A list a collection of items in which the items have a position It is an inherently familiar concept We keep “to-do” lists, shop with a grocery list, and invite a list of friends to a party Types of Lists Ordered Lists Unordered Lists Indexed Lists

Ordered Lists An ordered list is kept in order based on characteristics of the elements in the list, e.g. alphabetical order for names The class for objects kept in an ordered list must implement the Comparable interface When an element (object) is added to an ordered list, its position relative to the other elements stored in the list is determined by the compareTo method for their class

Unordered Lists An unordered list is not based on any characteristics of the elements themselves The class for the elements does not need to implement the Comparable interface for them to be stored in an unordered list They are stored in an order that is externally controlled by how and when the elements are added to the list

Indexed Lists An indexed list is an unordered list that also allows elements to be added, accessed, or removed based on a index value Again the order is externally controlled by how and when the elements are added and maybe by the index value used when adding You should be familiar with the ArrayList class which is a class for an Indexed list

Ordered versus Indexed Lists Is it meaningful for a list to be both ordered and indexed? No - The two concepts are not compatible In an ordered list, elements should be kept in the order determined by the compareTo method of the class of the elements In an indexed list, elements should be kept in the order indicated by the indices used when adding the elements

Text’s List Interface Hierarchy OrderedListADT<T> + add(element: T) : void <<interface>> ListADT<T> <<interface>> UnorderedListADT<T> + removeFirst() : T + removeLast() : T + remove(element :T) : T + first() : T + last() : T + contains(target : T): boolean + isEmpty() : boolean + size() : int + iterator() : Iterator<T> + toString() : String + addToFront(element: T) : void + addToRear(element: T) : void + addAfter(element: T, target : T): void <<interface>> IndexedListADT<T> + add(index: int, element: T) : void + add(element: T) : void + set(index: int, element: T) : void + get(index: int) : T + indexOf(element :T) : int + remove(index: int) : T

Using Ordered Lists In a tournament, teams play pairwise with each other in a series of games until the last two teams play in championship game To “seed” the tournament in the first round, the best team plays the worst team, the next best team plays the next worst team, and all teams are paired this way This is “fairest” because it is impossible for the best team to play the second best team in the first round – eliminating one of them

Using Ordered Lists Tournament Maker (for a number of teams that is a power of 2) Algorithm is based on adding the teams to an ordered list where the team compareTo method uses the number of wins and that determines each team’s order in the list Algorithm removes first and last team and pairs them for a game until all teams have been paired for a game (i.e, list is empty)

Using Ordered Lists For subsequent rounds, the games can be paired based on their game number Since the game numbers were determined based on the number of wins of the teams this algorithm will build out the subsequent rounds of play correctly to make it most likely that the best and second best teams make it to the championship game

UML for Tournament Maker OrderedListADT implements Team ArrayOrderedList extends ArrayList TournamentMaker Tournament implements {main} ListADT Note: Corrections to L&C Figure 6.10

Using Indexed Lists The Josephus Problem described in the text is a classic computer science problem Going around in a circle of n players, we successively eliminate every mth player until we reach the last player who wins A solution that is O(n) can be based on an indexed list

Using Indexed Lists The Josephus Problem: n = 7, m = 3 1 2 3 4 5 6 7 1

Using Indexed Lists Josephus Solution based on an ArrayList int numPeople = 7, gap = 3; int counter = gap - 1; ArrayList<Integer> list=new ArrayList<Integer>(); // init to 1, 2, ... ... while (!list.isEmpty()) { System.out.println(list.remove(counter)); numPeople = numPeople - 1; if (numPeople > 0) counter = (counter + gap - 1) % numPeople; }

Josephus Problem A recursive solution to the Josephus problem that provides the survivor’s number for gap = 2 only public static int J(int n) // n is number of soldiers { if (n == 1) return 1; // base case else if (n % 2 == 0) return 2 * J(n/2) - 1; // n is even else return 2 * J(n/2) + 1; // n is odd } The explanation of how it works is in the link: http://www.cut-the-knot.org/recurrence/r_solution.shtml

Josephus Problem Sample run for 1 to 16 soldiers with the gap = 2 > run Josephus 1. 1 2. 1 3. 3 4. 1 5. 3 6. 5 7. 7 8. 1 9. 3 10. 5 11. 7 12. 9 13. 11 14. 13 15. 15 16. 1 > Note the interesting pattern in the number of the survivor as the number of soldiers goes up. The weblink explains the reason.

Implementing Lists Please study L&C sections 6.4 and 6.5 Much of the method code is similar to code used for previously studied data structures Note the suggested use of a doubly linked list to simplify code for the remove method nodePtr.getNext().setPrevious(nodePtr.getPrevious()); nodePtr.getPrevious().setNext(nodePtr.getNext()); nodePtr next next Target for Remove previous previous

Lists in Java Collections API These are the three primary list classes: java.util.ArrayList – a list based on an underlying array java.util.LinkedList – a list based on an underlying linked structure java.util.Vector – a list that is more often used for backward compatibility today and is no longer “in vogue” for new applications (although some of its subclasses such as java.util.Stack are still of interest)

Review: Collection Interface Hierarchy Iterable <<interface>> Collection <<interface>> List <<interface>> Set <<interface>> SortedSet <<interface>> Queue

Analysis of List Implementations Much of the time analysis is similar to the analysis of methods for previous structures Some methods that are O(1) for the array implementation are O(n) for linked version Set or get element at the specified index Some methods that are O(1) for the linked implementation are O(n) for array version Add to front

Analysis: ArrayList vs LinkedList The ArrayList is more efficient If items are often added to the end of the list If items are often retrieved by index value The ArrayList is less efficient: If items are often added in the middle (all of the following items must be moved out of the way) The LinkedList is more efficient: If items are often added to the start of the list The LinkedList is less efficient: