Create the same City Database that you created in the first question using Hash table. Add another attribute in the City record, that is, city_id. Use city_id to compute the hashing. Implment member functions to insert, delete, search and display city records. Use the chained hashing to resolve the collision issue. ------------------------------------------------------------------------- so here is my out put from the first question #include #include #include using namespace std; struct city{ string name; int x; int y; city *left; city *right; bool deleted; }; class database{ private: city *root; void insert(city *&temp, string name, int x, int y){ if(temp == NULL){ temp = new city(); temp->name = name; temp->x = x; temp->y = y; temp->left = NULL; temp->right = NULL; temp->deleted = false; } else{ if(name < temp->name){ insert(temp->left, name, x, y); } else if(name > temp->name){ insert(temp->right, name, x, y); } else{ if(temp->deleted == true){ temp->deleted = false; temp->x = x; temp->y = y; } else{ insert(temp->right, name, x, y); } } } } city *findByName(city *temp, string name){ if(temp != NULL){ if(temp->name == name) return (temp->deleted == false ? temp : findByName(temp->right, name)); else if(name < temp->name) return findByName(temp->left, name); else if(name > temp->name) return findByName(temp->right, name); } else{ return NULL; } } city *findByCoordinates(city *temp, int x, int y){ if(temp == NULL){ return NULL; } else{ city *ret = NULL; if(temp->x == x && temp->y == y){ if(temp->deleted == false){ ret = temp; } } if(ret == NULL){ ret = findByCoordinates(temp->left, x, y); } if(ret == NULL){ ret = findByCoordinates(temp->right, x, y); } return ret; } } void print(city *temp, int x, int y, double d){ if(temp != NULL){ print(temp->left, x, y, d); if(temp->deleted == false){ double distance = sqrt(pow(temp->x - x, 2) + pow(temp->y - y, 2)); if(distance < d){ cout << temp->name << " at (" << temp->x << ", " << temp->y << ")" << endl; } } print(temp->right, x, y, d); } } public: database(){ root = NULL; } void insertCity(string name, int x, int y){ insert(root, name, x, y); } void searchByName(string name){ city *found = findByName(root, name); if(found == NULL){ cout << "City not found" << endl; } else{ cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ")" << endl; } } void searchByCoordinates(int x, int y){ city *found = findByCoordinates(root, x, y); if(found == NULL){ cout << "City not found" << endl; } else{ cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ")" << endl; } } void deleteByName(string name){ city *found = findByName(root, name); if(found == NULL){ cout << "City not found" << endl; } else{ cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl; found->deleted = true; } } void deleteByCoordinates(int x, int y){ city *found = findByCoordinates(root, x, y); if(found == NULL){ cout << "City not found" << endl; } else{ cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl; found->deleted = true; } } void printInDistance(int x, int y, double d){ print(root, x, y, d); } }; int main(){ database db; db.insertCity("city2", 1, 1); db.insertCity("city1", 2, 3); db.insertCity("city3", 1, 3); db.printInDistance(0, 0, 10); return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Create the same City Database that you created in the first question using Hash table. Add another attribute in the City record, that is, city_id. Use city_id to compute the hashing. Implment member functions to insert, delete, search and display city records. Use the chained hashing to resolve the collision issue.

-------------------------------------------------------------------------

so here is my out put from the first question

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

struct city{
string name;
int x;
int y;
city *left;
city *right;
bool deleted;
};

class database{
private:
city *root;
void insert(city *&temp, string name, int x, int y){
if(temp == NULL){
temp = new city();
temp->name = name;
temp->x = x;
temp->y = y;
temp->left = NULL;
temp->right = NULL;
temp->deleted = false;
}
else{
if(name < temp->name){
insert(temp->left, name, x, y);
}
else if(name > temp->name){
insert(temp->right, name, x, y);
}
else{
if(temp->deleted == true){
temp->deleted = false;
temp->x = x;
temp->y = y;
}
else{
insert(temp->right, name, x, y);
}
}
}
}
city *findByName(city *temp, string name){
if(temp != NULL){
if(temp->name == name) return (temp->deleted == false ? temp : findByName(temp->right, name));
else if(name < temp->name) return findByName(temp->left, name);
else if(name > temp->name) return findByName(temp->right, name);
}
else{
return NULL;
}
}
city *findByCoordinates(city *temp, int x, int y){
if(temp == NULL){
return NULL;
}
else{
city *ret = NULL;
if(temp->x == x && temp->y == y){
if(temp->deleted == false){
ret = temp;
}
}
if(ret == NULL){
ret = findByCoordinates(temp->left, x, y);
}
if(ret == NULL){
ret = findByCoordinates(temp->right, x, y);
}
return ret;
}
}
void print(city *temp, int x, int y, double d){
if(temp != NULL){
print(temp->left, x, y, d);
if(temp->deleted == false){
double distance = sqrt(pow(temp->x - x, 2) + pow(temp->y - y, 2));
if(distance < d){
cout << temp->name << " at (" << temp->x << ", " << temp->y << ")" << endl;
}
}
print(temp->right, x, y, d);
}
}
public:
database(){
root = NULL;
}
void insertCity(string name, int x, int y){
insert(root, name, x, y);
}
void searchByName(string name){
city *found = findByName(root, name);
if(found == NULL){
cout << "City not found" << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ")" << endl;
}
}
void searchByCoordinates(int x, int y){
city *found = findByCoordinates(root, x, y);
if(found == NULL){
cout << "City not found" << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ")" << endl;
}
}
void deleteByName(string name){
city *found = findByName(root, name);
if(found == NULL){
cout << "City not found" << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl;
found->deleted = true;
}
}
void deleteByCoordinates(int x, int y){
city *found = findByCoordinates(root, x, y);
if(found == NULL){
cout << "City not found" << endl;
}
else{
cout << "City " << found->name << " found at (" << found->x << ", " << found->y << ") has been deleted" << endl;
found->deleted = true;
}
}
void printInDistance(int x, int y, double d){
print(root, x, y, d);
}
};

int main(){
database db;
db.insertCity("city2", 1, 1);
db.insertCity("city1", 2, 3);
db.insertCity("city3", 1, 3);
db.printInDistance(0, 0, 10);
return 0;
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education