0% found this document useful (0 votes)
59 views2 pages

Programming Techniques Homework 5 Recursion

The document discusses recursive algorithms and binary trees. It provides examples of a recursive routine that calculates a value and traces the execution of a tree traversal procedure. It asks questions about properties of recursive algorithms, calling a recursive routine, comparing iterative and recursive routines, and tracing a tree traversal.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
59 views2 pages

Programming Techniques Homework 5 Recursion

The document discusses recursive algorithms and binary trees. It provides examples of a recursive routine that calculates a value and traces the execution of a tree traversal procedure. It asks questions about properties of recursive algorithms, calling a recursive routine, comparing iterative and recursive routines, and tracing a tree traversal.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

Homework 5 Recursion

Unit 11 Programming Techniques

Homework 5 Recursion
1. Give three properties of a recursive algorithm. [3]

2. A recursive routine is shown below:

function calc (n)


if n > 0 then
n = n + calc (n - 1)
endif
return n
endfunction

(a) State the purpose of the routine.


[1]

(b) Write pseudocode statements to call the routine with a parameter 5 and print the
output.
[2]

(c) What will be output?


[1]

3. Compare the advantages and disadvantages of iterative and recursive routines.


[3]

1
Homework 5 Recursion
Unit 11 Programming Techniques

4. A binary tree is shown below.

John

Alan Peggy

Chris Ken Sue

The binary tree may be represented using three one-dimensional arrays named left, name,
right.
Index left name right
[0] 2 John 1
[1] 5 Peggy 4
[2] -1 Alan 3
[3] -1 Chris -1
[4] -1 Sue -1
[5] -1 Ken -1
The procedure below describes a type of tree traversal that can be carried out on the tree.
procedure traverse (pos)
if left(pos) != -1 then traverse (left(pos))
if right(pos) != -1 then traverse (right (pos))
print name(pos)
endprocedure
Using the table below trace the execution of the program when it is called with
traverse(0). [5]

pos output

Total 15 marks
2

You might also like