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

In a tree, what are sibling nodes?

  1. ANodes that share the same parent
  2. BNodes that have no children
  3. CA node and its parent
  4. DNodes at the same depth in different subtrees
2.Easy

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

  1. ARight subtree, then node, then left subtree
  2. BLeft subtree, then node, then right subtree
  3. CLeft subtree, then right subtree, then node
  4. DNode, then left subtree, then right subtree
3.Easy

In an undirected graph, what is the degree of a vertex?

  1. AIts position in the adjacency list
  2. BThe number of vertices in the graph
  3. CThe number of edges connected to it
  4. DThe length of the longest path from it
4.Medium

Which data structure is used by depth-first search (iterative version)?

  1. AA hash table only
  2. BA heap
  3. CA stack
  4. DA queue
5.Medium

In a binary min-heap stored in an array (index starting at 0), the children of the node at index i are at:

  1. A2i and 2i + 1
  2. Bi/2 and i/2 + 1
  3. C2i + 1 and 2i + 2
  4. Di + 1 and i + 2
6.Medium

What is the cost of inserting an element in the middle of an array of n elements?

  1. AO(log n), because of binary search
  2. BO(n²), because each element moves n times
  3. CO(n), because later elements must shift
  4. DO(1), because the index is known
7.Hard

Which property must a red-black tree satisfy?

  1. AEvery path from a node to its descendant leaves has the same number of black nodes
  2. BThe root must be red
  3. CEvery red node must have red children, so red nodes always form unbroken chains to the leaves
  4. DEvery node must be red
8.Hard

In the BST with root 50, left subtree 30 (children 20, 40) and right subtree 70 (children 60, 80), the root is deleted using the in-order successor. Which key becomes the new root?

  1. A80
  2. B40
  3. C60
  4. D70

Answer key

  1. 1. A · Nodes that share the same parent
  2. 2. C · Left subtree, then right subtree, then node
  3. 3. C · The number of edges connected to it
  4. 4. C · A stack
  5. 5. C · 2i + 1 and 2i + 2
  6. 6. C · O(n), because later elements must shift
  7. 7. A · Every path from a node to its descendant leaves has the same number of black nodes
  8. 8. C · 60

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