Array – A contiguous store of data items, of a single data type that is zero-indexed.
It is a
static & mutable data structure.
List – A contiguous store of data items, of more than one data type that is zero-indexed. It
is dynamic & mutable data structure.
Tuples – Lists that can’t be changed once created. It is a static and immutable data
structure.
Record – A collection of related fields. A field is a variable, and each field in a record can
have a different data type.
Linked Lists:
A dynamic, non-contiguous data structure constructed from nodes & pointers.
By adding an extra pointer, nodes can point to the previous & next items (doubly linked list).
A circular linked list can be created by making the last node point to the first node.
Traversing a linked list: Implemented as array variables:
ptr = Start Output:
WHILE ptr != null >> Abigail
OUTPUT Data(ptr) >> Beatrix
ptr = Next(ptr) >> Chloe
END WHILE >> David
>> Harry
>> Francis
Removing items from a linked list:
ptr = Start Adding items to a linked list:
prevPtr = Start
Store new item at NextFree pointer
IF Data(ptr) = item to remove THEN
Identify new item’s place in list
Start = Next(ptr)
If new node should be placed before start
EXIT PROCEDURE
Set new item’s pointer to previous start value
END IF
Reset start to new item
WHILE ptr != null Else
IF Data(ptr) = item to remove THEN Temporarily store preceding item’s pointer
Next(prevPtr) = Next(ptr) Set preceding item’s pointer to the new item
EXIT PROCEDURE Set new item’s pointer to preceding item’s old pointer
END IF End if
prevPtr = ptr Increment NextFree pointer
ptr = Next(ptr)
END WHILE
Graphs:
A collection of vertices connected by edges:
Adjacency List Implementation (objects): Adjacency Matrix Implementation:
Traversing a graph:
Depth First Traversal: Following a path from a starting vertex until we’ve reached the last
vertex on that path. We then back track and follow the next path until we’ve reached the
last vertex again. We continue in this way until there are no more paths to explore.
Breadth First Traversal: Starts at the first vertex & visits all of the vertices as close to this
vertex as possible. We’re effectively moving through the graph layer by layer. First examining
the layers closer to the first vertex & then moving down to the layers furthest away from the
stating vertex.
Trees:
A connected graph with no cycles.
Binary Tree – A special type of tree in which each node has a maximum of two children.
Adding an item to a binary tree:
Starting at root node: if new node should be placed before the current node (it is less than),
follow left edge, else follow right edge. Repeat, comparing along each node.
Removing an item from a binary tree:
Deleting leaf Nodes: Simply delete the node and set our parent’s left/right pointer to null.
Deleting Node with 1 child: Make the parent of the node to delete, instead point to the 1
child that the node to delete had.
Deleting Node with 2 children: Take the minimum node in our right subtree and assign it to
the node we want to delete. This means we now just have to delete the leaf node.
Traversing a binary tree:
Pre-order – First visit the root, then traverse left subtree and then right subtree.
Post-order – First traverse left subtree, then right, then visit the root.
In-order – Traverse left subtree, then the root then right subtree.
Stacks:
A Last in First Out (LIFO) data structure. The last item pushed (added) onto the stack must
also be the first item popped (removed) off the stack.
Peek Operation – Returns the value at the top without deleting it.
Stack Pointer – Always points to the node at the top.
Adding (pushing) an item onto a stack:
Check for stack overflow (attempt to push an item onto a full stack).
Insert the new data item at the location pointed to by the stack pointer & increment the
stack pointer.
Removing (popping) an item off a stack:
Check for stack underflow (attempt to pop an item off an empty stack).
Copy data from position denoted by stack pointer and decrement stack pointer.
Queues:
A First in First Out (FIFO) data structure. Items are enqueued (added) at the back of the
queue & dequeued (removed) from the front of the queue.
Adding (enqueuing) an item to a Queue:
Check for queue overflow (attempt to enqueue an item in a full queue)
Insert the new data item at the location pointed to by the back/tail pointer & increment the
back/tail pointer.
Removing (dequeuing) an item from a stack:
Check for queue underflow (attempt to dequeue from an empty queue).
Take data from position denoted by front/head pointer & increment the front/head pointer.
Hash Tables:
An associative array (dictionary), that can be used to store key value pairs. It allows for very
fast retravel of data, often used in database indexing.
Hashing function/algorithm:
The calculation applied to the key that produces an index of where a value can be stored/
found in the hash table.
A perfect hash function gives you a unique index each time (minimize collisions) & is easy to
calculate.