100% found this document useful (1 vote)
164 views12 pages

Topic 5-Abstract Data Structures - Revision - Notes

The document provides an overview of abstract data structures including two-dimensional arrays, stacks, queues, linked lists, and trees. It describes their key characteristics and provides examples of algorithms that utilize each structure. For two-dimensional arrays, it constructs an algorithm to check that the sums of each row, column, and diagonal of a 2D array are correct. For stacks and queues, it gives examples of using common access methods like push, pop, and isEmpty. Finally, it differentiates between static and dynamic data structures and discusses when each may be suitable.

Uploaded by

N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
164 views12 pages

Topic 5-Abstract Data Structures - Revision - Notes

The document provides an overview of abstract data structures including two-dimensional arrays, stacks, queues, linked lists, and trees. It describes their key characteristics and provides examples of algorithms that utilize each structure. For two-dimensional arrays, it constructs an algorithm to check that the sums of each row, column, and diagonal of a 2D array are correct. For stacks and queues, it gives examples of using common access methods like push, pop, and isEmpty. Finally, it differentiates between static and dynamic data structures and discusses when each may be suitable.

Uploaded by

N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Topic 5

Abstract Data
Structures
Revision Notes

Nikos Giankos
Computer Science Teacher
Table of Contents
5.1 Abstract data structures .............................................................................................................................................. 3
Thinking Recursively ................................................................................................................................................... 3
5.1.1 Identify a situation that requires the use of recursive thinking........................................................................... 3
5.1.2 Identify recursive thinking in a specified problem solution ............................................................................... 3
5.1.3 Trace a recursive algorithm to express a solution to a problem ......................................................................... 4
Abstract Data Structures ............................................................................................................................................... 4
5.1.4 Describe the characteristics of a two-dimensional array .................................................................................... 4
5.1.5 Construct algorithms using two-dimensional arrays ......................................................................................... 4
5.1.6 Describe the characteristics and applications of a stack .................................................................................... 6
5.1.7 Construct algorithms using the access methods of a stack ................................................................................. 6
5.1.8 Describe the characteristics and applications of a queue ................................................................................... 6
5.1.9 Construct algorithms using the access methods of a queue ............................................................................... 6
5.1.10 Explain the use of arrays as static stacks and queues ...................................................................................... 6
Linked List .................................................................................................................................................................. 7
5.1.11 Describe the features and characteristics of a dynamic data structure .............................................................. 7
5.1.12 Describe how linked lists operate logically..................................................................................................... 7
5.1.13 Sketch linked lists (single, double and circular) .............................................................................................. 7
Trees ...........................................................................................................................................................................10
5.1.14 Describe how trees operate logically (both binary and non-binary) ................................................................10
5.1.15 Define the terms: parent, left-child, right-child, subtree, root and leaf ............................................................10
5.1.16 State the result of in-order, post-order, and preorder tree traversal .................................................................10
5.1.17 Sketch binary trees ........................................................................................................................................11
Application .................................................................................................................................................................11
5.1.18 Define the term dynamic data structure .........................................................................................................11
5.1.19 Compare the use of static and dynamic data structures ..................................................................................11
5.1.20 Suggest a suitable structure for a given situation ...........................................................................................12

Page 2 of 12
5.1 ABSTRACT DATA STRUCTURES
Thinking Recursively
5.1.1 Identify a situation that requires the use of recursive thinking
A recursive solution is one where the problem is broken up in to a smaller sub-problem repeatedly until the problem can be
easily solved. Each successively larger problem is then solved using the solution to the smaller sub problem. This type of
thinking can be used in many problems and sometimes (but not always) it is the easiest method of solving the problem.
Some examples of recursion that are commonly used in Computer Science are binary tree traversals and the Towers of
Hanoi problem. In general, any problem that can be broken up into a sub-problem and a base case (the smallest possible
sub-problem) can or may require the use of recursive thinking.

A Recursive function requires 2 things:


• A base case (which terminates or ends loop)
• A recursive case (which calls itself)

Advantage of Recursion
• Function calling related information will be maintained by recursion.
• Stack evaluation will be taken place by using recursion.
• In fix prefix, post-fix notation will be evaluated by using recursion.

Disadvantage of Recursion
• It is a very slow process due to stack overlapping.
• Recursive programs can create stack overflow.
• Recursive functions can create as loops.

5.1.2 Identify recursive thinking in a specified problem solution


n=5

Gives us the answer

Page 3 of 12
5.1.3 Trace a recursive algorithm to express a solution to a problem
A common basic example of a recursive algorithm is the algorithm to return the factorial of a number. The factorial is the
product of all integers from 1 up to the number. For example, 4! = 1 x 2 x 3 x 4 = 24.

