Question

How do you compute the size (tableSize) of hashtable in c++, pseudocode or an example might...

How do you compute the size (tableSize) of hashtable in c++, pseudocode or an example might work.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can maintain a separate variable say 'size' which dertermines the size of the hashtable on the go. Now, the important thing to notice is that you would have to maintain the size value yourself. Whenever, you insert an element into the hashtable, you have to increment it and whenever you remove an element from the hashtable, you should decrement it.

Please find a sample hashtable implementation below (HERE I AM EXPLICITELY MAINTAINING THE SIZE):

CODE

================

#include<bits/stdc++.h>

using namespace std;

//template for generic type

template<typename K, typename V>

//Hashnode class

class HashNode

{

public:

V value;

K key;

//Constructor of hashnode

HashNode(K key, V value)

{

this->value = value;

this->key = key;

}

};

//template for generic type

template<typename K, typename V>

//Our own Hashmap class

class HashMap

{

//hash element array

HashNode<K,V> **arr;

int capacity;

//current size

int size;

//dummy node

HashNode<K,V> *dummy;

public:

HashMap()

{

//Initial capacity of hash array

capacity = 20;

size=0;

arr = new HashNode<K,V>*[capacity];

//Initialise all elements of array as NULL

for(int i=0 ; i < capacity ; i++)

arr[i] = NULL;

//dummy node with value and key -1

dummy = new HashNode<K,V>(-1, -1);

}

// This implements hash function to find index

// for a key

int hashCode(K key)

{

return key % capacity;

}

//Function to add key value pair

void insertNode(K key, V value)

{

HashNode<K,V> *temp = new HashNode<K,V>(key, value);

// Apply hash function to find index for given key

int hashIndex = hashCode(key);

//find next free space

while(arr[hashIndex] != NULL && arr[hashIndex]->key != key

&& arr[hashIndex]->key != -1)

{

hashIndex++;

hashIndex %= capacity;

}

//if new node to be inserted increase the current size

if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1)

size++;

arr[hashIndex] = temp;

}

//Function to delete a key value pair

V deleteNode(int key)

{

// Apply hash function to find index for given key

int hashIndex = hashCode(key);

//finding the node with given key

while(arr[hashIndex] != NULL)

{

//if node found

if(arr[hashIndex]->key == key)

{

HashNode<K,V> *temp = arr[hashIndex];

//Insert dummy node here for further use

arr[hashIndex] = dummy;

// Reduce size

size--;

return temp->value;

}

hashIndex++;

hashIndex %= capacity;

}

//If not found return null

return NULL;

}

//Function to search the value for a given key

V get(int key)

{

// Apply hash function to find index for given key

int hashIndex = hashCode(key);

//finding the node with given key

while(arr[hashIndex] != NULL)

{

//if node found return its value

if(arr[hashIndex]->key == key)

return arr[hashIndex]->value;

hashIndex++;

hashIndex %= capacity;

}

//If not found return null

return NULL;

}

//Return current hashtable size

int getTableSize()

{

return size;

}

//Return true if size is 0

bool isEmpty()

{

return size == 0;

}

//Function to display the stored key value pairs

void display()

{

for(int i=0 ; i<capacity ; i++)

{

if(arr[i] != NULL && arr[i]->key != -1)

cout << "key = " << arr[i]->key

<<" value = "<< arr[i]->value << endl;

}

}

};

//Driver method to test map class

int main()

{

HashMap<int, int> *h = new HashMap<int, int>;

h->insertNode(1,1);

h->insertNode(2,2);

h->insertNode(2,3);

h->display();

cout << h->getTableSize() <<endl;

h->deleteNode(2);

cout << h->getTableSize() <<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
How do you compute the size (tableSize) of hashtable in c++, pseudocode or an example might...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static...

    To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static array for the hash table - the definition of a hash function HASH(X) suitable for storing string keys (not integers). The student is expected to propose his/her own hash table. - a method for inserting keys in the hash table. The solution MUST consider chaining (e.g. linked lists, trees, etc.) as the collision response criteria. - a method for searching keys...

  • give an example of how the problem of moral hazard might prevent you from getting financing...

    give an example of how the problem of moral hazard might prevent you from getting financing for something you want to do. Can you think of a way of overcoming this problem

  • How do you solve for letter e?? 3. A cloud might have -5.00 C of charge...

    How do you solve for letter e?? 3. A cloud might have -5.00 C of charge and be 200.0 m above the earth. The ground gains +3.000 C because of induction. a. How many electrons make up the 15.00 C charge? 312XIOiq e ab. How much force will be unleashed by the bolt of lightening that follows?33xio o C .hanc-e. How strong is the electric field around the cloud,m4- d. What potential difference was created when the eleetrons moved the...

  • Give an example in the real world where you might do a t-test. Explain what your...

    Give an example in the real world where you might do a t-test. Explain what your sample and population averages might be.

  • What might be an example of a personal goal? How could you use KPIs and metrics...

    What might be an example of a personal goal? How could you use KPIs and metrics to help you move toward that goal?

  • How does the size of an object affect the grip that an infant uses?  How might this...

    How does the size of an object affect the grip that an infant uses?  How might this factor influence where an infant falls on the Halverson’s prehension sequence?  How does the shape of an object affect the grip used?              Do infants learn to reach for objects by better matching their hand position to the seen location of the object, or by better controlling their arms? Explain why you chose your answer. 3. What are the major developmental trends we see in...

  • this assingent is to  pseudocode, i have instructions and the example my teacher had given me for...

    this assingent is to  pseudocode, i have instructions and the example my teacher had given me for this, could i have some assistance, i need to do this in netbeans IDE and i need to use the scanner method as well and i need to pass both. Overview: This assignment will allow you to use pseudocode to implement the program to see the value in planning first, coding later. While coding is the glamorous part of the job, software development is...

  • How do you think the moderator might change the relationship?

    How do you think the moderator might change the relationship?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT