1.1 Data structures Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a data structure

A

a collection or group of related data items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

why are data structures useful

A

collects together basic data types (e.g. integer, real etc.) and allows data to be organised in a convenient, logical manner related to the program being developed.

can be accessed, searched, sorted etc. more quickly than data held in a file as data structures are held in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

name 2 types of data structure

A

arrays, records

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

give an example of data to be stored in a 2 dimensional array

A

the sales of different products (downwards column) per month (across column)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

give an example of data stored in a 3d array

A

the test scores of different students, per form group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is a record data structure and where would it be used?

A

The record data structure allows items of data relating to something to be grouped together into a single record type.

cam be used in e.g. pseudocode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a stack data structure

A

a stack is a LIFO data structure

is a limited access data structure

is used as e.g.

run time stack mechanism (keep track of where it is up to with procedure calls)

undo feature in an application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

explain adding an item to a stack

A

increment the stackpointer (add 1 to) and place the new item in the position indicated by the stackpointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

explain removing an item from a stack

A

display the item indicated by stack pointer (e.g. pressing undo in a web browser displays the previous page) and the decrement (take 1 from) stackpointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how could a stack be implemented using an array and variable

A

the array stores the items in the stack, the variable (e.g. the stack pointer) always points at the last element added to the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

pseudocode procedure to add data to a stack

A

Procedure AddToStack
Get Item
IF Stack pointer = limit
Error message “stack full”
ELSE
Stackpointer := Stackpointer + 1
Stack[Stackpointer] := Item
END IF
End Procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

pseudocode procedure to remove data from stack

A

Procedure RemoveFromStack
IF Stack pointer = 0
Error “Stack empty”
ELSE
Output stack[Stackpointer]
Stack pointer := Stackpointer –1
END IF
END Proc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is a queue data structure and how is it implemented

A

a FIFO data structure (First In First Out)

e.g. queuing up in a shop, processes in a ‘round-robin scheduling policy’

often implemented using an array and two pointers to indicate the start and the end of a queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

explain removing an item from a queue data structure

A

involves displaying the data in position indicated by start pointer (front of queue) and then incrementing (+1) the start pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

explain adding an item to a queue data structure

A

involves incrementing (+1) the end pointer (back of queue) and putting data into that position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what happens when the end pointer goes beyond the end

A

the queue is circular so if the end pointer goes beyond the end of the queue, it starts again at 1.

17
Q

draw a diagram to represent the stack data structure, add an item to it, and remove an item

A
18
Q

draw a diagram to represent the queue data structure, add an item to it, and remove an item

A
19
Q

pseudocode algorithm to add an item to a queue data structure

A

Procedure AddtoQueue
If Endpointer < MaxSize then
Queue[Endpointer]= newitem
Endpointer = Endpointer + 1
Else
Output “queue is full”
End if
End Procedure

20
Q

pseudocode algorithm to remove item from queue data structure

A

Procedure RemoveFromQueue
If startpointer <> Endpointer then
Output Queue[startpointer]
Queue[startpointer] = null
startpointer = startpointer + 1
else
output “queue is empty”
end if
end procedure

21
Q

What is a linked list and what does it consist of

A

a linked list is a dynamic data structure (its size can be increased and decreased as required, unlike an array)

items can be added to a linked list without having to move the existing items to make space

items can be removed from a linked list without having to shift the other items to fill the gap left

consists of:
start pointer which points to the first item in the list, which will have a pointer to the next item and so on.

there is also another linked list of free locations, items that are deleted from the normal linked list are added onto the front of this free list. Items added to the linked list are obtained from the front of the free list.

22
Q

advantages of a linked list

A

A linked list is a dynamic data structure: its size can be increased and decreased as required. An array is static and therefore its size is fixed.

Items can be added into a linked list without having to move the existing items to make space.

Items can be removed from a linked list without having to shift the other items to fill in the gap left

23
Q

disadvantages of a linked list

A

more complex to program than an array

extra programming required to access the data in the opposite direction

can only be accessed in a linear manner

24
Q

what is a binary tree data structure and what could it be used for

A

a type of tree where all nodes can either have 0, 1 or 2 children, each node having a left pointer and right pointer to their children (which could be empty/null)

we could use binary trees to keep an ordered list for searching using the rule that:
-anything ‘less than’ or before the data in the alphabet is to the left

-anything ‘greater than’ or after the data is to the right

25
Q

how would a new item be added in an ordered binary tree

A

Starting at the root node of the tree;

Compare the data you want to add to the current node;

If the new data is less than the node then follow the left pointer else follow the right pointer;

Repeat this until you reach the end of the tree;

Update the pointers of the last node to point to the new one.

26
Q

redraw a tree with a specified node deleted

A
27
Q

removing data from a binary tree

A

Removing data from a binary tree is a little trickier compared to adding an item. The steps you will follow will depend on the type of node being deleted.

There are 3 possible cases:
A node has no children - remove the node and the pointer which pointers to it

A node with 1 child - remove the node and its pointers, the node it was connected to has a pointer to the node that the one that was removed points to

A node with 2 children (find min in right node) - find the value of the smallest node to the right pointer of the one to be removed. You then replace that node with the one to be removed and remove the small node with the pointer that points to it.

28
Q

the three steps in pre-order traversal

A

the root node is visited first

then the left subtree

then the right subtree

29
Q

in-order traversal

A

left subtree visited first

then the root node

then the right subtree

30
Q

post-order traversal

A

left subtree visited first

then the right subtree

then the root node

31
Q

advantages of a binary tree

A

if the tree is balanced (same number of items on each side) then it can be quick to find data in a tree, this is because the number of items being searched is effectively halved each level down the tree

may be more idk

32
Q

disadvantages of a binary tree

A

find some

33
Q

draw a representation of an unbalanced binary tree and what is the side effect

A

all go down the same pointer

side effect is that it will slow the search down

34
Q

what is a hash table and what does it consist of

A

has 2 components:
table where the data is stored
a mapping function (e.g. hashing algorithm)

The hashing algorithm uses data that is going to be stored to generate the location in the table where it will be stored.

35
Q

What is separate chaining in hash tables

A

e.g. if the hashing algorithm produces the same index value for 2 different pieces of data, a collision occurs and a linked list is created to hold both sets of data.

However, it will slow the process of finding data again down, as once the hash location has been computed, the linked list will need to be searched aswell.

36
Q

in hash tables, why should we ensure that there is an evenly distributed spread of data

A

collisions could occur otherwise

37
Q

What would be the pseudocode to display all of the items in a linked list?

A

Procedure DisplayAll
p = start
While p <> -1
Display data(p)
p = next (p)
End While
End procedure