Assignment 05 w24
Assignment 05 w24
Outline: in this assignment you will implement both the Map and the Unordered Map abstract data types as C++ classes,
using a self-balancing BST and a hash table respectively as the underlying data structures. You will then use your maps to
solve some well-known programming problems (part 3).
Part 1: Map ADT (40%) – maps are associative containers that store elements formed by a combination of key type and
mapped type. The keys are used to sort and uniquely identify the elements, while the values store the content associated to
the key. The map has the following properties:
1. Associative – elements in the container are referenced by their key and not by their absolute position in the
container.
2. Ordered – elements in the container follow a strict order at all times.
3. Unique keys – no two elements in the container can have equivalent keys.
Implement a map ADT using a self-balancing BST (AVL or red-black tree) as the underlying data structure. In addition to the big
5, it must support the following operations: (where value_type is a pair<const key_type, mapped_type>)
Must also provide an iterator inner class that provides the following methods with the expected behaviour:
• begin
• end
• ++ (increment)
• * (dereference)
Your iterator must support an infix traversal of the tree (it should print out a sorted list when used in a range-based ‘for’ loop).
Page 176 (section 4.8.3) of the Weiss textbook gives some hints on how to go about creating a BST iterator.
See the C++ std::map reference: https://cplusplus.com/reference/map/map/ for further details and usage examples.
Part 2: Unordered Map (40%) – similar to maps, unordered maps are associative containers that store key-value pairs.
However, the unordered map is implemented using a hash table instead of a tree, which allows for 𝑂(1) find and remove. The
drawback is that the elements are no longer ordered, so it does not support findMin or findMax, and cannot produce a sorted
output using iterator traversal.
Implement an unordered map ADT using a hash table (separate chaining or double hashing) as the underlying data structure.
You may use std libraries for the linked list (separate chaining) and hash functions. If the hash table becomes overloaded (𝜆 >
0.5) it should allocate more space and rehash all the existing elements. Don’t forget the big 5!
As with the map, unordered map must provide an iterator. However, the unordered map iterator need not print the elements in
order, it can simply traverse elements of the hash table (skipping empty spots).
Use your unordered map to solve the following leetcodes, provide your solution code:
• two sum,
• longest substring without repeating characters
• substring with concatenation of all words