Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming II Overview of Collections

Similar presentations


Presentation on theme: "Fundamentals of Programming II Overview of Collections"— Presentation transcript:

1 Fundamentals of Programming II Overview of Collections
Computer Science 112 Fundamentals of Programming II Overview of Collections

2 Why 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 0 1 2 D1 D2 D3
D1 D2 D3 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 0 1 2 D1 D2 D3
D1 D2 D3 Lists allow accesses, replacements, insertions, and removals at any position

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

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

10 Linear Collections List Stack Queue Priority Queue 0 1 2 D1 D2 D3
D1 D2 D3 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 D2 D1 D3 Each element except the root has a unique predecessor Each can have zero or more successors

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

13 Unordered Collections
Bag Set Dictionary D1 D2 D3 D4 D5 No notion of position from the user’s perspective We can visit all the objects with a for loop

14 Sorted Collections Sorted Set Sorted Dictionary D1 D2 D3 D4 D5
The elements are not ordered by position, but they are ordered by content (ascending order)

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 Searching, Sorting, and Complexity Analysis Chapter 3
For Monday Searching, Sorting, and Complexity Analysis Chapter 3


Download ppt "Fundamentals of Programming II Overview of Collections"

Similar presentations


Ads by Google