9.12 Lab: Hashing - Linear Probe (insert, search)
Hash Function: Add the ASCII values of all characters in the key string and return the reminder obtained when divided by the size of the table. Example: If key = "Bob", and size = 53, we get (66 + 111 + 98) % 53 => 275 % 53 => 10. Collision Resolution Method: Linear Probe. The assignment consists of the following classes/files: - main.cpp (incomplete) - Student.h (given) - HashNode.h (given) - HashTable.h (given) - HashTable.cpp (incomplete) Read and understand the given code then write two functions: - insert hash - search hash
This commit is contained in:
parent
01d2d7f2cf
commit
3d112946f5
8
06-hash-tables/.idea/.gitignore
generated
vendored
Normal file
8
06-hash-tables/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
1
06-hash-tables/.idea/.name
generated
Normal file
1
06-hash-tables/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
|||||||
|
06_hash_tables
|
2
06-hash-tables/.idea/06-hash-tables.iml
generated
Normal file
2
06-hash-tables/.idea/06-hash-tables.iml
generated
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
6
06-hash-tables/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
06-hash-tables/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
4
06-hash-tables/.idea/misc.xml
generated
Normal file
4
06-hash-tables/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
8
06-hash-tables/.idea/modules.xml
generated
Normal file
8
06-hash-tables/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/06-hash-tables.iml" filepath="$PROJECT_DIR$/.idea/06-hash-tables.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
06-hash-tables/.idea/vcs.xml
generated
Normal file
6
06-hash-tables/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
7
06-hash-tables/CMakeLists.txt
Normal file
7
06-hash-tables/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(06_hash_tables)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
add_executable(06_hash_tables main.cpp
|
||||||
|
HashTable.cpp)
|
50
06-hash-tables/HashNode.h
Normal file
50
06-hash-tables/HashNode.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Specification file for the HashNode class
|
||||||
|
|
||||||
|
#ifndef _HASH_NODE
|
||||||
|
#define _HASH_NODE
|
||||||
|
|
||||||
|
#include "Student.h"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
class HashNode {
|
||||||
|
private:
|
||||||
|
Student item;
|
||||||
|
int occupied; // 1 -> occupied, 0 -> empty
|
||||||
|
int noCollisions;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
HashNode() {
|
||||||
|
occupied = 0;
|
||||||
|
noCollisions = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
HashNode(Student anItem) {
|
||||||
|
item = anItem;
|
||||||
|
occupied = 1;
|
||||||
|
noCollisions = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
HashNode(Student anItem, int ocp, int nCol) {
|
||||||
|
item = anItem;
|
||||||
|
occupied = ocp;
|
||||||
|
noCollisions = nCol;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setters
|
||||||
|
void setItem(const Student &anItem) { item = anItem; }
|
||||||
|
|
||||||
|
void setOccupied(int ocp) { occupied = ocp; }
|
||||||
|
|
||||||
|
void setNoCollisions(int nCol) { noCollisions = nCol; }
|
||||||
|
|
||||||
|
// getters
|
||||||
|
Student getItem() const { return item; }
|
||||||
|
|
||||||
|
int getOccupied() const { return occupied; }
|
||||||
|
|
||||||
|
int getNoCollisions() const { return noCollisions; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
71
06-hash-tables/HashTable.cpp
Normal file
71
06-hash-tables/HashTable.cpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
// Implementation file for the Hash class
|
||||||
|
// Written By: Iurii Tatishchev
|
||||||
|
// Changed by: Iurii Tatishchev
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "HashTable.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
/*~*~*~*
|
||||||
|
A simple hash function
|
||||||
|
*~**/
|
||||||
|
int HashTable::_hash(string key) const {
|
||||||
|
int sum = 0;
|
||||||
|
for (char c : key)
|
||||||
|
sum += c;
|
||||||
|
return sum % hashSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*~*~*~*
|
||||||
|
hash insert - linear probe
|
||||||
|
*~**/
|
||||||
|
bool HashTable::insert(const Student &itemIn) {
|
||||||
|
if (count == hashSize)
|
||||||
|
return false;
|
||||||
|
int pos = _hash(itemIn.getName());
|
||||||
|
int collisions = 0;
|
||||||
|
while (hashAry[pos].getOccupied()) {
|
||||||
|
++pos;
|
||||||
|
++collisions;
|
||||||
|
pos = pos % hashSize;
|
||||||
|
}
|
||||||
|
hashAry[pos].setItem(itemIn);
|
||||||
|
hashAry[pos].setOccupied(1);
|
||||||
|
hashAry[pos].setNoCollisions(collisions);
|
||||||
|
count++;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*~*~*~*
|
||||||
|
hash delete - linear probe
|
||||||
|
*~**/
|
||||||
|
bool HashTable::remove(Student &itemOut) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*~*~*~*
|
||||||
|
hash search - linear probe
|
||||||
|
search for key
|
||||||
|
if found:
|
||||||
|
- copy data to itemOut
|
||||||
|
- copy number of collisions for this key to noCol
|
||||||
|
- returns true
|
||||||
|
if not found, returns false
|
||||||
|
*~**/
|
||||||
|
bool HashTable::search(Student &itemOut, int &noCol, string key) {
|
||||||
|
int pos = _hash(key);
|
||||||
|
for (int collisions = 0; collisions < count; collisions++) {
|
||||||
|
if (!hashAry[pos].getOccupied()) return false;
|
||||||
|
if (hashAry[pos].getItem().getName() == key) {
|
||||||
|
itemOut = hashAry[pos].getItem();
|
||||||
|
noCol = hashAry[pos].getNoCollisions();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
pos = (pos + 1) % hashSize;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
51
06-hash-tables/HashTable.h
Normal file
51
06-hash-tables/HashTable.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Specification file for the Hash class
|
||||||
|
|
||||||
|
#ifndef HASHTABLE_H_
|
||||||
|
#define HASHTABLE_H_
|
||||||
|
|
||||||
|
#include "HashNode.h"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
class HashTable {
|
||||||
|
private:
|
||||||
|
HashNode *hashAry;
|
||||||
|
int hashSize;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
public:
|
||||||
|
HashTable() {
|
||||||
|
count = 0;
|
||||||
|
hashSize = 53;
|
||||||
|
hashAry = new HashNode[hashSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
HashTable(int n) {
|
||||||
|
count = 0;
|
||||||
|
hashSize = n;
|
||||||
|
hashAry = new HashNode[hashSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
~HashTable() { delete[] hashAry; }
|
||||||
|
|
||||||
|
int getCount() const { return count; }
|
||||||
|
|
||||||
|
int getSize() const { return hashSize; }
|
||||||
|
|
||||||
|
double getLoadFactor() const { return 100.0 * count / hashSize; }
|
||||||
|
|
||||||
|
bool isEmpty() const { return count == 0; }
|
||||||
|
|
||||||
|
bool isFull() const { return count == hashSize; }
|
||||||
|
|
||||||
|
bool insert(const Student &itemIn);
|
||||||
|
|
||||||
|
bool remove(Student &itemOut);
|
||||||
|
|
||||||
|
bool search(Student &itemOut, int &noCol, string key);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _hash(string key) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HASHTABLE_H_
|
34
06-hash-tables/Student.h
Normal file
34
06-hash-tables/Student.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Specification file for the Student class
|
||||||
|
|
||||||
|
#ifndef STUDENT_H
|
||||||
|
#define STUDENT_H
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
class Student {
|
||||||
|
private:
|
||||||
|
double gpa;
|
||||||
|
string name;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Student() {
|
||||||
|
name = "";
|
||||||
|
gpa = -1;
|
||||||
|
} // Constructor
|
||||||
|
Student(string n, double g) {
|
||||||
|
name = n;
|
||||||
|
gpa = g;
|
||||||
|
} // Overloaded Constructor
|
||||||
|
|
||||||
|
// Setters and getters
|
||||||
|
void setName(string n) { name = n; }
|
||||||
|
|
||||||
|
void setGpa(double g) { gpa = g; }
|
||||||
|
|
||||||
|
string getName() const { return name; }
|
||||||
|
|
||||||
|
double getGpa() const { return gpa; }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
79
06-hash-tables/main.cpp
Normal file
79
06-hash-tables/main.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
CIS 22C
|
||||||
|
|
||||||
|
This program builds and searches a hash table.
|
||||||
|
|
||||||
|
Written by: Iurii Tatishchev
|
||||||
|
Reviewed & Modified by: Iurii Tatishchev
|
||||||
|
IDE: CLion
|
||||||
|
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
#include "HashTable.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void buildHash(HashTable &hash);
|
||||||
|
|
||||||
|
void searchManager(HashTable &hash);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
HashTable hash;
|
||||||
|
string option;
|
||||||
|
|
||||||
|
buildHash(hash);
|
||||||
|
|
||||||
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
||||||
|
|
||||||
|
searchManager(hash);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* **************************************************
|
||||||
|
This function builds a hash table with data from the keyboard.
|
||||||
|
It calls the insert() function that inserts the new data at the right location in the hash table.
|
||||||
|
An input line contains the gpa of a student follow by name (assume it is a single word name)
|
||||||
|
To stop reading enter "#"
|
||||||
|
************************************************** */
|
||||||
|
void buildHash(HashTable &hash) {
|
||||||
|
string line;
|
||||||
|
cout << "Enter gpa and name [# to stop reading]:" << endl;
|
||||||
|
getline(cin, line);
|
||||||
|
while (line != "#") {
|
||||||
|
stringstream temp(line); // create a stringstream named temp with data from line
|
||||||
|
double gpa;
|
||||||
|
string name;
|
||||||
|
temp >> gpa; // read gpa from temp
|
||||||
|
temp >> name; // read name from temp
|
||||||
|
Student newStu(name, gpa);
|
||||||
|
hash.insert(newStu);
|
||||||
|
getline(cin, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* **************************************************
|
||||||
|
This function searches a hash table with user provided data.
|
||||||
|
It calls the hash search function in a loop.
|
||||||
|
To stop searching enter "#"
|
||||||
|
************************************************** */
|
||||||
|
void searchManager(HashTable &hash) {
|
||||||
|
string name;
|
||||||
|
cout << "Enter name [# to stop searching]:" << endl;
|
||||||
|
getline(cin, name);
|
||||||
|
while (name != "#") {
|
||||||
|
Student item;
|
||||||
|
int nc;
|
||||||
|
if (hash.search(item, nc, name)) {
|
||||||
|
cout << item.getName() << " " << item.getGpa() << " (" << nc << " collisions!)" << endl;
|
||||||
|
} else {
|
||||||
|
cout << name << " not found!" << endl;
|
||||||
|
}
|
||||||
|
getline(cin, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user