private int factorial(int number){


if(number < 1){return 0;}
else if(number == 1){return 1;}
else {return factorial(number-1)*number;}
}
Here is the trace table for the above algorithm for the number 4:

Number Stack Note


contains

4 4 After calling the function, the return statement waits for a number to be returned from the function
called
3 4, 3
2 4, 3, 2
1 4, 3, 2, 1 This is returned to the function which called it which multiplies every number in the stack

Abstract Data Structures


5.1.4 Describe the characteristics of a two-dimensional array
A two-dimensional array is an array of arrays. This means that each entry in the first array is an array that contains data.
The set-up is similar to that of a table. The whole table is an array and every row within the table is one of the secondary
arrays.

Array A
Array 1 Entry A.1.1 Entry A.1.2 Entry A.1.3
Array 2 Entry A.2.1 Entry A.1.2 Entry A.2.3
Array 3 Entry A.3.1 Entry A.1.2 Entry A.3.3
Array 4 Entry A.4.1 Entry A.1.2 Entry A.4.3

Like a table, each entry can be accessed in a method similar to a coordinate system. First the number for the position in the
outer array, then the number for the position of the entry in the inner array.

5.1.5 Construct algorithms using two-dimensional arrays


A=[
[8,1,6] ,
[3,5,7] ,
[4,9,2]
]

OK = "correct"

loop R from 0 to 2
output A[R][0] , " " , A[R][1] , " " , A[R][2]
end loop

loop R from 0 to 2
SUM = 0
loop C from 0 to 2

Page 4 of 12
SUM = SUM + A[R][C]
end loop

if SUM != 15 then
output "Row " , R , " is wrong"
OK = "wrong"
end if
end loop

loop C from 0 to 2
SUM = 0
loop R from 0 to 2
SUM = SUM + A[R][C]
end loop

if SUM != 15 then
output "Column " , C , " is wrong"
OK = "wrong"
end if
end loop

SUM = 0
loop X from 0 to 2
R=X
C=X
SUM = SUM + A[R][C]
end loop

if SUM != 15 then
output "Main diag is wrong"
OK = "wrong"
end if

SUM = 0
loop X from 0 to 2
R=X
C = 2-X
SUM = SUM + A[R][C]
end loop

if SUM != 15 then
output "Other diag is wrong"
OK = "wrong"
end if

output "Entire square is " , OK

Page 5 of 12
5.1.6 Describe the characteristics and applications of a stack
A stack is a data list structure where items are added and removed from the same end. This is called LIFO (last in, first out).
A stack of plates in a cafeteria is a great example of this. Plates are added and taken off from only the top. In computer
programming a stack is often used in recursive programs where each result is put in the stack until the base case is reached
when the items are taken off the stack in the same order they were out on.

5.1.7 Construct algorithms using the access methods of a stack


The basic access methods of a stack are:
• push: push adds an element on top of the stack and changes the stack pointer to point to the new element
• pop: removes an element from the top of the stack and changes the stack pointer to point to the previous element.
• isEmpty: checks if the stack has any elements. This can be checked through the stack pointer. If the stack pointer
is undefined, the stack is empty.
.
NAMES = ["Alex","Bobby","Cho","Deke"]
STACK = new Stack()

loop COUNT from 0 to 3


STACK.push(NAMES[COUNT])
end loop

loop while NOT(STACK.isEmpty())


NAME = STACK.pop()
output NAME
end loop

5.1.8 Describe the characteristics and applications of a queue


A queue is a data list structure where items are added from 1 end and removed from the other end. This is called FIFO (first
in, first out)

Examples of the applications of queues may include print queues and the computer modelling of physical queues (eg
supermarket checkouts).

5.1.9 Construct algorithms using the access methods of a queue


Access methods include:
• enqueue: adds an element to the queue
• dequeue: removes an element from the queue
• isEmpty: checks if the queue is empty

5.1.10 Explain the use of arrays as static stacks and queues


Arrays are static data structures (can’t change size). Stacks and Queues are dynamic data structures (can change size).
Arrays can behave like a stack or a queue if enough elements are provided which allows the stack or queue to function.

One way of doing this, is to reserve the first element of the array for the stack pointer, which will contain the index of the
current top element in the stack. If the stack pointer is stored in array[0], then its initial value will be 0. If an element is now
pushed, the stack pointer value increases to 1 and the new element is stored to array[1]. The other way round if an element
is popped from the stack, the top element in the array will be set to null and the stack pointer value will be decreased by
one.

Page 6 of 12
Linked List
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


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

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 in contiguously, which can slow down access speeds

5.1.13 Sketch linked lists (single, double and circular)


