Unit 7 - ADTs Flashcards
Elementry Data Types Examples
Boolean, char, integer, real
Composite Data Types
Array, String
Abstract Data Types examples
Stacks, Queues, Dictionaries
Abstract Data Types (definition)
A logical description of how we view data and possible operations
Static Data Types Definition
Fixed in size, cannot change size once program is running
Dynamic Data types defininition
Can grow/shrink in size once program is running as it uses the heap
The heap (memory)
A portion of memory which space is automatically allocated/ deallocated to as required
Dynamic Data Structure Uses
Queues, as maximum size is not known before implementation
Record Structure
Consists of a fixed number of variables called fields, each field can have a different data type
Records in C#
Must be defined using a dictionary or using a class
Tuples
Tuples are mutable data structures –> declared like a list
Array Limitations
Size must be declared when created, cannot grow in size, one data type (in some languages including C#)
Array Benefits
Can be any dimension, uses 0 based indexing, values are mutable
Mutable Definition
Values can be changed whilst the programming is running
enqueue(data)
Adds an element to the queue
dequeue()
Removes (and returns) element from the queue
peek()
returns element from queue without removing it from queue
is_empty()
Checks if queue is empty
is_full()
Check if queue is full (if it is static)
What is a Queue
It is a mutable fifo data structure, meaning that items are added at the back and removed from the front
Why circular queues exist?
When an item is dequeued, a space of memory is left however this is left at the front of the queues and data in a queue is only added at the back, this leaves memory being wasted and queues filling up fast, circular queues solve this problem
How do circular queues work?
Let the queue be defined as an array. If the end is pointer is at the end of the array, however the start pointer is not at the front, a circular queue means that the next item to be enqueued will be added to the first slot on the array. The pointer for the end also points to here, this means that whilst the array is not full, data can keep being enqueued and it will go around in circles
Application of Queues
Printer Queues, the thing that was printed first will be the first to be printed.
Simulations of actual queues eg in a supermarket
How do priority queues work?
Each data is given a priority, the ones with the higher priorities are nearer to the front of the queue which means they are more likely to get dequeued sooner. When data is added it is added as the last one with that priority making it possible for it to be added at the front or in the middle
Why use a list instead of an array?
Lists are dynamic, arrays are static.
Similarity between Lists and Arrays
Both use zero based indexing,
Name of elements in linked list
Node
What must each node in a linked list store?
The data relating to that element, and a pointer, pointing to the next node
Advantages of linked lists
Dynamic, contiguous memory locactions are not required, can be used to implememtn stacks and queues, easy to insert data
Disadvantages of linked lists
Random access is not allowed so you have to sequencially access each element starting from the first one, extra memory space is required for each pointer, reverse traversing is difficult (unless a second pointer is used)
How to add data into a linked list? (simple)
Where the data is wanting to be added, change the pointer of the one before to point to that one and make the pointer of that element point to the next one.
How do you traverse a linked list?
Starting at the head, follow the pointers of each node until you reach a pointer pointing to null which means that you have reached the end of the list.
How hard is deleting data in a linked list?
Very easy as you just have to change the pointer in the node before it.
What is a doubly linked list?
A linked list with pointers pointing both forward and backwards.
What are graphs used to represent?
Complex, non-linear relashonships between objects.
What is the degree of a node on a graph?
The number of other nodes it is connected to. (the number of neighbours)
What is are neighbours on a graph?
Two nodes that are connected by an edge
What is a loop on a graph?
It is an edge that connects a node to itself.
What is a path on a graph?
A sequence of nodes that are connected by edges.
What is a cycle on a graph?
A cycle is a closed path, a path that starts and ends at the same node. However no iterim node can be visisted more than once.
Examples of what graphs can represent.
Social networks (whos friends with who etc), transport networks (the London Underground), the internet (each router is a node), the world wide web (each node is a webpage, edges show which ones are linked)
What is an undirected graph?
A graph which means that you can travel both to and from each node.
What is a directed graph?
Each edge of the graph has a direction (showing that you can only move in the specified direction between nodes).
What is a weighted graph?
On each edge of a weighted graph, there is a value. This can be used to represent many thing eg the distance between towns etc. Weights enable you to find the shortest paths.
Ways to implement a graph in code:
Adjacency matrix or adjacency list
What is an adjaceny matrix?
An adjaceny matrix is a two dimensional array where each node is represented by the index. If the data stored at [x,y] is not 0, eg in an unweighted graph it will be 1, in a weighted graph it will be the weight, there is an edge between x and y. If the graph is undirectionaly, the matrix will be simetrical.
What is an adjaceny list?
An adjacency list stores each node, and then what it is connected too. This could be implemented using a linked list or an array.
How are weights stored in an adjacency list?
Each list has both the node and the weight of that node eg. B links to D,15; H,40; F,46.
Pros of Adjacency Lists (compared to adjacency matrix)
They are space efficient for graphs with few edges and many nodes, it is easier to frequently add/ delete nodes from an adjacency list