• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Hash Functions/Tables

JC0133

Senior member
So I am trying to understand Hash Functions and Tables, I know there are different types like direct and Open addressing. What is the point of hash functions and tables, how do they work in the real world or what are they used for? Also a Hash function is supposed to be like a Dictionary?

I don't understand if the table is an Array and the Array slots have linked list in them or something like that? From what I have been reading there is a Universal Set, where it has some keys which picks the slots.

Is the Universal set an array with another array of keys in it of numbers?

Can you actually put a linked list into an Array slot?

Can somebody right some basic puesdo code to kinda show me how a basic hash function and Table work with the Universal set and Keys?
 
Whew! It sounds to me like you could use a good basic book on the subject. With universal sets and perfect hashes it seems like you need an advanced book on the subject too. But I'll try and summarize some answers.

What is the point of hash functions and tables, how do they work in the real world or what are they used for? Also a Hash function is supposed to be like a Dictionary?
To answer backwards, A hash function is used to map keys into a hash table. A hash table is one way of implementing a dictionary. A tree is another common way. A hash table allows you to quickly store and look up data in a dictionary by key. It does not make it easy to go through all the data in sorted order, while a tree does. But lookups in a hash table are faster than in a tree.

I know there are different types like direct and Open addressing.
Those define what you do when two different keys map to the same location in the array.

From what I have been reading there is a Universal Set, where it has some keys which picks the slots.
I hadn't heard of universal hashing before. Looks like it's just certain hash functions that work particularly well (produce fewer collisions than the average hash function) with certain data types. But universal sets look like set theory applied to hash functions, and I'm not prepared to get into that. This seems to be related to perfect hash functions, though. If you find a perfect hash function for your data, you don't have to worry about collisions. 🙂

Can you actually put a linked list into an Array slot?
Depends on the language, but in most, yes. In C you would just make the array an array of pointers.
 
So I am trying to understand Hash Functions and Tables, I know there are different types like direct and Open addressing. What is the point of hash functions and tables, how do they work in the real world or what are they used for? Also a Hash function is supposed to be like a Dictionary?

Your question has some basic and some advanced parts, as Ken_g6 pointed out. For the basic parts (what's the point, how do they work in the real world, what are they used for...) the wikipedia article on hash tables is actually really good:

https://en.wikipedia.org/wiki/Hash_table

I would point out this section in particular:
In a well-dimensioned hash table, the average cost (number of instructions) for each lookup is independent of the number of elements stored in the table. Many hash table designs also allow arbitrary insertions and deletions of key-value pairs, at (amortized[2]) constant average cost per operation.[3][4]
That is to say: search, insert, and delete operations should all be, on average, O(1) if your table/function/data are all well chosen. You're essentially creating a key-value pair, where the hash of the key tells you the memory address to find the value. There is no searching. You just go directly to the correct memory address where the value is stored.
 
So I am trying to figure out how to create an dynamic array of linked list for my hash table. I am using a basic linked list below that I got out of one of my C++ books. Is this the correct way to do it? How do I initialize each pointer to NULL?

What I am confused about is most example I keep finding online about creating a Hash table, it seems like they are making the array out of the pointer class not the linked list class, this is very confusing to me.

Code:
//My example

#include <iostream>
using namespace std;

class IntNode {
    public:
        IntNode(){}
        IntNode(int theData, IntNode* theLink) : data(theData),link(theLink) {}
        IntNode* getLink() const {return link; }
        int getData() const {return data; }
        void setData(int theData) {data = theData; }
        void setLink(IntNode* pointer) {link = pointer; }

private:
    int data;
    IntNode *link;

};

typedef IntNode* IntNodePtr;

class LinkedList1 {
    public:
        LinkedList1() {head = NULL;}
        //void headInsert(IntNodePtr& head, int theData);
        void headInsert(int theData);
        void insert(IntNodePtr afterMe, int theData);
        void deleteHeadNode();
        void remove(int theData);
        void output();
    private:
        IntNodePtr head;
};



int main() {
    char ans;
    int tableSize=0;

    cout<<"Please enter in the table size: ";
    cin >> tableSize;

[B]    LinkedList1* first;

    first = new LinkedList1[tableSize];[/B]




    do {
        first[0].headInsert(2);
        first[0].headInsert(3);
        first[0].headInsert(4);
        first[0].headInsert(5);
        first[0].headInsert(6);
        first[0].output();
        first[0].deleteHeadNode();
        cout<<endl;
        first[0].output();
        first[0].remove(3);
        cout<<endl;
        first[0].output();

        cout << "Would you like to start over again(y = yes/n = no)? ";
        cin >> ans;

    } while (ans != 'n');

    return 0;
}//end of main


void LinkedList1::headInsert(int theData) {
    head = new IntNode(theData, head);
}//end of headInsert

void LinkedList1::insert(IntNodePtr afterMe, int theData) {
    afterMe->setLink(new IntNode(theData, afterMe->getLink()));
}//end of insert

void LinkedList1::deleteHeadNode() {
    if (head != NULL) {
        head = head->getLink();
    }//end of if statement
    else {
        cout<<"You are at the first node already"<<endl;
    }//end of else
}//end of deleteHeadNode

void LinkedList1::remove(int theData) {
    IntNodePtr before,discard,test; 
    before = head;
    discard = head;
    test = head;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    if (before == NULL) {
        cout<<"List is empty"<<endl;
    }
    else {
        while(discard->getData() != theData && discard->getLink() != NULL) {
            count1++;
            discard = discard->getLink();
        }//end of while loop

        int i = count1 - 1;

        while (count2 != i) {
            before = before->getLink();
            count2++;
        }//end of while loop

        while(test->getLink() != NULL) {
            test = test->getLink();
            count3++;
        }

        if(count3 == count1) {
            cout<<"Value is not in the list"<<endl;
        }
    }

    before->setLink(discard->getLink());
}//end of remove function

void LinkedList1::output() {
    IntNodePtr position = head;

    if(position == NULL) {
        cout<<"The list is empty"<<endl;
    }
    else {
        while(position != NULL) {
            cout<<"The values of the list are "<<position->getData()<<endl;
            position = position->getLink();
        }//end of while loop
    }
}//end of output list
 
So I am trying to figure out how to create an dynamic array of linked list for my hash table. I am using a basic linked list below that I got out of one of my C++ books. Is this the correct way to do it? How do I initialize each pointer to NULL?

You don't know how to initialize an array of pointers to NULL? 😵

What I am confused about is most example I keep finding online about creating a Hash table, it seems like they are making the array out of the pointer class not the linked list class, this is very confusing to me.

When you're doing a hash table with separate chaining, each entry is a pointer to a separate linked list of length zero or more.

450px-Hash_table_5_0_1_1_1_1_1_LL.svg.png


If you find it difficult to construct a linked list of keys and values, I suggest starting by using std::list, defining a class for your linked list nodes, and using std::list<yourclass>.
 
So when creating a linked list, I know there is a node class, and a linked list class.

Where creating a hash table is there a node class, linked list class and a Hash Table class?
 
So when creating a linked list, I know there is a node class, and a linked list class.

Where creating a hash table is there a node class, linked list class and a Hash Table class?

Probably more of a Hashtable and a node class.

The hashtable has an array (needs to have fast random access) of either linked lists or (more likely) autogrowing arrays/ArrayLists.

The complexity of a hashtable is figuring out how big it should be, and, often, figuring out how to handle resizing.
 
Back
Top