ISF DP Computer Science

Abstract Data Structures #


Review Tools #

Topic 5 Revision from Computer Science Cafe.

Topic 5 Key Terminology from Computer Science Cafe.

Video 1 from CS Classroom (Intro, 2D Arrays, Recursion)

Video 2 from CS Classroom (Stacks and Queues)


Key Terms (covered) #


Example Problems Stacks + Queues #

1. Identify two applications of a stack. [2]


2. Identify two applications of queues in computing. [2]


3. Stack operations. [6]

A stack is a data structure that is used in the implementation of a recursive method.

(b) Outline the purpose of the stack access method isEmpty(). [2] #


The stack TOWNS holds several town names, and the name “Cardiff”is on the top of the TOWNS stack (see Figure 1a).

An algorithm is needed that will reverse the contents of the TOWNS stack. The name “Geneva” should be on top of the TOWNS stack after reversing its contents (see Figure 1b).

(c) Construct an algorithm that will reverse the TOWNS stack using an empty queue. [5] #


4. A computer science student is coding and running a program while several documents, such as essays, lab reports and homework, are being printed out.

(a) Define the term queue as a data structure. [1] #

(b) Identify two different queues used in the given scenario. [2] #


5. Identify one characteristic of a queue. [1]


Example Problems 2D Arrays #

1. 2D Arrays using dice rolls. [15]

A program is developed to simulate the roll of dice in a game.

Three dice are thrown, with faces that have numbers from 1 to 6.

The dice are thrown seven times, and the data are stored in a two-dimensional array called DICEDIAL (see Figure 2).

(a) Construct an algorithm in pseudocode to calculate the sum of all values stored in the DICEDIAL array. [3] #


The sub-program DuplicateNum(DICEDIAL,R) checks whether there are repeated numbers in row R. If the numbers are not repeated, it returns 0, otherwise it returns the repeated number.

The DuplicateNum() sub-program will produce the following from the values used in Figure 2:

DuplicateNum(DICEDIAL,0) returns 2

DuplicateNum (DICEDIAL,1) returns 4

DuplicateNum(DICEDIAL,2) returns 0

(b) Construct an algorithm in pseudocode for the sub-program DuplicateNum(DICEDIAL, R). [4] #


The sub-program highestRT(DICEDIAL) accepts the DICEDIAL array and outputs the highest row total and the indexes of all the rows with that total.

From the example data given in Figure 2, highestRT(DICEDIAL) would output that the highest row total is 16, and it occurs in the rows with indexes 3 and 4.

(c) Construct an algorithm in pseudocode for the sub-program highestRT(DICEDIAL). [8] #


2. Bus Stops [15]

A bus company provides services within a city. Passengers can look up the distance between any two bus stations on any of its routes.

For each route, a one-dimensional string array is used to store the names of all bus stations on the route, and a two-dimensional array is used to store the distances between the bus stations (in kilometers). Only the lower triangle of the two-dimensional array is used to store the distances.

Figure 1 shows data about Route X, a bus route between Oppox and Dovely.

For example, the distance between Kingsley and Kronos (2.0 kilometers) can be found in ROUTE_X_DISTANCES[7][5].

(a) State the distance between Kiko and Longlines. [1] #


The two-dimensional array ROUTE_X_DISTANCES is valid if all the entries on and above the main diagonal are zero and all the entries below the main diagonal are greater than zero.

(b) Construct an algorithm in pseudocode that checks the elements of the array ROUTE_X_DISTANCES and outputs whether the array is valid or not. [5] #

Note: Marks should also be awarded if a candidate wrote the algorithm in Java/Python/Javascript.


(c) Construct an algorithm in pseudocode that inputs the names of two bus stations and outputs the distance between them. If any of the inputted names are not found, the method should output an appropriate message. [6] #

Note: Award marks if algorithm is presented in a Java/Python/Javascript/any other program rather than IB pseudocode. For example, please see the following Javascript program


(d) The array ROUTE_X_TIMES (Figure 3) stores the approximate number of minutes it takes for a bus to travel to a bus station from the previous one. For example, ROUTE_X_TIMES[6] stores the number of minutes it takes for a bus to travel from Kingsley to Allapay: 7 minutes. #

Explain how this data could be used to determine the number of minutes it takes for a bus to travel between any two bus stations. [3]


Example Problems Recursion #

Define the term recursion. [1]


Outline two disadvantages of recursive methods. [4]


Consider the following recursive method:

def rec(A):
    if A >= 2:
        return rec(A - 2) + rec(A - 1)
    else:
        return 1

Determine the value of rec(5) (show all your working). [4] #


Explain how a stack may be used in the implementation of a recursive function. [4]


Consider the following recursive method:

fun(N)
    if N > 0
        then
            return (N mod 10) + fun(N div 10)
        else
            return 0
    end if
end fun

(a) Determine the value of fun(1216). Show all your working. [4] #

(b) Deduce the purpose of this recursive method. [2] #


Example Problems Trees #

Consider the following binary tree:

graph TB;
    A-->X;
    A-->C;
    C-->E;
    C-->Y;
    Y-->H;

(a)State the result of postorder traversal of the binary tree. [1]

(b)State the result of inorder traversal of the binary tree. [1]


(c)Describe the structure of a node in a binary tree. [2]


A decision has been made to create a new application. It will use a binary tree as an alternative to the two arrays.

(b)Identify the components of a node in a binary tree. [3]


(c)The input data will be inserted into the binary tree so that an inorder traversal of the binary tree would output all the students’ names and scores, sorted from the highest to the lowest score. Describe the steps in this insertion process. [6]


Define the term child in relation to a binary tree. [2]


(c)Outline the steps involved in traversing the given tree using postorder tree traversal. [4]


(a)Identify one difference between a binary tree and a non-binary tree. [1]


Outline why a binary tree would be a good choice of data structure for maintaining an address book. [2]