Topic 5-Abstract Data Structures
Topic 5-Abstract Data Structures
Three
different
representatio
ns of the
same 2d array
23 34 54 77 89
D
Linked lists are normally drawn with boxes and arrows but really they
are more like this...
3F4C
data: 54
next: 3348
HEAD: AF34
210E
data: 34 AF34
next: 3F4C data: 23
next: 210E 3348
124B
data: 77
data: 89
next: 124B
next: 0000
23 34 54 77 89
D
23 34 54 77 89
D
You could make this linked list using Here is the code to add a node on
the following code: the end of the list:
Node head = new Node(); Node newNode = new Node();
head.data = 23; newNode.data = 100;
head.next = new Node(); if (head == null) {
head.next.data = 34; head = newNode;
head.next.next = new Node(); }
head.next.next.data = 54; else {
head.next.next.next = new Node(); Node tmp = head;
head.next.next.next.data = 77; while (tmp.next != null) {
head.next.next.next.next = new Node(); tmp = tmp.next;
head.next.next.next.next.data = 89; }
tmp.next = newNode;
But as you can see, this approach is }
not workable as the list grows in You no longer need to know how to
size. Instead we use loops to add code linked list algorithms, but you
nodes. may well need to describe linked
lists, how they are constructed and
how data is inserted.
© Justin Robertson 2019. All rights reserved.
Types of linked list
Singly linked list.
4 12 32 44 79
HEAD
FRONT BACK
N
To insert a new
node, you need a
pointer to the node
4 12 32 44 79
HEAD
before the location
where the new node
will go. Make sure
TMP
you understand why.
Can you explain
why?
Step 2: 24 N.NEXT = TMP.NEXT
N
The next thing to do
is get the new
node's next pointer
4 12 32 44 79
HEAD
Step 3: 24
N
why not?
If you don't already have those for your own linked list operations, it is
easy to find the end of the list using this code:
And if you want to find the last-but-one node, you can do this:
Node tmp = head;
Node lastButOne;
while (tmp.next != null) {
lastButOne = tmp;
tmp = tmp.next;
}
Shuffle all the cards, including the head pointer. (you will need two more students):
Then give each student a card at random and
tell them to keep it secret. The head pointer 3. The head node can spontaneously
should then identify herself and write the create:
address of the first node clearly on the board. a. A new node (use a blank card)
She will then play the part of a tmp pointer that b. A pointer pointing to that node
will traverse the list. 4. Any pointer pointing at a node can
instruct their node to change its data
Rules: value and the address it is pointing to.
1. Only the head/tmp pointer can ask Using these rules, practice some of the
questions. algorithms on the next slide.
2. Other students can only answer:
a. Whose address is… [address]? Each time the group decides on an action,
(Student answers by raising a hand.) e.g. asking/answering a question, changing
b. The next two questions can only be a field value, etc, get them to write it down
answered if the tmp pointer is on the board in pseudocode. Build up the
pointing at the student: full pseudocode for each algorithm.
© Justin Robertson 2019. All rights reserved.
isEmpty() returns true if the list is empty, otherwise false Linked List
getFirstElement() returns the value of the head node Methods
getLastElement() returns the value of the tail node On the left are some of
insertAtHead(num) inserts num as the new head node
the methods you can
include in your linked
insertAtTail(num) inserts num as the new tail node
list to make it more
list() output every item in the list useful.
getCount() returns the number of elements in the list
Concept questions:
exists(num) returns true if num is in the list
probably beyond the scope of the course. void insertAtTail(int n) { // insert code }
Students will have to think hard about all void insertInOrder(int n) { // insert code }
the possible preconditions such as the list
boolean isEmpty(){ // insert code }
is empty, the new node should be placed }
1 2 3
You have to move all of the discs from 1 to 3 subject to
two rules.
1 2 3
You can only move one disc at a time.
1 2 3
1 2 3
And you can't put a larger disc on top of a smaller one.
1 2 3
Now, you could start moving stuff about randomly, but
you will quickly find that this is actually quite a tough
problem, especially if you have more than a few discs.
A moment's thought draws your attention to this guy.
1 2 3
Think:
What will the state of the game be when you come to
move this disc? Take one full minute to think about it.
Don't shout out. We will discuss it in a moment.
1 2 3
Because this one is the biggest, when you move it, you
will have to move it to a rod that is empty, and if you
want to move it from 1 to 3, then all the others need to
be on 2. You quickly realise that at some point, you will
have to get to this next position...
1 2 3
Once you have reached this position, then you can
move the big one over...
1 2 3
And then work on getting the others from 2 to 3.
1 2 3
But how can you get the first four from 1 to 2? Let's
consider this as a separate problem.
1 2 3
A moment's thought draws your attention to this guy.
Like before, if you want to move it from 1 to 2, then all
the others need to be on 3 and you quickly realise that
at some point, you will have to get to this position...
1 2 3
Then you can slide the big one over, etc.
1 2 3
The problem of the Towers of Hanoi is to
move N discs from the source peg to the
destination peg via the intermediate peg.
What is the
base case
here?
Function Output
call
Actually it is.
Recursion workshee
t © Justin Robertson 2019. All rights reserved.
Discuss in your table
groups.
● In what ways is this
pattern recursive in
nature?
Hints:
if (arg1 == base_case) {
return [value]
else {
Data
All the nodes together
form a binary tree. The
Recursive! nodes within the red
triangle also form a
binary tree. The nodes
within the blue triangle
also form a binary tree.
How may binary trees
are there in total?
Binary search tree
50
25 76
12 37 65 89
6 17 32 41 59 72 83 95
Binary search tree: A binary tree that has the following properties:
• The left subtree of a node contains only nodes with data less than the
node's data.
• The right subtree of a node contains only nodes with data greater than
the node's data.
• Both the left and right subtrees are also binary search trees.
Terminology
Parent of the root node
node that
contains 12
Right subtree
50
of the tree of
Left child of which the 76
the node that 25 76 node is the
contains 25 root
12 37 65 89
6 17 32 41 59 72 83 95
Leaf nodes
Binary trees don’t have to be symmetrical
Insertion operation
We want to insert a new node into the tree. Where should we put it?
63
50
25 76
12 37 65 89
6 17 32 41 59 72 83 95
Insertion operation
It is bigger than 50,
so go down the right
subtree…
50 63
25 76
12 37 65 89
6 17 32 41 59 72 83 95
Insertion operation
It is smaller than 76,
so go down the left
subtree…
50
25 76
63
12 37 65 89
6 17 32 41 59 72 83 95
Insertion operation
It is smaller than 65,
so go down the left
subtree…
50
25 76
12 37 65 89
63
6 17 32 41 59 72 83 95
Insertion operation
It is bigger than 59,
and 59 has no right
children, so that’s
where it goes.
50
25 76
12 37 65 89
6 17 32 41 59 72 83 95
63
Task
Think about how you would code the insert operation…
50
25 76
12 37 65 89
6 17 32 41 59 72 83 95
63
The iterative way
Don’t think about this too long because there is a beautifully elegant
alternative…
private Node root;
12 37 65 89
6 17 32 41 59 72 83 95
Binary Tree Traversals
Let's say that you want to print out your binary tree. Here are the
three different traversals.
preOrder(n.right);
}
25 76
void inOrder(Node n){
if(n == null) return;
inOrder(n.left);
print(n);
12 37 65 89
inOrder(n.right);
}
void postOrder(Node n){
if(n == null) return;
postOrder(n.left); 6 17 32 41 59 72 83 95
postOrder(n.right);
print(n);
}
This is not on the syllabus any more but it's a good example of how binary trees are used.
4 × (3 + 8)
This is written in what is called infix notation, in which the
operators (+, - , x, /) are written between their operands. It is the
usual notation that you are familiar with from mathematics.
However, there are two other ways of writing expressions like these,
and both of them are used in computer science.
Examples
Infix: 4 × (3 + 8)
Prefix: × 4 + 3 8
Postfix: 4 3 8 + ×
1. 4+5
2. 4/3+7
3. (5 + 2) × (3 + 1)
4. 4 / (3 + 6) + 5 × 9
This is not on the syllabus any more but it's a good example of how binary trees are used.
Reasons:
expressions
• When we see an operand, push it onto
the stack
• When we see an operator, pop two
operands from the stack, do the
operation, and push the answer onto the
stack
• Loop
Task
• Create annotated notes in Word, using
drawing objects.
• Show how 5 x (4 + 3) is converted to
postfix notation.
• Show how the postfix expression is
evaluated using a stack.
This is not on the syllabus any more but it's a good example of how binary trees are used.
Task answer
Parse expression 5 × (4
1
+ 3) 3
Convert to postfix:
543+×
Push 5 4
Push 4
5
Push 3 (diagram 1)
Pop 3 2
7
Pop 4
5
Push 3 + 4 = 7 (diagram
2)
Pop 7
3
Pop 5 35
Push 7 × 5 = 35
(diagram 3)
Past exam questions
Past exam questions (cont.)
Recursio
n
Advantages:
A recursive function is a function that calls itself, ● Some problems are much easier to
either directly or indirectly. Recursion is a powerful program using recursion.
programming technique in which a problem is
divided into a set of similar subproblems, each Disadvantages:
solved with a trivial solution. Generally, a recursive ● Can be confusing.
function calls itself to solve its subproblems.
Douglas Crockford, JavaScript the Good Parts, 2008
● Can take up a lot of memory with
repeated calls.
● Can be slower than iterative
int fib(int n) {
if (n <= 1) alternatives.
return n;
else
return (fib(n-1) + fib(n-2));
}
boolean binSearch(int[] x, int start, int end, int n)
{
int gcd(int m, int n) { if (end < start) return false;
if ((m % n) == 0) int mid = (start+end) / 2;
return n; if (x[mid] == n) {
return true;
else } else {
return gcd(n, m % n); if (x[mid] < n) {
} return binSearch(x, mid+1, end, n);
} else {
return binSearch(x, start, mid-1, n);
int factorial(int n) {
}
if (n == 0) }
return 1; }
else
return (n * factorial(n-1));
} © Justin Robertson 2019. All rights reserved.
Junk
32 8 21 10
[1] 4 64 24 19
44 13 73 50
[0] 32 8 21 10
[1] 4 64 24 19
[2] 44 13 73 50
12 65
3 17 43 77
13 24 69