Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 112 Fundamentals of Programming II Overview of Collections.

Similar presentations


Presentation on theme: "Computer Science 112 Fundamentals of Programming II Overview of Collections."— Presentation transcript:

1 Computer Science 112 Fundamentals of Programming II Overview of Collections

2 The Need for Collections Some programs need a container to manage several data objects You’ve used lists, tuples, and dictionaries in Python There are other types of collections as well

3 What Is a Collection? A collection is a container for zero or more data objects A collection tracks its own size and resizing is automatic A collection comes with built-in operations for access, insertions, removals, etc. Details of the underlying data structures and operations are hidden in the implementation, so we ignore them

4 Built in Python Collections String Tuple List Set Dictionary

5 Basic Categories of Collections Linear Hierarchical Graph Unordered

6 Linear Collections List Stack Queue Priority Queue D1D2D3 0 1 2 Elements are ordered by position Each element except the first has a unique predecessor Each element except the last has a unique successor

7 Linear Collections List Stack Queue Priority Queue D1D2D3 0 1 2 Lists allow accesses, replacements, insertions, and removals at any position

8 Linear Collections List Stack Queue Priority Queue D1D2D3 0 1 2 Stacks allow accesses, insertions and removals at one end only

9 Linear Collections List Stack Queue Priority Queue D1D2D3 0 1 2 Queues allow insertions at one end and removals and accesses at the other end

10 Linear Collections List Stack Queue Priority Queue D1D2D3 0 1 2 Priority queues sort elements by priority, but elements with equal priority are added and removed in queue-like order

11 Hierarchical Collections Binary tree General tree Binary search tree Heap D1 D2 D3 Each element except the root has a unique predecessor Each can have zero or more successors

12 Graph Collections Undirected graph Directed graph D4 D3 D5 D1D2 Each element can have zero or more predecessors Each can have zero or more successors

13 Unordered Collections Bag Set Dictionary D4 D3 D5 D1D2

14 Sorted Collections Sorted Set Sorted Dictionary D4 D3 D5 D1D2 The elements are not ordered by position, but they are ordered by content

15 Aspects of a Collection Formal properties (what it is and what you can do with it – as expressed in its interface) Implementations – array-based, link-based, etc. Performance characteristics – space/time tradeoffs of different implementations

16 Basic Types of Operations Create a collection: list(), list(aCollection) Obtain the string representation: str(aCollection) Obtain the number of items currently in the collection: len(aCollection)

17 Basic Types of Operations Test an item for membership: item in aCollection Iterate through the items: for item in aCollection:... Concatenate two collections (of the same type): aCollection1 + aCollection2 Test for equality: aCollection == anObject

18 Basic Types of Operations Make the collection empty: aCollection.clear() Add an item: aCollection.add(item) Remove an item: aCollection.remove(item)

19 For Monday Searching, Sorting, and Complexity Analysis Chapter 3


Download ppt "Computer Science 112 Fundamentals of Programming II Overview of Collections."

Similar presentations


Ads by Google