Data structures worksheet with answers

Free printable Data structures worksheet: 8 problems from easy to exam level, with an answer key. Print it or save it as a PDF.

For teachers: free to print, copy and use in class. Every new worksheet has new values and its own answer key. A link to ExamProblems on your course page is appreciated.

Name: ____________________   Date: ____________

1.Easy

A graph consists of:

  1. ARows and columns
  2. BVertices (nodes) connected by edges
  3. COnly a root and leaves
  4. DKeys and hashes only
2.Easy

In which order does a postorder traversal of a binary tree visit nodes?

  1. ARight subtree, then node, then left subtree
  2. BNode, then left subtree, then right subtree
  3. CLeft subtree, then node, then right subtree
  4. DLeft subtree, then right subtree, then node
3.Easy

What is the name of the stack operation that adds an element to the top?

  1. APush
  2. BEnqueue
  3. CPop
  4. DPeek
4.Medium

In a circular queue stored in an array of capacity n, how is the next position after index i computed?

  1. Ai + 1
  2. B2i + 1
  3. C(i − 1) mod n
  4. D(i + 1) mod n
5.Medium

What balance condition does an AVL tree maintain at every node?

  1. AEvery node is coloured red or black
  2. BThe heights of the left and right subtrees differ by at most 1
  3. CAll leaves are at exactly the same depth
  4. DBoth subtrees of every node contain exactly the same number of nodes
6.Medium

A binary tree has root 1 with left child 2 and right child 3; node 2 has children 4 (left) and 5 (right). What is the preorder traversal?

  1. A1 2 3 4 5
  2. B1 2 4 5 3
  3. C4 2 5 1 3
  4. D4 5 2 3 1
7.Hard

An AVL node is unbalanced because its left child's right subtree became too tall (left-right case). Which fix is applied?

  1. AA single right rotation at the node
  2. BA right rotation at the left child, then a left rotation at the node
  3. CA single left rotation at the node
  4. DA left rotation at the left child, then a right rotation at the node
8.Hard

What does a splay tree do after each access?

  1. ARebuilds the whole tree in balanced form
  2. BRecolours the nodes on the search path
  3. CMoves the accessed node to a leaf
  4. DRotates the accessed node up to the root

Answer key

  1. 1. B · Vertices (nodes) connected by edges
  2. 2. D · Left subtree, then right subtree, then node
  3. 3. A · Push
  4. 4. D · (i + 1) mod n
  5. 5. B · The heights of the left and right subtrees differ by at most 1
  6. 6. B · 1 2 4 5 3
  7. 7. D · A left rotation at the left child, then a right rotation at the node
  8. 8. D · Rotates the accessed node up to the root

Full step-by-step solutions: practise this topic on examproblems.com.