02 Fundamentals of Data Structures Flashcards

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

Describe queues.

A

A queue is an abstract data structure with a first-in-first-out policy.
It maintains a front pointer for the first element in the queue and a rear pointer for the last item in the queue.
Items are enqueued to the position after the rear pointer and it increments and dequeued from the front pointer and front pointer increments.

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

Describe stacks and applications of them.

A

Stack is an abstract data structure which follows a last-in-first-out policy.

  • Stacks are also used to facilitate recursive subroutines where the state of each call is stored in a stack frame and placed on a stack.
  • can be used to maintain a list of operations for an ‘undo’ function in a piece of software, where the most recent operation is the first to be undone
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe graphs.

A

A graph is an abstract data type that can be used to represent complex, non-linear relationships between objects.

  • Consists of nodes and edges
  • Neighbours are two nodes connected
  • A loop is an edge that connects a node to itself
  • Path is a sequence of nodes connected by edges
  • A cycle is a path that starts and ends at the same node and doesn’t visit any node more than once

Uses: internet (nodes are routers and edge is hop), social networks, transport networks, WWW (nodes are webpages)

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

Describe trees.

A

A tree is a connected, undirected graph with no cycles.

  • The root is the start node for traversals
  • Branch is a path from the root to a leaf (endpoint)
  • Height is the number of nodes from root to furthest away leaf (longest branch)
  • Root has no parent and leaf nodes have no children
  • Binary tree is a tree where each node has no more than 2 children
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe hash tables.

A
  • A hash table is a data structure that implements an associative array (a dictionary).
  • In an associative array, data is stored as a collection of key-value pairs.
  • The position of the data within the array is determined by applying a hashing algorithm to the key - a process called hashing.
  • The hashing algorithm is called a hash function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe dictionaries.

A

A dictionary is an abstract data type that defines an unordered collection of data as a set of key-value pairs.

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

Describe vectors.

A

A vector is an abstract data type used to represent properties that have both direction and magnitude.

  • Dot product is the sum of the products of each item in both vectors
  • cos x = (a DOTPRODUCT b)/ (|a|x|b|)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between static and dynamic data structures?

A

STATIC

  • Reserve memory for a set amount of data in memory
  • Fixed in size and only one data type
  • Inefficient for memory use
  • Efficient for searching through structure

DYNAMIC

  • Memory capacity allocated is not fixed
  • Efficient memory use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Compare uses and benefits of static and dynamic data structures.

A

STATIC

  • ADV 1: The memory allocation is fixed and so there will be no problem with adding and removing data items.
  • ADV 2: Easier to program as there is no need to check on data structure size at any point.
  • DIS: Can be very inefficient as the memory for the data structure has been set aside regardless of whether it is needed or not

DYNAMIC

  • ADV 1: Makes the most efficient use of memory as the data structure only uses as much memory as it needs
  • DIS 1: Because the memory allocation is dynamic, it is possible for the structure to ‘overflow’ should it exceed its allowed limit. It can also ‘underflow’ should it become empty.
  • DIS 2: Harder to program as the software needs to keep track of its size and data item locations at all times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Outline linear queues

A
  • A linear queue is where items are removed from the front of queue and items are added to the rear of the queue.
  • As items are removed from the front of the queue the front of the queue will move further and further back.
  • If we simply add to the rear of the queue and remove from the front in a linear fashion we would eventually reach the maximum limit of the queue and have no more space to add items.
  • Also, there will be wasted empty space at the front of the queue.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Outline circular queues.

A
  • When either pointer reaches queuemax it will next move to position 1.
  • A circular queue reuses the empty slots at the front of the array that are caused when elements are removed from the queue.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe the steps involved when adding an
item to a priority queue, implemented as a
static data structure using an array,

A
  • Starting with the item at the rear of the queue move each item back one place in the array until you (reach the start of the queue or) find an item with the same or higher priority than the item to add
  • Add new item in the position before that item
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the steps involved in adding an item
to a linear queue that has been implemented as
a static data structure using an array.

A
  • Check that the queue is not already full;
  • (if it isn’t) then add 1 to the value of the rear pointer;
  • Then add the new item to the position indicated by the rear pointer;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Discuss the advantages and disadvantages of
dynamic data structures compared to static
data structures. [4 marks]

A

Advantages

  • No wasted memory;
  • Can grow as more data is added to the data structure // no limit on number of
    items that can be added (except due to hardware limitations);
  • Resources only allocated as they are needed (with static data structures they are
    allocated at creation even if not needed until later);

Disadvantages

  • Additional memory needed for pointers;
  • Can result in memory leak (if memory that is no longer needed is not returned to
    the heap);
  • Can take longer to access an item directly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Principles of operation of queues and applications.

A
  • enqueue(data)
  • dequeue()
  • is_empty()
  • is_full()
  • print tasks are stored on a print queue while waiting to be printed
  • simulation for a proposed road scheme could model the behaviour of traffic queueing at junctions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the creation and maintenance of stacks.

A
  • Push() adds to stack
  • Pop () removes item from top of stack
  • Peek () checks value at top of stack without removing
  • Stack pointer points to the top of the stack starting at zero
17
Q

Describe the creation and maintenance of hash tables.

A
  • A collision occurs when the hash function produces the same hash value for two or more keys.
  • Collisions are resolved by inserting the value in the next available space in the hash table
  • To insert value, hashing function is applied and value stored where the index is the hash
  • To retrieve a value, hashing function is applied and table finds index with that hash and gets value