let’s complete the function getHashIndex() which implements the hashing function. Recall that the hashing function take

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

let’s complete the function getHashIndex() which implements the hashing function. Recall that the hashing function take

Post by answerhappygod »

let’s complete the function getHashIndex() which
implements the hashing function. Recall that the hashing
function takes a key and returns the index in the hash table where
that entry is located.
We’ll use a very simple hashing function: key %
hashTableSize. hashTableSize is already defined as a private
member variable of the class and its value is set by the
constructor.
Find the comment // STEP 2 and complete getHashIndex()
to return the proper index.
Step 3
Next we’ll implement the add() function. Given a key and
value, add() first hashes the key to get itemHashIndex, then adds
the a new HashedEntry node containing the entry to the linked chain
pointed to by hashTable[itemHashIndex].
The new HashedEntry node which add() creates will need
to be pointed to by a shared pointer. For simplicity, the new node
will always be added to the beginning of the linked chain pointed
to by hashTable[itemHashIndex].
Here is the pseudocode for add():
It isn’t in the pseudocode, but add() must also
increment the private member variable itemCount when an entry is
added.
Locate the comment // STEP 3 and complete the
implementation of add().
Step 4
The remove() function locates the entry for a given key
(if it exists) and removes that entry. So, remove() must hash
the key to get itemHashIndex, then traverse the linked chain
pointed to by the hashTable[itemHashIndex] looking for a matching
key. If a node with a matching key is found that node is
removed. Otherwise the given key was not in the
dictionary.
Here is the pseudocode for remove():
It isn’t in the pseudocode, but remove() must also
decrement the private member variable itemCount when an entry is
added.
Find the comment // STEP 4 and complete
remove().
Step 5
The getItem() function returns the value of an entry for
a given key. Its process is quite similar to remove() but it
does not, of course, remove any data.
So, getItem() must hash the key to get itemHashIndex,
then traverse the linked chain pointed to by the
hashTable[itemHashIndex] looking for a matching key. If a
node with a matching key is found then its value is returned,
otherwise it should throw a logic_error which says “Item not found
in Dictionary!”
Find the comment // STEP 5 and complete
getItem().
Step 6
The function contains() is very similar to
getItem(). Given a key, contains() returns true if the key is
stored in the dictionary, false if it is not.
Find the comment // STEP 6 and complete
contains().
Step 7
The function traverse() visits each entry in the
dictionary in sorted order by its key value and runs a function on
the value of the entry. The “visit” is done by calling the function
visit() on value stored in each node. visit() is given to
traverse() as a parameter.
The pseudocode for traverse() is:
For index = 0 through index <
hashTableSize
If hashTable[index] != nullptr
Traverse the linked chain pointed to by hashTable[index]
and call visit() on the value stored in each node
Note: While doing the traverse, you’ll need to pull the
data out of the node before passing it to visit(). So, if
curPtr is pointing to your node, don’t do this:

visit(curPtr->getItem());
Instead, do this:
ItemType curItem = curPtr->getItem();
visit(curItem);
Find the comment // STEP 7 and complete
traverse().
template <class KeyType, class ItemType>
using hashTableType = std::shared_ptr<HashedEntry<KeyType,
ItemType>>[];
template <class KeyType, class ItemType>
class HashedDictionary
{
private:
// creates a unique pointer to an array of shared
HashedEntry pointers
std::unique_ptr<hashTableType<KeyType,
ItemType>> hashTable; // Array of pointers to
entries
int itemCount;

// Count of dictionary entries
int hashTableSize;
// Table
size must be prime
static const int DEFAULT_CAPACITY = 101;

void destroyDictionary();
int getHashIndex(const KeyType& itemKey) const;

int getNextPrime(int number) const;
bool isPrime(int number) const;

public:
HashedDictionary();
HashedDictionary(int maxNumberOfEntries);
HashedDictionary(const HashedDictionary<KeyType,
ItemType>& dict);

virtual ~HashedDictionary();

bool isEmpty() const;
int getNumberOfItems() const;
void clear();
bool add(const KeyType& itemKey, const
ItemType& newItem);
bool remove(const KeyType& itemKey);
ItemType getItem(const KeyType& itemKey) const;

bool contains(const KeyType& itemKey) const;

void traverse(void visit(ItemType&)) const;

}; // end HashedDictionary
//////////////////////////////////////////////////////////////////////

// Private helper methods
//////////////////////////////////////////////////////////////////////
template <class KeyType, class ItemType>
void HashedDictionary<KeyType,
ItemType>::destroyDictionary()
{
for (int i = 0; i < hashTableSize; i++)
{
// If there are hashed entries at this
location,
// we need to delete them
while (hashTable != nullptr)
{
hashTable =
hashTable->getNext();
} // end while
} // end for
itemCount = 0;
} // end destroyDictionary
template <class KeyType, class ItemType>
int HashedDictionary<KeyType, ItemType>::getHashIndex(const
KeyType& key) const
{
// STEP 2
} // end getHashIndex
///////////////////////////////////////////////////////////////////

// Container methods (add, remove, getItem, contains,
traverse)
//////////////////////////////////////////////////////////////////////
template <class KeyType, class ItemType>
bool HashedDictionary<KeyType, ItemType>::add(const
KeyType& searchKey, const ItemType& newItem)
{
// STEP 3
return false;
} // end add
template <class KeyType, class ItemType>
bool HashedDictionary<KeyType, ItemType>::remove(const
KeyType& searchKey)
{
//STEP 4
return false;
} // end remove
template <class KeyType, class ItemType>
ItemType HashedDictionary<KeyType, ItemType>::getItem(const
KeyType& searchKey) const
{
// STEP 5
ItemType temp;
return temp;
} // end getItem
template <class KeyType, class ItemType>
bool HashedDictionary<KeyType, ItemType>::contains(const
KeyType& searchKey) const
{
// STEP 6
return false;
} // end contains
template <class KeyType, class ItemType>
void HashedDictionary<KeyType, ItemType>::traverse(void
visit(ItemType&)) const
{
// STEP 7
} //end traverse
#endif
in C++
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply