0

Hi I know it's bascially a hash table, but since it ask for iterate time is O(number of elements) rather than O(size of array). Any thoughts?

Detail requirements:

  1. Use array-like data strcutre only
  2. Achieve the below time compleixty:

Add:O(1) time

Delete: O(1) time

Search: O(1) time

Clear: O(1) time

Iterate: O(number of elements)

Below is my solution:

  public class myDS
    {
        public LinkedList<int> idxArr;
        public HashEntry[] eleArr;
        private int capacity = 251; //Some random prime number

        //Constructor
    public myDS()
    {
        eleArr = new HashEntry[capacity];
        /*for (int i = 0; i < capacity; i++) Not sure if we need it.
        {
            eleArr[i] = null;
        }*/
        idxArr = new LinkedList<int>();
    }

    //Add
    public void Add(int key, int value)
    {
        int hash = key % capacity;
        while (eleArr[hash] != null && eleArr[hash].GetKey() != key)
        {
            hash = (hash + 1) % capacity;
        }
        eleArr[hash] = new HashEntry(key, value);
        idxArr.AddFirst(hash);
    }
    //Search
    public int Get(int key)
    {
        int hash = key % capacity;
        while (eleArr[hash] != null && eleArr[hash].GetKey() != key)
        {
            hash = (hash + 1) % capacity;
        }
        //Not exist
        if (eleArr[hash] == null)
            return -1;
        else
            return eleArr[hash].GetValue();
    }

    //Clear, is it a true O(1) time?
    public void Clear()
    {
        var newEleArr = new HashEntry[capacity];
        var newIdxArr = new LinkedList<int>();

        eleArr = newEleArr;
        idxArr = newIdxArr;
    }

    //Delete
    public void Delete(int key)
    {
        int hash = key % capacity;
        eleArr[hash] = null;
        //How to delete a element in the linked list in O(1) time??

    }
    //Iterator
    public void Iterate()
    {
        var idxEnumerator = idxArr.GetEnumerator();

        while (idxEnumerator.MoveNext() && idxEnumerator.Current != null)
        {
            int idx = idxEnumerator.Current;
            if (eleArr[idx] != null)
            {
                Console.WriteLine("Key: " + eleArr[idx].GetKey() + "Value: " + eleArr[idx].GetValue());
            }
        }
    }
}
Kevman
  • 260
  • 2
  • 15
  • 2
    In a hash table implemented using arrays, `size of array` should be directly proportional to `number of elements` (typically within 2x), so the Big-O should be identical. – Jedi Jun 23 '17 at 02:43
  • @Jedi But in that case, every time we add an element, we should resize the array, which leads us a non-O(1) time. – Kevman Jun 23 '17 at 02:55
  • 1
    True. Hash tables don't offer true O(1), rather amortized O(1): https://stackoverflow.com/questions/3949217/time-complexity-of-hash-table and http://www.cs.cornell.edu/courses/cs312/2008sp/lectures/lec20.html – Jedi Jun 23 '17 at 02:57
  • @Jedi Any idea on my Delete method? I meet a problem there. – Kevman Jun 23 '17 at 03:30
  • @Kevman We should not resize array every time - only when occupancy becomes too high – MBo Jun 23 '17 at 06:37

0 Answers0