Question

Implement a StringHash class using python using open addressing and linear probing, it should contain the following methods: Using strings for the data 1. StringHash(size) — create a new hashTable wit...

Implement a StringHash class using python using open addressing and linear probing, it should contain the following methods:

Using strings for the data

1. StringHash(size) — create a new hashTable with a default size of 11 for your array, but allow the user to specify an alternate size if desired

2. addItem(string) — place value into the hashTable iv. bool PindItem(string value) — return true if the value is present, else false

3. findItem(string value) — return true if the value is present, else false

4. removeItem(string) — same as findItem, but it removes the item if present (see the notes on removing from open addressing)

5. displayTable() — returns the complete hash table, one line per element, empty cells as “_empty_”, deleted items as “_deleted_”, and strings as their value

If the array is over 1/2 full, you should create a new larger array and move the old values to the new array using a revised hash method.

1 0
Add a comment Improve this question Transcribed image text
Answer #1
class StringHash:
    key = 0
    oldvals = None

    # Define The size and initial values are _empty_
    def __init__(self, size):
        self.size = size
        self.vals = ['_empty_'] * self.size

    # Add value into values if values goes to half it will add new array
    def addItem(self, val):
        if self.vals[self.key] == '_empty_':
            self.vals[self.key] = val
            self.key +=1
            if  self.key >= self.size/2:
                self.size += int(self.size/2)
                self.oldvals = self.vals
                self.vals = ['_empty_'] * self.size
                for i in range(len(self.oldvals)):
                    self.vals[i] = self.oldvals[i]

    # find the value in array
    def findItem(self, val):
        flag = False
        if val in self.vals:
            flag = True
        return flag

    # remove the value in array
    def removeItem(self, val):
        if(self.findItem(val)):
            for i in range(len(self.vals)):
                if self.vals[i] == val:
                    self.vals[i] = '_deleted_'

    # display the value in array
    def displayTable(self):
        for i in range(len(self.vals)):
            print(i,":",self.vals[i])
Add a comment
Know the answer?
Add Answer to:
Implement a StringHash class using python using open addressing and linear probing, it should contain the following methods: Using strings for the data 1. StringHash(size) — create a new hashTable wit...
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
  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • Question 8: (a) We create a Hash Table of size 7, using open addressing and linear...

    Question 8: (a) We create a Hash Table of size 7, using open addressing and linear probing with the hash function h(n)=n%7. Write the content of the array after inserting the following values in sequence: 11, 28,46, 7,67, 88,0. (3 marks) . loj [1] [2] [3] [41-5] 问 (b) Explain the issue linear probing has and how it can be addressed. (3 marks)

  • C++: Create a class called "HashTable" This class will implement hashing on an array of integers...

    C++: Create a class called "HashTable" This class will implement hashing on an array of integers Make the table size 11 Implement with linear probing. The hash function should be: h(x) = x % 11 Create search(), insert() and delete() member functions. Search should return the index of where the target is located, insert should allow the user to insert a value, and delete removes the desired value from the table. In main perform the following: 1. Insert: 17 11...

  • True or False? 1. Duplicate hash codes create collisions. 2. In open addressing with linear probing,...

    True or False? 1. Duplicate hash codes create collisions. 2. In open addressing with linear probing, a successful search for an entry that corresponds to a given search key could potentially follow a different probe sequence used to add the entry to the hash table. 3. Completely balance binary trees are not necessarily full. 4. A preorder traversal of a binary tree is an example of a depth-first traversal.

  • Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow...

    Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner; public class HashtableChaining<K, V> {    // Hashtable bucket    private ArrayList<HashNode<K, V>> bucket;    // Current capacity of the array list    private int numBuckets;    // current size of the array list    private int size;       public HashtableChaining(int buckets){        bucket = new ArrayList<>();        numBuckets = buckets;   ...

  • Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create...

    Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended. Methods/Constructor correctly completed (2pt) This is the Map class methods Map(), put(key, value), get(key), isEmpty(), makeEmpty() Private class correctly completed (2pt) This is the Entry class methods Add the methods that are required for this to work correctly when stored in the QuadraticProbingHashTable Tester class (1pt) Show the methods of the Map...

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

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