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 NIL when 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.
  • Programming: write a complete program that reads the input with input() and prints exactly the output described. Save it as a .py file 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

  • Division: Junior

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

For parents and teachers: report a problem

Exercise 2: Wait Your Turn

  • Division: Junior

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.

For parents and teachers: report a problem

Exercise 3: Nobody Left

  • Division: Junior

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.

For parents and teachers: report a problem

Exercise 4: The Library Tree

  • Division: Junior

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

For parents and teachers: report a problem

Exercise 5: Counting Leaves

  • Division: Junior

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

For parents and teachers: report a problem

Exercise 6: Three of a Kind

  • Division: Junior

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

For parents and teachers: report a problem

Exercise 7: The Returns Desk

  • Division: Junior

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, where v is a number or a word, puts v in;
  • POP takes 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 v is 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.

For parents and teachers: report a problem

Exercise 8: How Deep Is Each Letter?

  • Division: Junior

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:

  1. the depth of each letter, in the order the letters were inserted, separated by single spaces;
  2. 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.

For parents and teachers: report a problem

Exercise 9: Popped and Pushed

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 10: A Queue of Sums

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 11: Root First

  • Division: Intermediate

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`).

For parents and teachers: report a problem

Exercise 12: Root Last

  • Division: Intermediate

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`).

For parents and teachers: report a problem

Exercise 13: Outside the Tree

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 14: The Bottom Row

  • Division: Intermediate

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`).

For parents and teachers: report a problem

Exercise 15: Where Did 15 Go?

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 16: Three Walks

  • Division: Intermediate

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.

For parents and teachers: report a problem

Exercise 17: Heap Rows

  • Division: Intermediate

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.

For parents and teachers: report a problem

Exercise 18: Pull Out the R

  • Division: Senior

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

For parents and teachers: report a problem

Exercise 19: Next in Line

  • Division: Senior

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`).

For parents and teachers: report a problem

Exercise 20: A New Branch

  • Challenge
  • Division: Senior

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`).

For parents and teachers: report a problem

Exercise 21: Pruning the Tree

  • Challenge
  • Division: Senior

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.

For parents and teachers: report a problem

Back to Unit 9 — Data Structures