2.14 Data Structures Flashcards

1
Q

Arrays

A

Arrays: Ordered, finite set of elements of a single type

1D Array: Linear array, always taken to be zero-indexed (first element in the array is considered to be at position 0)
- E.g oneDimensionalArray = [1, 23, 12, 14, 16, 29, 12] // creates a 1D array

2D Array: Visualised as table or spreadsheet. When searching through 2D array, first go down then across the columns to find given position
- E.g twoDimensionalArray = [[123, 28, 90, 38, 88, 23, 47],[1, 23, 12, 14, 16, 29, 12]]
- print(twoDimensionalArray[1,3]) // Goes down then across, returns 14

3D Array: Visualised as multi-page spreadsheet, thought of as multiple 2D arrays
- Selecting element in a 3D array: threeDimensionalArray[z,y,x]
- z = array no., y = row no., x = column no.
- threeDimensionalArray = [[[12,8],[9,6,19]],[[241,89,4,1],[19,2]]]
- print(threeDimensionalArray[0,1,2]) // Returns 19

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

Records

A

Record: Commonly referred to as row in a file, is made up of fields. Records are used in databases, as shown in the table below:
| ID | 1Name | 2Name |
| 002 | Tyson | Fury |
| 003 | Deonte | Wilder |

Above is file containing 3 records, each record has three fields
- Declaring a Record:
*fighterDataType = record
integer ID
string FirstName
string Surname
end record
*
- Each field in the record can be identified by recordName.fieldName
- Creating a Record:
fighter : fighterDataType
- To access attributes:
fighter.FirstName

001 | Antony | Joshua |

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

Lists

A

List: Data structure w/ no. of ordered items, items can occur more than once
- Lists similar to 1D arrays, elements accessed in the
same way
- The difference is that list values are stored non contiguously (do not have to be stored next to each other in memory, as data in arrays is stored)
- Lists can also contain elements of more than one data type, unlike arrays.

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

Tuples

A

Tuples: Ordered set of values of any type. Is immutable, cannot be changed, elements cannot be added or removed once it has been created

  • tupleExample = (“Value1”, 2, “Value3”)
  • Elements in a tuple are accessed in a similar way to elements in an array, with the exception that values cannot be changed or removed. Attempting to do so will result in a syntax error

print(tupleExample[0])
» Value1 :
tupleExample[0] = “ChangedValue”
» Syntax Error

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

Linked Lists

A

Linked List: Dynamic data structure used to hold an ordered sequence. Items do not have to be contiguous
- Each item is called a node, contains data field w/ another address called pointer field

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

Graphs

A

Graph: Set of vertices/nodes connected by edges/arcs

  • Directed Graph: Edges only traversed in one direction
  • Undirected Graph: Edges traversed in both directions
  • Weighted Graph: Cost attached to each edge
  • Computers able to process graphs using an adjacency matrix or adjacency list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Adjacency Matrix

A

O|A|B|C
A|
B|
C|

  • Convenient to work with due to quicker access times
  • Easy to add nodes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ajacency List

A

A → {B:4, C:18, D:12}
B → {A:4, C:5, E:8}
C → {A:18, B:5, D:5}

-More space efficient for large, sparse networks

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

Stacks

A
  • Stack: Last In First Out (LIFO) data structure
  • Items can only be added to or removed
    from top of stack, used to reverse an action, (E.g ‘undo’ buttons)
  • Either static or dynamic
  • Maximum size known in advance = static, easier to implement & make more efficient use of memory
  • Implemented using a pointer, points to top of stack, where next piece of data is inserted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Manipulating A Stack

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

Queues

A

Queue: First In First Out (FIFO) data structure; items added to end of queue & removed from the front of queue
- Used in printers, keyboards & simulators

  • Linear Queue: Data structure consisting of an array
    • Items are added into the next available space in the queue, starting from the front
    • Items are removed from the front of
      the queue
    • Queues make use of two pointers: one pointing to the front of the queue & one pointing to the back of the queue, where the next item can be added
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Manipulating A Queue

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

Trees

A

Tree: Connected form of a graph. Consist of root node connected to other nodes using branches

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

Binary Tree

A

Binary Tree: Tree in which each node has a maximum of 2 children
- Used to represent information for binary searches, as information in these trees is easy
to search through
- Represented by storing each node with a left & right pointer within a 2D array

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

Pre Order Traversal

A

Pre-order Traversal: Follows the order: root node, left subtree, then right subtree
- (Basically Depth First)

The order of traversal is: 15, 9, 5, 7, 11, 10, 12, 20, 25, 34
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In Order Traversal

A

In-order Traversal: follows the order: left subtree, root node, right subtree. Traverses nodes in sequential order

Order: 5, 7, 9, 10, 11, 12, 15, 20, 25, 34
17
Q

Post Order Traversal

A

Post order Traversal: Follows the order: left subtree, right subtree, root node
- Nodes are traversed in the order in which you pass them on the right

Order: 7, 5, 10, 12, 11, 9, 34, 25, 20, 15
18
Q

Hash Tables

A

Hash Table: Array coupled with hash function
- Hash Function: Takes in data (a key) & releases an output (the hash)
- The role of the hash function is to map key to an index in the hash table.
- Each piece of data is mapped to a unique value using the hash function
- However, possible for 2 inputs to result in same hashed value, known as collision
- A good hashing algorithm has low probability of collisions occurring but if it does occur, item is typically placed in the next available location. This is called collision resolution
- Hash tables used for indexing, as they provide fast access to data due to keys having a unique, one-to-one relationship with the address at which they are stored