PA3 Manual
PA3 Manual
ASSIGNMENT 3
HASHING, HEAPS, SORTING, TRIES
PLAGIARISM POLICY
The course policy about plagiarism is as follows:
1. Students must not share the actual program code with other students.
2. Students must be prepared to explain any program code they submit.
3. Students cannot copy code from the Internet.
4. Students must indicate any assistance they received.
5. All submissions are subject to automated plagiarism detection. Students are
strongly advised that any act of plagiarism will be reported to the Disciplinary
Committee.
SMART POINTERS
In order to implement this assignment, you must make use of smart pointers. Since certain
functions require specialized syntax, a tutorial code, SmartPointer.cpp, has been shared with
you. Within this tutorial file, you can explore the necessary syntax for the equivalent of the
methods you would have used in raw dynamic pointers, which will be necessary in certain
parts of this assignment.
PART 1: HASHING
TASK 1: HASHING FUNCTION
In this task you will be implementing a bitwise hash function as shown below. Write your
implementation in the HashFunction.cpp file which also provides the required definitions.
BITWISE HASH:
Initialize bitwise_hash = 0
REQUIRED FUNCTIONS:
● Takes a string and produces a corresponding hash key based on the bitwise
hash operation described earlier.
● Takes a hash key and compresses it to fit within the range bounded by the size of the
hash table.
TASK 2: CHAINING
In this task you will be implementing a hash table of a fixed size that uses chaining to
resolve any collisions. Refer to Chaining.hpp which provides all the required definitions.
Write your implementation in Chaining.cpp using the boiler code. In order to implement the
chaining aspect, you will need to use the provided LinkedList implementation.
FIGURE 1: CHAINING
MEMBER FUNCTIONS:
HashC(int size)
● Returns a hash key corresponding to the given string. Make use of the functions you
wrote in HashFunction.cpp to implement this method.
● Finds the given word in the hash table and returns a pointer to it. NULL is
returned if the word does not exist.
HashL(int size)
● Returns a hash key corresponding to the given string. Make use of the functions you
wrote in HashFunction.cpp to implement this method.
void resizeTable()
● Resize the hash table once the load factor becomes too large.
● Finds the given word in the hash table and returns a pointer to it. NULL is
returned if the word does not exist.
MEMBER FUNCTIONS:
Write the implementation for the following functions as described here:
void insertKey(int k)
void deleteKey(int i)
int parent(int i)
int left(int i)
int right(int i)
int extractMin()
int getMin()
The header file sorts.h has declarations for all sort functions. You must implement all of
these functions in sorts.cpp. You have also been given an implementation of the Linked List
data structure in LinkedList.h and LinkedList.cpp files which must be used in merge sort. You
must write all your code in sorts.cpp only and are not allowed to alter any other files. To see
the output of your sorting algorithms, use generator.cpp. Additionally, you have been
provided final test cases separately for both the parts as well (explained below) which you
must pass in order to receive any credit.
Please do keep in mind that it is not compulsory to use generator.cpp at all and you can
solve this entire part without using it even once. It has only been provided to aid you with
visualizing the output of your sorting functions. However, it is compulsory to pass the
individual test cases for both insertion sort and merge sort, given in the individual test case
files respectively.
Note: You must implement the actual insertion sort and merge sort algorithms
as taught in the class. You must also make sure to follow all the instructions
related to individual tasks given below, otherwise you may end up losing
marks.
TASK 1: INSERTION SORT
In this task, you will be implementing an array-based implementation of insertion sort, as
discussed in class. You will first be transferring longs from the unsorted vector to an array,
sorting the array and then transferring back elements from this sorted array back to a vector
(old or a new one). You will then be returning this sorted vector.
Note: You must make sure that tests for these sorting algorithms pass on a
Linux (Ubuntu) machine. We will be testing your implementation on Ubuntu
Linux so you will only receive credit if your test cases pass on Ubuntu.
PART 4: TRIES
In this part, you will be practically implementing the trie data structure. According to
Wikipedia, a trie is a type of search tree, a tree data structure used for locating specific keys
from within a set. These keys are most often strings, with links between nodes defined not
by the entire key, but by individual characters. In our case, we will be using words from the
English language as keys and Alphabets of the English language as individual characters.
Each node will be storing a character and a vector of nodes as its child nodes. For example,
a trie with keys bat and ball will look like this:
You have been provided trie.hpp and trie.cpp files. Member function declarations of the
trie class as well as the structure for a Node have been provided in the trie.hpp file. You are
not allowed to make any changes in the existing function declarations or the node structure
otherwise your test cases will not pass. You must implement all the member functions
declared in trie.hpp. Additionally, you are allowed to create as many helper functions as
private members of the trie class.
MEMBER FUNCTIONS:
● Searches for a word within the trie. Returns true if a word is found within the trie,
otherwise returns false
● For example, search(“Ball”) returns true whereas search(“Base”) returns false
string longestSubstr(string word)
vector<string> getTrie()
● Traverses the entire trie and returns all the words present within the trie, in
alphabetical order, in a vector
● For example, applying this function on the above trie will return a vector with the
following strings:
“Back”, “Ball”, “Bat”
● Removes the word provided as an argument to it, from the trie. However, it must
ensure not to delete the characters which are common ancestors to several words.
● For example, when deleteWord(“Back”) is called on the above trie, the resultanttrie
will look like this:
● As you can see, the characters ‘B’ and ‘A’ did not get deleted when
deleteWord(“Back”) was called because they were ancestors to the words “Ball” and
“Bat”.
HELPER FUNCTIONS
While it is highly encouraged to think of helper functions yourself, we would strongly suggest
you to make the following helper function before making any of the above functions as it will
remain useful to you while implementing several of the above functions:
findChar()
Make a function that searches if a particular character exists within the trie at one particular
level. When successfully implemented, this function should tell you if that character is found
or not. The return type and the parameters of this function are totally up to you to decide.
TRIE VISUALIZATION
The following link is being provided to you to better help you out with visualizing tries. We
would recommend you to visit this link and try out different trie operations such as insertion
and deletion and then see how the trie looks like as a result of these operations. This will
help you better understand what's expected from you in this part.
https://www.cs.usfca.edu/~galles/visualization/Trie.html
Note: Please keep in mind that your implementation will be tested against
hidden test cases that will not be disclosed to you. However, as long as your
implementation is generic enough and you have used the correct approach
while writing these functions, alongside ensuring that you have followed all
the rules of a standard trie, your implementation should pass any and all
hidden test cases and you do not need to worry about this at all.
TESTING
To test the implementation of your code, compile and run the test files using the following
commands:
PART 1:
PART 2:
PART 3:
PART 4:
Task Marks
Chaining 5
Linear Probing 20
Heaps 15
Insertion Sort 5
Merge Sort 15
Tries 40
SUBMISSION GUIDELINES
Zip the complete folder and use the following naming convention: