CS Grade 9-12

Data Structures: Stacks, Queues, and Trees

Practice core operations and use cases for common data structures

View Answer Key
Name:
Date:
Score: / 12

Practice core operations and use cases for common data structures

CS - Grade 9-12

Instructions: Read each problem carefully. Show your work in the space provided.
  1. 1
    Diagram of a stack with the top item being removed.

    A stack starts empty. The operations are push(4), push(9), push(2), pop(), push(7), pop(). What values are removed by the two pop operations, in order, and what remains in the stack from bottom to top?

  2. 2
    Diagram of a queue with items entering at the back and leaving at the front.

    A queue starts empty. The operations are enqueue(A), enqueue(B), enqueue(C), dequeue(), enqueue(D), dequeue(). What values are removed by the two dequeue operations, in order, and what remains in the queue from front to back?

  3. 3
    Comparison of stack behavior and queue behavior.

    Explain the difference between LIFO and FIFO. Name one data structure that uses each rule.

  4. 4
    Stack used to track opening parentheses.

    A program checks whether parentheses are balanced in the expression (a + b) * (c + d). Which data structure is best for tracking the opening parentheses, and why?

  5. 5
    Printer jobs waiting in a first-in-first-out queue.

    A printer receives jobs in the order Job1, Job2, Job3, and Job4. If the printer processes jobs in the order they arrive, which data structure should manage the jobs? What job is processed first?

  6. 6
    Binary tree shape with three leaf nodes highlighted.

    Draw or describe the binary tree with root 10, left child 5, right child 15, left child of 5 equal to 2, and right child of 5 equal to 7. Then list the leaf nodes.

  7. 7
    Binary tree with arrows showing preorder traversal pattern.

    For the binary tree with root A, left child B, right child C, left child of B equal to D, and right child of B equal to E, write the preorder traversal.

  8. 8
    Binary tree with arrows showing inorder traversal pattern.

    For the binary tree with root A, left child B, right child C, left child of B equal to D, and right child of B equal to E, write the inorder traversal.

  9. 9
    Binary search tree with a left-to-right inorder traversal cue.

    For the binary search tree containing the values 8, 3, 10, 1, 6, 14, 4, 7, and 13, list the values in sorted order using an inorder traversal.

  10. 10
    Binary search tree with the root's left and right children highlighted.

    A binary search tree is empty. Insert the values 12, 5, 18, 2, 9, and 15 in that order. What are the left and right children of 12?

  11. 11
    Undo history represented as a stack where the top action is removed first.

    Choose the best data structure for an undo feature in a text editor: stack, queue, or tree. Explain your choice.

  12. 12
    Customer service line showing first-come-first-served queue behavior.

    A customer service system must help customers in the same order they joined the waiting list. Choose the best data structure and explain what operation adds a customer and what operation serves a customer.

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets