Overview of Collections Flashcards
What is a collection?
Container for zero or more data objects
Tracks its own size and resizing is automatic
Comes with built-in operations for access, insertions, removals, etc.
What do we do with the details of a collection’s underlying data structures and operations? Why?
We ignore them because they are hidden in the implementation
Built-in python collections
String, Tuple, List, Set, Dictionary
Basic categories of collections
Linear, Hierarchical, Graph, Unordered
Types of linear collections
List, Stack, Queue, Priority Queue
Linear collection characteristics
Elements are ordered by position
Each element except the first has a unique predecessor
Each element except the last has a unique successor
List characteristics
Allow accesses, replacements, insertions, and removals at any position
Stack characteristics
Allow accesses, insertions, and removals at one end only
Queue characteristics
Allow insertions at one end and removals and accesses at the other end
Priority Queue characteristics
Sort elements by priority, but elements with equal priority are added and removed in queue-like order
Types of hierarchical collections
Binary tree, General tree, Binary search tree, Heap
Hierarchical collection characteristics
Each element except the root has a unique predecessor
Each can have zero or more successors
Types of graph collections
Undirected graph, Directed graph
Graph collection characteristics
Each element can have zero or more predecessors
Each can have zero or more successors
Types of unordered collections
Bag, Set, Dictionary
Unordered collection characteristics
No notion of position from the user’s perspective
We can visit all the objects with a for loop
Types of sorted collections
Sorted Set, Sorted Dictionary
Sorted collection characteristics
The elements are not ordered by position, but they are ordered by content (ascending order)
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
Basic types of operations
Create a collection: collection()
Obtain the string representation: str(collection)
Obtain the number of items currently in the collection: len(collection
Test an item for membership: item in collection
Iterate through the items: for item in collection:
Concatenate two collections (of the same type): collection1 + collection2
Test for equality: collection == object
Make the collection empty: collection.clear()
Add an item: collection.add(item)
Remove an item: collection.remove(item)