14.4. Assignments#
These assignments reinforce the main Chapter 14 ideas: matching structures to operations, implementing stacks and queues, and representing relationships with trees and graphs.
14.4.1. Preview Questions#
What is the difference between an abstract data type and a concrete implementation?
Give one real-world example that behaves like a stack.
Give one real-world example that behaves like a queue.
Why is a dictionary a natural way to represent an adjacency list?
When would a tree be a better model than a graph?
14.4.2. Lab Assignment#
Build a small task-management simulation.
Requirements:
Use a queue for incoming tasks.
Use a stack for undo history.
Use a dictionary to store task metadata by task ID.
Demonstrate adding three tasks, completing two tasks, undoing one action, and looking up one task by ID.
Include short comments explaining why each structure was chosen.
14.4.3. Homework Questions#
Implement a function
is_balanced(text)that checks whether parentheses in a string are balanced.Create a queue-based simulation of a customer-service line with at least five customers.
Represent a small company hierarchy as a tree and print it with indentation.
Represent course prerequisites as a graph using a dictionary.
Write a short paragraph comparing BFS and DFS. Include one situation where each would be useful.
14.4.4. Review Questions#
What operation makes a stack different from a queue?
Why is
collections.dequebetter than a list for queue behavior?What does a node store in a linked structure?
What is the root of a tree?
What are nodes and edges in a graph?
Which traversal uses a queue: BFS or DFS?
Which traversal can naturally use a stack: BFS or DFS?