Topic 5 - Abstract Data Structures HL
Topic 5 - Abstract Data Structures HL
Recursion - the concept of a function or method calling itself repeatedly. This is used to
accomplish repetitive tasks similar to a loop.
Recursive approach is more intuitive and easier – it divides a problem into smaller subproblems
until the smallest possible problem can be solved and results are carried back. (the subproblems
are all of the same type).
Function triangle(n){
if n = 1
then return 1
else
return n + triangle(n-1)
endif
The function will keep calling itself with one row less, until it reaches n = 1, where the number of
elements equals 1. The results is returned and added to the number of elements in the next row
until all rows were considered.
6 6 + triangle(5) 6 + 15 = 21
5 5 + triangle(4) 5 + 10 = 15
4 4 + triangle(3) 4 + 6 = 10
3 3 + triangle(2) 3+3=6
2 2 + triangle(1) 2+1=3
1 1 1
The algorithm will keep calling itself until the base condition n = 1 is met and a value of 1 is
returned. This is repeatedly added to the respective value of n until the value for triangle(6) is
found.
Multi-dimensional arrays
An array is a data structure which stores a collection of elements. Each element can be identified
by an array index or key.
5.1.4 Describe the characteristics of a two- dimensional
array.
A two-dimensional array is simply an array in which each element contains another array. The
following illustration shows this.
The common notation for accessing a two dimensional array is array[i][j], where i is the index
of element in the main array, and j is the index for the element in the inner array
In such an example, the index i would indicate the row and j would indicate the column.
Linked Lists
5.1.11 Describe the features and characteristics of a
dynamic data structure.
A dynamic data structure is a data structure that is flexible in growing or shrinking in size. This
can make memory allocation more efficient, by only using as much as is necessary, while also
allowing the programmer to design more flexible algorithms, where the size of a data structure is
not known before runtime.
Nodes and pointers are two concepts commonly used for dynamic data structures. The idea is
that each data element is allocated a node for storing information. In addition, each node has
a pointer which points to the memory space in which the next node (data element) is stored.
In addition, to the nodes, there is a head pointer which includes a reference to the first node in
the linked list. The pointer of the last node in the list will also indicate the end of the linked list.
This can be done in different ways, e.g. by pointing to null (nowhere).
Advantages:
flexible size
easy to implement stacks or queues with linked lists
simple insertion and deletion of nodes
Disadvantages:
Applications
In addition to the pointer to the next node, each node also has a pointer referencing the previous
node, as illustrated in figure 2.
This allows to also moving backwards through the list, potentially increasing access speeds or
making certain algorithms easier to implement. However, the tradeoff is the additional memory
space consumed by the backwards pointer.
Sometimes the last node of a linked list does not point to null, but back to the first node instead.
This is called a circular linked list and is illustrated in figure 3.
This type of linked list has a tail node instead of a head, which usually points to the last node.
This has the benefit that nodes can be appended to the end very quickly.
Adding elements
The first step is to create a pointer to the node after which the new node is to be inserted. At the
same time a new node is created.
Figure 4: Adding node to linked list 1
The next step is to set the pointer of the new node to the following node.
Note that the process is slightly different when adding a node at the beginning or end of the list.
It is also slightly different for doubly linked or circular linked lists. Figuring these processes out
can be good practise.
Deleting elements
Deleting elements is relatively easy, as it only requires to modify the previous node’s pointer to
the following node. After this the deleted node just needs to be removed from memory.
Searching elements
Searching needs to be done sequentially as in linear search, because nodes can only be
accessed by following pointers from node to node.
Binary trees
5.1.14-15 Characteristics of trees
Trees are hierarchical data structures that are best explained with a sketch.
A tree consists of nodes, which contain a piece of information and are represented by numbers
in this sketch. The uppermost node is called the root and it serves as the starting point when
retrieving data from the tree - in Figure 1 the number 8 is the root node. It has therefore has the
highest rank in the tree hierarchy. A node can link to other nodes, which are then called child
nodes - in a binary tree to a maximum of two. Consequently, the upper node is called a parent
node. For example, in the sketch number 3 is a parent node of 1 and 6, which are its child
nodes. As 6 has further child nodes, it can be considered to be the root of a subtree, which is
part of the tree as a whole. Number 1 on the other hand has no further child nodes, which is why
it is called a leaf. Other leaves here are 4, 7 and 13.
Binary trees have the characteristic of being ordered. The left-child (e.g. 3) must come before
the parent node (i.e. 8) and the right-child must come after the parent node (i.e. 10). For this
reason each node in a binary tree can have a maximum of two child nodes only.
Non-binary trees on the other hand do not follow these rules and could be used for example to
represent animal species trees.
Result: F, B, A, D, C, E, G, I, H.
Inorder traversal
A walk in which a node’s left subtree, then the node itself, and finally its right subtree are
traversed.
Result: A, B, C, D, E, F, G, H, I.
Postorder traversal
A walk in which the children are traversed before their respective parents are traversed.
Result: A, C, E, D, B, H, I, G, F.