Experiment 1 (1st) Consider the telephone book database of N clients. Make use of a hash table implementation to quickly look up a client's telephone number. Make use of two collision handling techniques and compare them using the number of comparisons required to find a set of telephone numbers. CODE: def tele_database(): phone_data = [] n = int(input("Enter Number of Clients :- ")) print("Enter Phone Numbers --\n") for _ in range(n): x = int(input("--> ")) phone_data.append(x) return phone_data def hash_function_1(key_ele, m_size): h1 = key_ele % m_size return h1 def hash_function_2(key_ele, m_size): h2 = 1 + (key_ele % (m_size - 1)) return h2 def hashtable(ht): print(f"\nHash Value \tKey") for ele in range(len(ht)): if ht[ele] != -1: print(f"\n\t{ele} \t---> \t{ht[ele]}") else: print(f"\n\t{ele}") phone_database = tele_database() m = int(input("Enter Hash Table Size :- ")) hash_table = [-1] * m opt = int(input("If collision occurs which collision resolution technique do you want to use?\n\t1. Linear " "Probing\n\t2. Double Hashing :- ")) for k in phone_database: h_1 = hash_function_1(k, m) if hash_table[h_1] == -1: hash_table[h_1] = k else: if opt == 1: while hash_table[h_1] != -1: h_1 = (h_1 + 1) % m hash_table[h_1] = k elif opt == 2: i=0 h_2 = hash_function_2(k, m) while hash_table[h_1] != -1: i += 1 h_1 = (h_1 + i * h_2) % m hash_table[h_1] = k hashtable(hash_table) Output: Experiment 2 (3) A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method CODE: #include <iostream> #include <string.h> using namespace std ; struct node { char lable[20]; int ch_count; struct node *child[10]; }*root; class GT { public: GT() { root=NULL; } void create_tree(); void display_tree(node *r); }; void GT::create_tree() { int tbook,tchapter,i,j,k,l; root=new node; cout<<"Enter the name of the book: "; cin>>root->lable; cout<<"Enter Number of Chapter: "; cin>>root->ch_count; tchapter=root->ch_count; for(i=0;i<tchapter;i++) { root->child[i]=new node; cout<<"Enter Chapter "<<i+1<<" name: "; cin>>root->child[i]->lable; cout<<"Enter Number of Section in the Chapter: "; cin>>root->child[i]->ch_count; for(j=0;j<root->child[i]->ch_count;j++) { root->child[i]->child[j]=new node; cout<<"Enter Section "<<j+1<<" name: "; cin>>root->child[i]->child[j]->lable; } } } void GT::display_tree(node *r1) { int i,j,k,tchapter; if(r1!=NULL) { cout<<"\n------------Book Hierarchy------------"; cout<<"\n Book Title: " <<r1->lable<<endl; tchapter=r1->ch_count; for(i=0;i<tchapter;i++) { cout<<"\n Chapter "<<i+1<<" : "; cout<<r1->child[i]->lable; cout<<"\n Section: "; for(j=0;j<r1->child[i]->ch_count;j++) { cout<<" "<<r1->child[i]->child[j]->lable<<endl; } } } } int main() { int choice; GT gt; while(1) { cout<<"--------------------"<<endl; cout<<"Book Tree Creation"<<endl; cout<<"--------------------"<<endl; cout<<"1. Create"<<endl; cout<<"2. Display"<<endl; cout<<"3. Quit"<<endl; cout<<"Enter the choice: "; cin>>choice; switch (choice) { case 1: gt.create_tree(); break; case 2: gt.display_tree(root); break; case 3: exit(1); default: cout << "Wrong Choice....!!!"<<endl; } } } OUTPUT: Experiment 3 CODE: #include <iostream> #include<iomanip> using namespace std; class node { private: int data; node * next; public: node() { data = 0; next = NULL; } friend class hashtab; }; class hashtab { private: node hashtable[20]; public: hashtab() { for(int i=0;i<20;i++) { hashtable[i].data=0; } } int hash(int x) { int address = x%20; return address; } void insert(int x) { int index; index = hash(x); if (hashtable[index].data==0) { hashtable[index].data = x; } else { node *pNew = new node(); pNew-> data = x; pNew -> next = hashtable[index].next; hashtable[index].next = pNew; } } void search(int x) { int flag; int index; index = hash(x); if(hashtable[index].data !=0) { if(hashtable[index].data == x) { flag =1; } } else { node * temp = hashtable[index].next; while(temp != NULL) { if(temp->data !=x) { temp = temp->next; } } } if(flag ==1) { cout<<"Key is Found"<<endl; } else { cout<<"Key Not Found"<<endl; } } void display() { cout<<"index"<<" "<<"data"<<endl; for(int i= 0; i<20;i++) { cout<< setw(5)<<i<<" "<<hashtable[i].data; node * temp = hashtable[i].next; while(temp != NULL) { cout<<setw(5)<<temp->data; temp = temp->next; }cout<<endl; } } }; int main() { node n; hashtab h; while(1) { cout<<" 1.Insert\n 2.Search\n 3.Display"<<endl; int ch ; cout<< "Enter Choice:" << "" ; cin>>ch; if (ch==1) { int n; cout<<"How many elements you have to insert:"<<" "; cin>>n; int key; for(int i =0;i<n;i++) { cout<<"Enter Key:" << ""; cin>>key; h.insert(key); } } else if (ch == 2) { int key; cout<<"Enter Key:" << ""; cin>>key; h.search(key); } else if(ch == 3) { h.display(); } else { break; } } return 0; } OUTPUT: #include <iostream> using namespace std; class Node { private: string title; Node *left; Node *right; public: Node(string title = "null") { this->title = title; this->left = NULL; this->right = NULL; } void display() { cout<<this->title; } friend class Binary_Tree; }; class Binary_Tree { private: Node * root; public: Binary_Tree() { root = NULL; } Binary_Tree(string title) { root = new Node(title); } void display() { if(root == NULL) { cout<<"Empty Book Record"<<endl; return; } cout<<"Book Record for -> "<<root->title<<endl; preOrder(root); } void addChapter(string chapter_name); void addSection(string chapter_name, string section_name); void addSubSection(string chapter_name, string section_name,string sub_section_name); void preOrder(Node *root); Node *SearchLast(Node *start); Node *Search(Node *start, string title); }; Node * Binary_Tree::Search(Node *start, string title) { Node *pCur; pCur = start; if(pCur == NULL) { return NULL; } else { while(pCur!= NULL) { if(pCur->title==title) {//found return pCur; } pCur = pCur->right; } return NULL; } } Node * Binary_Tree::SearchLast(Node *start) { Node *pCur; pCur = start; while(pCur->right != NULL) { pCur = pCur->right; } return pCur; } void Binary_Tree::preOrder(Node *root) { if(root!=NULL) { cout<<root->title<<endl; preOrder(root->left); preOrder(root->right); } } void Binary_Tree::addChapter(string chapter_name) { if(root == NULL) { cout<<"Empty Book Record ---: Failed to add chapter "<<endl; return; } else { Node *lastChapter = NULL,*Chapter; Chapter = root->left; if(Chapter == NULL) { root->left = new Node(chapter_name); cout<<chapter_name<<" First chapter in the book " <<root->title<<" added successfully "<<endl; } else { Chapter = Search(Chapter,chapter_name); if (Chapter == NULL ) { Chapter = root->left; lastChapter = SearchLast(Chapter); lastChapter->right = new Node(chapter_name); cout<<chapter_name<<" Chapter in the book "<<root->title <<" added successfully as a last chapter"<<endl; } else { cout<<"Error : Failed to add Chapter...duplicate chapter name"<<endl; } } } } void Binary_Tree::addSection(string chapter_name, string section_name) { if(root == NULL) { cout<<"Empty Book Record ---: Failed to add Section"<<endl; return; } else { Node *Chapter,*Section,*lastSection; Chapter = root->left; if(Chapter == NULL) { cout<<"No Chapters Present---: Failed to add Section "<<endl; return; } else { Chapter = Search(Chapter,chapter_name); if (Chapter == NULL ) { cout<<"Error : Failed to add Section...chapter name not found"<<endl; } else { Section = Chapter->left; if(Section == NULL) { Chapter->left = new Node(section_name); cout<<section_name<<" First section in the chapter " <<Chapter->title<<" added successfully "<<endl; } else { Section = Search(Section,section_name); if(Section == NULL) { lastSection = SearchLast(Chapter->left); lastSection->right = new Node(section_name); cout<<section_name<<" section in the chapter " <<chapter_name<<" added successfully as a last section"<<endl; } else { cout<<"Error: Duplicate section name "; } } } } } } void Binary_Tree::addSubSection(string chapter_name, string section_name,string sub_section_name) { if(root == NULL) { cout<<"Empty Book Record ---: Failed to add Sub - Section"<<endl; return; } else { Node *Chapter,*Section,*subSection,*lastSubSection; Chapter = root->left; if(Chapter == NULL) { cout<<"No Chapters Present---: Failed to add Sub - Section "<<endl; return; } else { Chapter = Search(Chapter,chapter_name); if (Chapter == NULL ) { cout<<"Error : Failed to add Sub - Section...chapter name " <<chapter_name<<" not found"<<endl; return; } else { Section = Chapter->left; if(Section == NULL) { cout<<"Error : Failed to add Sub-Section...No Sections added in chapter" <<chapter_name<< " yet "<<endl; return; } else { Section = Search(Section,section_name); if(Section == NULL) { cout<<"Error : Failed to add Sub - Section...Section" <<section_name<<" not found "<<endl; return; } else { subSection = Section->left; if(subSection == NULL) { Section->left = new Node(sub_section_name); cout<<sub_section_name<<" First Sub - Section in the Section " <<section_name<<" of "<<chapter_name<<" added successfully " <<endl; } else { subSection = Search(subSection,sub_section_name); if(subSection == NULL) { lastSubSection = SearchLast(Section->left); lastSubSection->right = new Node(sub_section_name); cout<<sub_section_name<<" sub section in the section " <<section_name<<" of chapter" <<chapter_name <<" added successfully as a last section"<<endl; } else { cout<<"Error: Duplicate sub section name "; } } } } } } } } int main() { string book_name; cout<<"Enter the Book Name : ";cin>>book_name; Binary_Tree b(book_name); string chapter_name,section_name,subsection_name; int choice; do{ cout<<"***** M E N U *****"<<endl; cout<<"1. Add Chapter "<<endl<<"2. Add Section "<<endl <<"3. Add Sub-Section"<<endl<<"4. Display "<<endl <<"5. Exit "<<endl; cout<<"Enter your choice : ";cin>>choice; switch(choice) { case 1: cout<<"Enter Chapter Name ";cin>>chapter_name; b.addChapter(chapter_name); break; case 2: cout<<"Enter Chapter Name ";cin>>chapter_name; cout<<"Enter Section Name ";cin>>section_name; b.addSection(chapter_name,section_name); break; case 3: cout<<"Enter Chapter Name ";cin>>chapter_name; cout<<"Enter Section Name ";cin>>section_name; cout<<"Enter Sub-Section Name ";cin>>subsection_name; b.addSubSection(chapter_name,section_name,subsection_name); break; case 4: b.display(); break; default: cout<<"Enter choice between 1..5"<<endl; } }while(choice != 5); return 0; }