4 - Data Structures

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/48

49 Terms

1
New cards

Characteristics of an array

  • Same data type

  • Numerical index

  • Traditionally fixed in size

  • Duplicates


2
New cards

Characteristics of a list

  • Dynamic size

  • Zero-based index

  • Mixed data types

  • Duplicates


3
New cards

Characteristics of a tuple

  • Fixed size - a new tuple must be made for new data

  • Mixed data types

  • Duplicates


4
New cards

Characteristics of a record

  • Groups related data items

  • Fields have meaningful names

  • Different data types

  • Represents an individual object


5
New cards

What are static data structures?

Collections of data that are fixed in size

6
New cards

What are dynamic data structures?

Collections of data that can grow/shrink in size

  • Unused memory is re-allocated as required


7
New cards

Adv/Disadv of static data structures

Adv: No issue when items are removed.

Disadv: Inefficient since memory may be left unused


8
New cards

Adv/Disadv of dynamic data structures

Adv: Memory is only used when it is needed

Disadv: Risk of overflow/underflow, deemed harder to program

9
New cards

Definition of mutable

The structure and data can be changed at runtime (after its creation)

10
New cards

Overflow and underflow definition

Overflow: When an item is pushed onto a full structure

Underflow: When an item is removed from an empty structure

11
New cards

Stack definition

A data structure with items popped on and off with a LIFO structure

12
New cards

Queue definition

A data structure with items enqueued and dequeued with a FIFO structure

13
New cards

How can we implement a stack with an array?

  • The stack is initialised as an empty array, with variables for the pointer and max capacity saved.

  • As an item is pushed, the pointer increments by 1

  • As an item is popped, the pointer decrements by 1

  • If an item is peeked at, the top item is returned

  • Underflow and overflow is checked for before pushing/popping to prevent error


14
New cards

How can we implement a queue with an array?

  • The stack is initialised as an empty array, with variables for the head pointer, tail pointer and max capacity saved.

  • As an item is enqueued, the tail pointer increments by 1

  • As an item is dequeued, the head pointer increments by 1

  • If an item is peeked at, the top item is returned

  • Underflow and overflow is checked for before pushing/popping to prevent error


15
New cards

What are the problems caused by implementing a queue via an array?

Both the pointers travel in the same direction, which can cause the queue to quickly run out of space.

Solution: Circular Queue — cycling the tail pointer to the front if it reaches the end.

16
New cards

Uses of stacks

  • Depth-first searches

  • Evaluating maths expressions (e.g. RPN or Shunting Yard)


17
New cards

Uses of queues

  • Breadth-first searches

  • Process scheduling (e.g. RR uses a circular queue)


18
New cards

How can you traverse through a stack/queue?

Repeatedly pop/dequeue items and output their value

19
New cards

What is Reverse Polish Notation? How can we convert it to infix notation with a stack?

Formatting a mathematical expression with the operators after the operands.

  • Push digits onto the stack until an operator is reached

  • Push the operator onto the stack and conduct the operation with the two previous digits

  • Repeat till you have one number remaining


20
New cards

What is the Shunting Yard Algorithm?

An algorithm used to convert infix notation to RPN.

  • If the value is a digit, push onto the output stack

  • If an operator, push onto the operator stack.

    • However if this has less precedence than the operator below it, push all operators below onto the output stack

    • Furthermore, if the operator is a closed bracket, pop the entire operator stack onto the output stack (without brackets)

  • Once the end of the expression is reached, pop the entire operator stack and push onto the output stack.


21
New cards

Graph definition

A structure with nodes and edges

22
New cards

Adjacency matrix definition

A matrix storing adjacency between nodes, where the columns represent nodes in the data structure. Typically implemented as dictionaries.

23
New cards

Cost definition

A value given to an edge which represents its relationship with the vertices

24
New cards

Implementation of an adjacency matrix in weighted graphs

A: {B:8, C:2, D:10}

25
New cards

Implementation of an adjacency matrix in unweighted graphs

A: {B, C, D}

26
New cards

What are the different types of traversals on a graph?

Inorder: Reads left, root, right

Preorder: Reads root, left, right

Postorder: Reads left, right, root

Way to remember: pre, in, or post references the roots position in the reading

27
New cards

Tree definition

A type of graph that is undirected, connected, and acyclic.

  • This means that there is always a findable path


28
New cards

Binary tree definition

A tree with 0-2 edges per node, with each connecting to a different child node.

29
New cards

How do we present binary trees as arrays?

knowt flashcard image
30
New cards

How do we present binary trees as objects?

knowt flashcard image
31
New cards

Dictionary definition and its characteristics

A general-purpose abstract data structure used for storing objects

Characteristics:

  • Has a set of keys

  • Each key holds a value

  • When the key is supplied, the value is returned


<p>A general-purpose abstract data structure used for storing objects</p><p>Characteristics:</p><ul><li><p>Has a set of keys</p></li><li><p>Each key holds a value</p></li><li><p>When the key is supplied, the value is returned</p></li></ul><p></p>
32
New cards

Uses of dictionaries

Storing/Retrieving data

Implementing other data structures

Containing categorised data

Mapping data

33
New cards

Operations on a dictionary

  1. Creation

  2. Adding key-value pair

  3. Removing key-value pair

  4. Appending key-value pair

  5. Returning T/F if a key exists

  6. Returning a value from a given key

  7. Returning length of the dictionary


34
New cards

What is a hash table?

A data structure that stores key-value pairs. Used to immediately find an item in an (un)sorted list with no need for comparisons

  • Typically large to account for collisions between values

  • This can cause clustering around common collision points


35
New cards

What is a hashing function?

A function that calculates an items position.

36
New cards

3 solutions for collisions

  • Open addressing

  • Chaining

  • Overflow table


37
New cards

What is open addressing?

Collision values repeatedly check the next space until it is empty.

38
New cards

What is linear probing?

The process of using linear search to find key-value pairs.

39
New cards

What is chaining?

Using a 2D hash table, which lets multiple items be stored in the same position.

40
New cards

How are items removed/added?

  • Calculate its hash value, and find its corresponding position.

  • Either add it, or check if its the value we’re looking for.

    • If so, retrieve and delete it.

    • Else, search via linear probing.


41
New cards

Vector definition

A series of numbers used to represent spatial information

42
New cards

How can we represent vectors?

knowt flashcard image
43
New cards

What is vector addition?

Combining vectors to make a resultant vector.

44
New cards

What is scalar vector multiplication?

Multiplying vectors by scalar values to create new vectors.

45
New cards

What is the convex combination of 2 vectors? How is it represented?

The area between two vectors on a graph. Represented by αu + βv where:

  • u and v are vectors

  • α + β = 1

  • α and β are bigger than/greater to 0


46
New cards

How is convex combination used?

To calculate fields of view in a game.

(Any value supplied as u and v give a vector on the hypotenuse between the two vectors)

47
New cards

What is the dot/scalar product?

Multiplying each matching component of the vector together, then adding the results.

48
New cards

How can we use the dot/scalar product?

To find the angle between two vectors, where |n| is the magnitude.

<p>To find the angle between two vectors, where |n| is the magnitude.</p>
49
New cards