Contest Python: ACSL › Unit 9 — Data Structures
Exercises: Unit 9 — Data Structures
There are two kinds of exercises here, just as on an ACSL contest.
- Short-answer: work it out with pencil and paper and write one exact answer.
- A popped value is a number or a letter, and
NILwhen the stack or queue was empty. - A heap row or a traversal is written as its letters run together (
RORN), or as its numbers separated by single spaces (12 18 25). - A position in a heap is counted row by row, left to right, with the root at position 1.
- A depth, a path length or a count is just a whole number, such as
12.
- A popped value is a number or a letter, and
- Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file.
The ACSL rules of this unit hold in every exercise: a key equal to one already in a BST goes to the left, the root has depth 0, a "heap" is a min-heap unless the question says max-heap, and a heap is built by inserting the items one at a time, in the order given.
Each exercise is marked with the lowest ACSL division it belongs to. Do every exercise at or below your division. Exercises marked Challenge are extra practice for when you want more.
Exercise 1: Off the Top
The following commands are carried out on an empty stack.
PUSH(4)
PUSH(7)
PUSH(1)
X = POP()
PUSH(6)
PUSH(3)
Y = POP()
PUSH(8)
Z = POP()
W = POP()
What is the value of W?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 2: Wait Your Turn
The following commands are carried out on an empty queue.
PUSH(C)
PUSH(A)
PUSH(T)
X = POP()
PUSH(D)
PUSH(O)
Y = POP()
PUSH(G)
Z = POP()
W = POP()
What is the value of W?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: One value: the letter or number popped, or `NIL` if the structure was empty.
Exercise 3: Nobody Left
The following commands are carried out on an empty queue.
PUSH(5)
PUSH(8)
X = POP()
Y = POP()
PUSH(2)
Z = POP()
W = POP()
What is the value of W?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: One value: the letter or number popped, or `NIL` if the structure was empty.
Exercise 4: The Library Tree
Build a binary search tree by inserting the letters of LIBRARY, in order.
What is its internal path length?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 5: Counting Leaves
Build a binary search tree by inserting the letters of PUMPKIN, in order.
How many leaves does it have?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 6: Three of a Kind
Build a binary search tree by inserting the letters of BANANAS, in order.
What is the depth of the third A that was inserted?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 7: The Returns Desk
The first line of input is STACK or QUEUE, telling you which structure to use.
The second line is a whole number n, the number of commands.
Each of the next n lines is one command on that structure, which starts out empty:
PUSH v, wherevis a number or a word, putsvin;POPtakes an item out, by the rules of the structure.
For every POP, print the value taken out, on its own line.
If the structure is empty, print NIL instead.
Constraints
- 1 ≤ n ≤ 100, and at least one command is a
POP. - Each
vis a whole number or a word of capital letters, with no spaces.
Sample Input
STACK
7
PUSH 12
PUSH 5
POP
PUSH 9
POP
POP
POP
Sample Output
5
9
12
NIL
The fourth POP finds the stack empty.
With QUEUE on the first line, the same commands print 12, 5, 9 and NIL.
How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.
Exercise 8: How Deep Is Each Letter?
The input is one line holding a word of capital letters. Insert its letters, in order, into an empty binary search tree (a letter equal to one already in the tree goes to the left).
Print two lines:
- the depth of each letter, in the order the letters were inserted, separated by single spaces;
- the internal path length of the tree.
Constraints
- The word has between 1 and 30 letters.
Sample Input
PROGRAM
Sample Output
0 1 1 2 2 3 3
12
This is ACSL's sample tree: P is the root, and the second R goes to the left of the first.
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 9: Popped and Pushed
The following commands are carried out on an empty stack.
PUSH(5)
PUSH(3)
PUSH(9)
X = POP()
Y = POP()
PUSH(X - Y)
PUSH(X * Y)
Z = POP()
W = POP()
PUSH(Z + W)
V = POP()
What is the value of V?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 10: A Queue of Sums
The following commands are carried out on an empty queue.
PUSH(4)
PUSH(10)
PUSH(7)
X = POP()
PUSH(X * 2)
Y = POP()
PUSH(Y - X)
Z = POP()
W = POP()
PUSH(W - Z)
V = POP()
What is the value of V?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 11: Root First
Build a binary search tree by inserting the letters of ALGORITHM, in order.
Write its preorder traversal.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The letters in the order the traversal visits them, run together with no spaces (as in `RORN`).
Exercise 12: Root Last
Build a binary search tree by inserting the letters of BIRTHDAY, in order.
Write its postorder traversal.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The letters in the order the traversal visits them, run together with no spaces (as in `RORN`).
Exercise 13: Outside the Tree
Build a binary search tree by inserting the letters of TENNESSEE, in order.
What is its external path length?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 14: The Bottom Row
The letters of SCHOOLBUS are inserted, in order, into an empty min-heap.
Write the bottom row of the heap, from left to right.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The letters of the bottom row, left to right, run together with no spaces (as in `RORN`).
Exercise 15: Where Did 15 Go?
The numbers 15, 40, 22, 8, 31, 50, 27, 12 and 45 are inserted, in that order, into an empty max-heap.
Number the positions row by row, from left to right, with the root at position 1.
At what position is 15?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 16: Three Walks
The input is one line holding a word of capital letters. Insert its letters, in order, into an empty binary search tree (a letter equal to one already in the tree goes to the left).
Print three lines: the inorder, the preorder and the postorder traversal of the tree, each with its letters run together.
Constraints
- The word has between 1 and 30 letters.
Sample Input
AMERICAN
Sample Output
AACEIMNR
AAMECIRN
ACIENRMA
This is the tree of Lesson 2, Example 6.
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.
Exercise 17: Heap Rows
The first line of input is MIN or MAX.
The second line holds whole numbers separated by single spaces.
Insert the numbers, in order, into an empty min-heap (for MIN) or max-heap (for MAX), by ACSL's rule: each new number goes in the next free place, then swaps with its parent while it is out of order.
Print the heap one row per line, from the root down, with the numbers of each row from left to right, separated by single spaces.
Constraints
- There are between 1 and 50 numbers, each from -1000 to 1000.
- Numbers may repeat. A number that equals its parent does not swap.
Sample Input
MIN
40 25 31 12 18 7
Sample Output
7
18 12
40 25 31
The last row has only three numbers, because the heap holds six.
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 18: Pull Out the R
Build a binary search tree by inserting the letters of TRIANGLES, in order.
Then delete R by ACSL's rule.
What is the internal path length of the new tree?
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 19: Next in Line
The letters of HEAPSORT are inserted, in order, into an empty min-heap.
Then the root is removed by ACSL's rule.
Write the bottom row of the new heap, from left to right.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The letters of the bottom row, left to right, run together with no spaces (as in `RORN`).
Exercise 20: A New Branch
Build a binary search tree by inserting the letters of ALGORITHM, in order.
Then delete L by ACSL's rule.
Write the preorder traversal of the new tree.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The letters in the order the traversal visits them, run together with no spaces (as in `RORN`).
Exercise 21: Pruning the Tree
The first line of input is a word of capital letters. The second line is one capital letter that appears in the word exactly once.
Insert the letters of the word, in order, into an empty binary search tree (a letter equal to one already in the tree goes to the left). Then delete the given letter by ACSL's rule:
- a node with no children is removed;
- a node with one child is replaced by that child;
- a node with two children is replaced by its left child, and its right subtree becomes the right child of the right-most node of the left child's tree.
Print the preorder traversal of the new tree, with its letters run together.
Constraints
- The word has between 2 and 30 letters.
- The letter to delete may be the root.
Sample Input
KEYBOARD
K
Sample Output
EBADYOR
K is the root, with left child E and right child Y.
E becomes the root, and since E has no right child, the Y subtree becomes the right child of E.
How this is checked: Checked by running your program on 12 test inputs (1 sample, 11 hidden) and comparing its output line by line.