Doubly linked list
In addition to the pointer to the next node, each node also has a pointer referencing the previous node, as illustrated as
follow:

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.

Page 7 of 12
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 as follow:

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.

The next step is to set the pointer of the new node to the following node.

Page 8 of 12
Lastly, the previous node needs to point to the new 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 practice.

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.

Page 9 of 12
Trees
You are only expected to be able to work with trees in diagrams and not in pseudocode.

5.1.14 Describe how trees operate logically (both binary and non-binary)
A binary tree is a tree data structure where each node has up to two child nodes, creating the branches of the tree. … Parent
nodes are nodes with children, while child nodes may include references to their parents.

Non-binary trees on the other hand do not follow these rules and could be used for example to represent animal species
trees. A non-binary, or multifurcating, tree is a tree in which at least one node has more than two children.

5.1.15 Define the terms: parent, left-child, right-child, subtree, root and leaf
• Parent: A node that has one or more children is a parent node.
• Left-child: The child node that is to the left of the parent. In binary trees the node is less than the parent node.
• Right-child: The child node that is to the right of the parent. In binary trees the node is greater than the parent
node.
• Subtree: A portion of another tree that is a tree itself.
• Root: A node that has no parent, it is at the top of the tree.
• Leaf: A node that has no children, they are at the bottom of the tree.
• Left-child and right-child apply only to binary trees. All the other definitions apply to all trees.

5.1.16 State the result of in-order, post-order, and preorder tree traversal
All of the traversals work with a recursive algorithm that is applied from each node, starting with the root.
The tree form of a simple equation

• In-order: The in-order traversal of the above tree will result in 2 x 4 + 9 / 3. The in-order traversal means that the
parent node is in between the two children nodes.

• Post-order: The post-order traversal of the above tree will result in 2 4 x 9 3 / +. Post order traversal means that
the parent node comes after the children nodes.

• Preorder: The preorder traversal of the above tree will result in + x 2 4 / 9 3. Pre order traversal means that the
parent node comes before the children nodes.

Page 10 of 12
5.1.17 Sketch binary trees
Sketching binary trees is very easy if you know how the tree is supposed to look. Just make sure that each node has no more
than 2 children. If a node is the only child of its parent, then it cannot have any children itself. If you don't know how the
tree is supposed to look then it will take a bit more work to sketch.

Application
5.1.18 Define the term dynamic data structure
Dynamic Data structure is defined as “A dynamic data structure (DDS) refers to an organization or collection of data in
memory that has the flexibility to grow or shrink in size, enabling a programmer to control exactly how much memory is
utilized.”

• These data structures can change size during the execution of a program
• Dynamic data structures grow and shrink as required by the program
• The size of the structure is determined during run- time
• They are a very efficient use of memory space

5.1.19 Compare the use of static and dynamic data structures

Page 11 of 12
5.1.20 Suggest a suitable structure for a given situation
Stacks
Best used for:
• Perhaps the most important application of stacks is to implement function calls (methods).
• Most compilers implement function calls by using a stack.
• This also provides a technique for eliminating recursion from a program: instead of calling a function recursively,
the programmer uses a stack to simulate the function calls in the same way that the compiler would have done so.
• Conversely, we can often use recursion instead of using an explicit stack.

Queues
Best used for:
• Computing applications: serving requests of a single shared resource (printer, disk, CPU),
• Buffers
• MP3 players and portable CD players, iPod playlist.
• Playlist for jukebox: add songs to the end, play from the front of the list.
• Interrupt handling: When programming a real-time system that can be interrupted (e.g., by a mouse click or
wireless connection), it is necessary to attend to the interrupts immediately, before proceeding with the current
activity. If the interrupts should be handles in the same order they arrive, then a FIFO queue is the appropriate
data structure.

Linked Lists
Best when:
• Need constant-time insertions/deletions from the list (such as in real-time computing where time predictability is
absolutely critical)
• Don’t know how many items will be in the list. With arrays, you may need to re-declare and copy memory if the
array grows too big
• Don’t need random access to any elements
• To be able to insert items in the middle of the list (such as a priority queue)

Arrays
Best when:
• You need indexed/random access to elements
• You know the number of elements in the array ahead of time so that you can allocate the correct amount of
memory for the array
• You need speed when iterating through all the elements in sequence.
• Memory is a concern. Filled arrays take up less memory than linked lists. Each element in the array is just the
data. Each linked list node requires the data as well as one (or more) pointers to the other elements in the linked
list.

Binary Tree’s
• Used in many search applications where data is constantly entering/leaving, such as the map and set objects in
many languages’ libraries.

Page 12 of 12

You might also like