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
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
ADVERTISEMENT