Professional Documents
Culture Documents
Topic 5
Topic 5
Abstract Data
Structures
Thinking recursively
5.1.1 Identify a situation that requires the use of recursive thinking.
Snowflakes and fractals. Towers of Hanoi.
5.1.2 Identify recursive thinking in a specified problem solution.
5.1.3 Trace a recursive algorithm to express a solution to a problem.
State the output of the recursive algorithm. For example, trees.
Abstract data structures
5.1.4 Describe the characteristics of a two-dimensional array.
5.1.5 Construct algorithms using two-dimensional arrays.
5.1.6 Describe the characteristics and applications of a stack.
Last in, first out (LIFO).
Applications of stacks: running recursive processes, return memory addresses.
5.1.7 Construct algorithms using the access methods of a stack.
Access methods: push, pop, isEmpty
5.1.8 Describe the characteristics and applications of a queue.
First in, first out (FIFO).
Applications of queues: print queues, computer modelling of physical queues (supermarket
checkouts)
Both linear and circular implementation of a queue are required.
5.1.9 Construct algorithms using the access methods of a queue.
Access methods: enqueue, dequeue, isEmpty.
5.1.10 Explain the use of arrays as static stacks and queues.
Explain push and pop operations, and test on empty/full stack.
Explain enqueue and dequeue operations, and test on empty/full queue.
Linked lists
Diagrams and descriptions. Not expected to construct linked list algorithms using pseudocode.
5.1.11 Describe the features and characteristics of a dynamic data structure.
Concepts of nodes and pointer.
5.1.12 Describe how linked lists operate logically.
5.1.13 Sketch linked lists (single, double and circular).
Sketch diagrams illustrating: adding a data item to linked list, deleting specified data item,
modifying the data held in the linked list, searching for a given data item.
Trees
Diagrams and descriptions. Not expected to construct or trace tree algorithms.
5.1.14 Describe how trees operate logically (both binary and non-binary).
5.1.15 Define the terms: parent, left-child, right-child, subtree, root and leaf. Only to a binary tree.
5.1.16 State the result of inorder, postorder and preorder tree traversal.
5.1.17 Sketch binary trees.
Adding a new data item, adding one or more new nodes, and/or removing one or more nodes.
Applications
5.1.18 Define the term dynamic data structure.
5.1.19 Compare the use of static and dynamic data structures.
5.1.20 Suggest a suitable structure for a given situation.
ORDER
Recursive thinking
Abstract data structures: Static versus dynamic structures, suitability
Queues
Can be implemented as static or dynamic structures
Stacks
Binary trees
Thinking recursively Definitions
Identify the need and uses
Trace algorithms
Thinking recursively Definitions
Recursion: Solution of a problem in tems of itself.
• Creates looping behaviour without actually using a while/for loop: A method will call itself
until some terminating condition is met, without any specific repetition construct.
• Break down the problem into smaller subtasks
• Alternative to iteration → Every algorithm done recursively can be done iteratively
• Harder to code
• The computer "remembers" every previous state of the problem. This information is
"held" by the computer on the "activation stack" (i.e., inside each function's memory
workspace). Every function has its own workspace for every call of the function.
• More elegant than the iterative equivalent, but stack space is limited, so only a certain
amount of recursive steps can be taken before it runs out of memory.
Thinking recursively Components of a recursive algorithm
o Factorials
o Towers of Hanoi
Examples o Koch snowflakes
o Fractal trees
o Fibonacci sequence
# Recursive solution
def factorial (n):
if n > 1 :
return n * factorial(n -1 )
# Notice the function is calling itself!
else:
return n
Thinking recursively Towers of Hanoi
Puzzle that consists of three rods and a number of disks of different size, which can slide onto
any rod and are initially stacked from largest to smallest on the left rod. The rules are:
• We are finished when the entire tower has been moved to another rod.
• We can only move one disk at a time.
• We can never place a larger disk on a smaller one.
Generalized version
• Move n-1 disks from rod A to rod B, leaving
disk n in rod A
• Move disk n from rod A to rod C
• Move n-1 disks from rod B to rod C
First 4 iterations
Thinking recursively Koch snowflake
Pseudocode