Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

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

2-D arrays Used to make lists but very different structures


Linked-lists

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

Every recursive algorithm has


• A base case : a point at which the recursion will stop because the most basic endpoint has
been reached, so a simple answer can be given.
• A test : to determine if we have reached the base case. If we don't have a test, our
recursive loop could go for infinity.
Thinking recursively Factorial
Reminder: Product of an integer and all the positive integers below it.
n!=n(n-1)! Defined in a recursive manner
# Iterative solution
What is the base case?
def factorial (n):
value = 1
What is the test?
while n > 0 : What is the recursive call?
value = value * n How would we trace this algorithm?
n = n - 1
return value

# 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.

How it works with 3 disks


Tower of Hanoi: Moving 3 disks from tower A to tower B
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Graphically Written version
Thinking recursively Towers of Hanoi

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

Recursive, because it is applied again and again


in both the first and the third steps for n-1
disks.
At some point n will be equal to 1 and a single
disk will be moved from rod A to rod C,
resulting in an algorithm with finite number of
steps.
Thinking recursively Fractal tree
The recursive steps for a fractal tree could be described as
• Draw the trunk
• At the end of the trunk, split by some angle and draw two branches
• Repeat at the end of each branch until a sufficient level of branching is reached
Thinking recursively Fibonacci

Each number is the sum of the previous two numbers.


The first few numbers in the sequence are: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
Thinking recursively Koch snowflake
Mathematical curved based on the Koch curve, developed by Helge von Koch
Can be constructed by starting with an equilateral triangle. Using recursion each line segment changes using
the following steps:
1. Divide the initial line segment into three sub-segments of the same length
2. Draw an outward pointing equilateral triangle that has the middle segment from step(1) at its base
3. Delete the line segment that is the base of the triangle from previous step

First 4 iterations
Thinking recursively Koch snowflake
Pseudocode

You might also like