0% found this document useful (0 votes)
80 views

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy