13.4.3. Homework#
Use these questions after class to check your understanding of the chapter concepts and coding skills.
Sign in from the account menu before you start working on the homework.
Complete all true/false and coding questions before you submit the homework.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Stacks
Undo history is naturally modeled as a stack because the most recent action is removed first.
2. Queues
A service line is naturally modeled as a queue because the earliest arrival should usually be served first.
3. Trees
A tree node can have any number of parents and still remain a tree.
4. Graphs
Course prerequisites can be represented as a graph because courses can connect to other courses through dependency relationships.
5. ADT Interfaces
An abstract data type is defined mainly by the exact variable names used inside its implementation.
### 6. Use a stack
# Push three pages and pop the most recent page. Print exactly these two lines:
# # Back to: checkout
# # Remaining stack: ['home', 'products']
history = []
### Your code starts here.
### Your code ends here.
Back to: checkout
Remaining stack: ['home', 'products']
### 7. Traverse a binary tree
# Use the Node class to build this binary tree:
# 1
# / \
# 2 3
# / / \
# 4 5 6
# Write preorder, inorder, and postorder functions that each return a list of values.
# Print exactly these three lines:
# # Preorder: [1, 2, 4, 3, 5, 6]
# # Inorder: [4, 2, 1, 5, 3, 6]
# # Postorder: [4, 2, 5, 6, 3, 1]
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
### Your code starts here.
### Your code ends here.
Preorder: [1, 2, 4, 3, 5, 6]
Inorder: [4, 2, 1, 5, 3, 6]
Postorder: [4, 2, 5, 6, 3, 1]
### 8. Represent a tree
# Use the TreeNode class to build this team tree, with CEO as the root:
# CEO
# Data
# Operations
# Link each child to its parent with add_child(). Then build a list of the values of
# the root's children. Print exactly this line:
# # Direct reports: ['Data', 'Operations']
class TreeNode:
def __init__(self, value):
self.value = value
self.children = []
def add_child(self, child):
self.children.append(child)
### Your code starts here.
### Your code ends here.
Direct reports: ['Data', 'Operations']
### 9. Represent a graph
# Use a dictionary of adjacency lists. Print exactly this line:
# # DS200 neighbors: ['DS300', 'ML310']
graph = {"DS100": ["DS200"], "DS200": ["DS300", "ML310"]}
### Your code starts here.
### Your code ends here.
DS200 neighbors: ['DS300', 'ML310']
### 10. Build a binary search tree
# Write insert(root, value) and contains(root, value) for a binary search tree.
# Insert the values in order, starting from an empty tree (None).
# Then print the inorder traversal and check one present value and one absent value.
# Print exactly these three lines:
# # Inorder: [10, 25, 30, 45, 60, 70, 85]
# # Contains 30: True
# # Contains 55: False
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
values = [45, 25, 70, 10, 30, 60, 85]
### Your code starts here.
### Your code ends here.
Inorder: [10, 25, 30, 45, 60, 70, 85]
Contains 30: True
Contains 55: False