Question bank › IB Computer Science › Theme B – Computational thinking & problem-solving

B4 Abstract data types: stacks, queues, linked lists, trees (HL)

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core4 marks

The diagram shows the initial contents of a stack, with 5 at the top.

The following operations are performed in order: push(6), pop(), pop(), push(1), peek().

(a) State the values returned by the two pop() operations. [1]

(b) State the value returned by peek(). [1]

(c) Write down the final contents of the stack from bottom to top and state the number of items. [2]

835topInitial stack
2StandardHL only4 marks

A singly linked list stores the nodes A, B, C and D as shown. Each node contains a data value and a pointer to the next node, and head points to node A.

(a) Describe the steps required to delete node C from the list. [3]

(b) State one advantage of a linked list over an array when many deletions and insertions are needed. [1]

headABCDD's pointer is null
3HardHL only5 marks

The binary search tree shown was built by inserting the values 50, 30, 70, 20, 40, 60, 80 and 35 in that order.

(a) Write down the sequence of values produced by an in-order, a pre-order and a post-order traversal. [3]

(b) State the number of nodes examined (including the node holding 35) when searching for the value 35, starting at the root. [1]

(c) State which of the three traversals visits the values in ascending order. [1]

5030702040608035
4Standard5 marks

A stack is used to evaluate the postfix (reverse Polish) expression 5 3 + 2 * 4 -. Operands are pushed; when an operator is read, two values are popped, the operation is applied and the result is pushed.

(a) Show the contents of the stack after the token + and after the token * has been processed. [2]

(b) State the final result. [1]

(c) When the operator - is processed, state the two values popped, in the order they are popped, and identify which of them is the left-hand operand of the subtraction. [2]

5Hard6 marks

Write a Python class Stack that implements a stack of fixed maximum size using a list and an integer top. It must include:

  • a constructor taking the maximum size;
  • methods is_empty() and is_full();
  • a method push(item) that raises an error if the stack is full;
  • a method pop() that returns the top item and raises an error if the stack is empty.

[6]

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro