Set 6 Flashcards
What is a static data structure?
A data structure that reserves a fixed amount of memory, specified by the programmer in advance of its use
What is a dynamic data structure?
Data structures that have no fixed size. The memory used grows and shrinks as items are added/removed
Give two advantages of static data structures over dynamic data structures:
- Data is quicker to access directly, with minimal overhead
- No additional memory is needed to store all of the pointers
Give two advantages of dynamic data structures:
- There is no wasted memory
- There is no theoretical limit on the number of items that can be added
Is a queue FIFO or LIFO?
FIFO (First in first out)
Give the four key operations of a queue:
- Enqueue - adds item to rear of the queue
- Dequeue - removes item and returns item from the front of the queue
- IsEmpty - checks if the queue is empty
- IsFull - checks is the queue is full
Describe briefly the data structures and pointers used by a linear queue:
- Linear queues are implemented with arrays (or lists)
- They use a front and rear pointers to point to:
- front → the next item to dequeue
- rear → last item enqueued
Disadvantages of linear queues if you
1. Don’t shuffle down the items
2. If you do
- There is a limit on the number of items that can be added and removed (maxSize)
- Implementing a linear queue where you “shuffle” the items down each time an item is dequeued is very processing intensive
What is the key difference between a circular queue and a linear queue?
- Circular queues virtually connect the end to the start of the array
- This overcomes the problem of reusing spaces in the array
What happens to the rear
pointer when enqueuing an item to a circular queue?
rear ← (rear + 1) % maxSize
What is a priority queue? How does enqueuing work?
- A queue where each element in the queue has a priority
- When new elements are enqueued, they are inserted ahead of those of lower priority and behind elements of equal or greater priority
Name an algorithm that can be implemented with a priority queue
Dijkstra’s Algorithm
Is a graph static or dynamic?
Dynamic, they can grow and shrink in size
What is a graph?
Graphs are sets of vertices (nodes) and the edges (arcs) that connect them
What is a graph with a high ratio of edges to vertices called?
Dense
What is a graph with a low ratio of edges to vertices called?
Sparse
What is a weighted graph?
A graph that has weights (number values) on each edge
What is a connected graph?
A graph where there is a path between each pair of vertices
Suggest three things that graphs could be used to model
- Social networking: the nodes could be individual people. An edge could represent that two people are acquaintances.
- Transport networks: the nodes could be towns. An edge could represent that a train line connects two towns.
- The internet: the nodes could be routers. An edge could represent that two routers are connected.
Give two ways to represent graphs:
Adjacency matrix and adjacency list
How do you represent no edge for a weighted graph?
“-” or “∞” (can’t use zero)
How could an adjacency list be implemented if graph is 1. weighted 2. unweighted?
- List of dictionaries if weighted
- List of lists if unweighted
Give two advantages of adjacency lists:
- Adjacency lists are very space efficient, as no memory is needed to store the empty spaces
- It is easy to add / delete nodes
What is the disadvantage of an adjacency list:
Slow to query (e.g. to check the presence of an edge), as each item in the list must be searched sequentially until the desired edge is found
Give two disadvantages of adjacency matrices:
- If the graph is sparse, then there are lots of empty spaces, which is wasted memory
- It can be hard to add / delete nodes if a 2D static array is used
What is a tree?
A connected, undirected graph with no cycles
What is a rooted tree?
- A tree in which one vertex has been designated as the root
- Rooted trees have parent-child relationships between adjacent nodes
- The root is the only node with no parent. All other nodes are descendants of the root
What is an internal node?
A node in a rooted tree which has a parent and at least one child
What is a leaf node?
A node in a rooted tree that has a parent but is not the parent of other nodes (no child nodes)
Suggest a use for rooted trees
A game tree, where edges represent moves, and nodes represent all possible positions in the game
What is a binary tree?
A rooted tree in which each node has at most two child nodes
Name a common application of binary trees
Binary search trees
What is an ordered binary tree?
- A type of binary tree where the data items are in a particular order
- Items left of a node have a value less than the node
- Items right of a node have a value greater than the node
What is a hash table?
A data structure that creates a mapping between keys and values
What is the purpose of a hash table?
- To provide a way of storing a list of values in which a key is used to locate values in the list without searching through all other values
- They are used so that records can be retrieved quickly
What does a hash function/algorithm do in a hash table?
Computes the array index for the value to be stored or retrieved
What is hashing?
Hashing is the irreversible process of converting data of arbitrary length into data of a fixed length.
When does a collision occur (hashing)?
When two or more keys hash to the same index
When are collisions unavoidable (hashing)?
When the number of possible keys is larger than the number of available spaces
What is a dictionary?
- A collection of key-value pairs in which the value is accessed via the associated key
- There can be no duplicate keys
Name an application of dictionaries
Dictionary based compression
What is the meaning of the symbol ↦ ?
Maps to
Describe the steps involved in rehashing
- A larger hash table is created
- Each value in the existing table is inserted into the new table in a position determined by a new hashing algorithm
Describe the steps involved in rehashing
- A larger hash table is created
- Each value in the existing table is inserted into the new table in a position determined by a new hashing algorithm
Describe the steps involved in adding a record to a hash table
- Hash algorithm applied to key, which returns the location (often an array index) in table where the record should be stored
- If location is not empty, a collision has occurred
– Open addressing is used (the next free location is used, wrapping round at the end of the array)
– Or rehashing occurs (starting from scratch with a larger array)
– Or chaining is used (a pointer to a list is stored at each location a collision has occurred)
Name three methods of collision handling (for a hash table)
- Rehashing
- Chaining
- Open addressing
How are collisions resolved with chaining?
A pointer to a list is stored at each location that points to a list of items that have collided at that location
How are collisions resolved with open addressing?
The item is stored in the next available location in the hash table (wrapping around at the end)
Example of where parity bits are typically used
In the transmission of 7-bit standard ASCII codes
What is a downside of majority voting?
If you choose a large odd number of repetitions, there is lots of redundant information that needs to be transmitted across the channel
What is a checksum?
- A checksum is a piece of data that is added to a block of data to enable error detection.
- It is produced by applying a checksum algorithm (often MOD to limit magnitude of checksum).
- The receiver recalculates the checksum and if it doesn’t match the data’s checksum then there is an error in the data
What is a check digit?
- An extra digit that is placed at the beginning or end of a number and is used to identify a product or verify a user
- It allows for error detection
- They are produced by algorithms that often use modulo arithmetic
Name an application of breadth-first search
Finding the shortest path for an unweighted graph
Name an application of depth-first search
Navigating a maze
Give two differences between depth-first traversal and breadth-first traversal:
DF: Chooses to explore the most recent node to be discovered
BF: Chooses to explore the least recent node to be discovered
DF: Implemented using a stack (when implemented iteratively)
BF: Implemented using a queue (when implemented iteratively)
What is tree traversal?
Visiting the nodes of a tree in a specific order
Use of pre-order traversal
Copying a tree
Give two uses of in-order traversal:
- Outputting the contents of a binary search tree in ascending order
- Producing infix expression from an expression tree
Give two uses of post-order traversal:
- Infix to RPN conversions
- Producing a postfix (RPN) expression from an expression tree
Describe pre-order traversal operation
- Visit the node
- Traverse the left hand sub tree
- Traverse the right hand sub tree
Describe in-order traversal operation
- Traverse the left hand sub tree
- Visit the node
- Traverse the right hand sub tree
Describe post-order traversal operation
- Traverse the left hand sub tree
- Traverse the right hand sub tree
- Visit the node