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

Topic 5 - Abstract Data Structures HL

The document discusses recursion and recursive algorithms. It provides examples of recursively calculating triangular numbers and traversing a binary tree. Recursion involves dividing a problem into smaller subproblems until reaching a base case, then using the results to solve the original problem. Recursive algorithms are more intuitive for some tasks compared to loops. Traversing a binary tree recursively involves pre-order, in-order, and post-order traversal methods.

Uploaded by

6137 Denize
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Topic 5 - Abstract Data Structures HL

The document discusses recursion and recursive algorithms. It provides examples of recursively calculating triangular numbers and traversing a binary tree. Recursion involves dividing a problem into smaller subproblems until reaching a base case, then using the results to solve the original problem. Recursive algorithms are more intuitive for some tasks compared to loops. Traversing a binary tree recursively involves pre-order, in-order, and post-order traversal methods.

Uploaded by

6137 Denize
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Thinking recursively:

Recursion - the concept of a function or method calling itself repeatedly. This is used to
accomplish repetitive tasks similar to a loop.

Why not normal loops?

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

5.1.1 Identify a situation that requires the use of


recursive thinking.
mathematical fractals

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.

5.1.2 Identify recursive thinking in a specified problem


solution.
Recursion can be applied when a problem can be divided into smaller problems that are similar
to the big problem itself. A very common application of recursion in computer science is the
traversal of binary trees.

5.1.3 Trace a recursive algorithm to express a solution


to a problem.
Let’s trace the example algorithm for triangular numbers for 6 rows - triangle(6).

n return final returned value

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

arr[0][0] arr[0][1] arr[0][2] arr[0][3]

arr[1][0] arr[1][1] arr[1][2] arr[1][3]

arr[2][0] arr[2][1] arr[2][2] arr[2][3]

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 & pointers

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.

5.1.12 Describe how linked lists operate logically.


Characteristics

A linked list is based on these concepts of nodes and pointers, as shows on the following


diagram.

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

Pros & Cons

Advantages:

 flexible size
 easy to implement stacks or queues with linked lists
 simple insertion and deletion of nodes

Disadvantages:

 additional memory usage because of pointers


 sequential access only
 nodes are stored incontiguously, which can slow down access speeds

Applications

Some common applications of linked lists include:

 implementing other dynamic data structures, e.g. stacks or queues


 document viewer (e.g. images) using buttons for previous and next elements can
make use of (doubly) linked lists
 dynamic memory allocation
 process management, e.g. Linux process management

5.1.13 Sketch linked lists (single, double and circular).


See figure 1 above for the single linked list.

Doubly linked list

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.

Circular linked list

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.

Figure 5: Adding node to linked list 2

Lastly, the previous node needs to point to the new node.


Figure 6: Adding node to linked list 3

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.

Figure 7: Deleting node to linked list 1

Figure 8: Deleting node to linked list 2

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.

Figure 1: Binary tree

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 and non-binary trees

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.

5.1.16 Different tree traversal methods


Preorder traversal

A walk in which each parent node is traversed before its children.

Figure 4: Preorder Tree Traversal

1. Display the data part of the root (or current node).


2. Traverse the left subtree by recursively calling the pre-order function.
3. Traverse the right subtree by recursively calling the pre-order function.

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.

Figure 2: Inorder Tree Traversal

1. Traverse the left subtree by recursively calling the in-order function.


2. Display the data part of the root (or current node).
3. Traverse the right subtree by recursively calling the in-order function.

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.

Figure 3: Postorder Tree Traversal

1. Traverse the left subtree by recursively calling the post-order function.


2. Traverse the right subtree by recursively calling the post-order function.
3. Display the data part of the root (or current node).

Result: A, C, E, D, B, H, I, G, F.

You might also like