Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HashTable

Hash Table data structure

Hierarchy

  • HashTable

Constructors

Properties

Methods

Constructors

constructor

  • new HashTable(tableSize?: number): HashTable
  • Parameters

    • tableSize: number = ...

      size of the hash table

    Returns HashTable

Properties

buckets

buckets: LinkedList[]
property

Array of buckets or slots

keys

keys: object
property

A map of key -> index in the bucket for fast lookup

Type declaration

  • [x: string]: number

Methods

delete

  • delete(key: string): any
  • Parameters

    • key: string

    Returns any

get

  • get(key: string): any
  • Parameters

    • key: string

    Returns any

getKeys

  • getKeys(): string[]
  • Returns string[]

has

  • has(key: string): boolean
  • Parameters

    • key: string

    Returns boolean

hash

  • hash(key: string): number
  • POLYNOMIAL STRING HASHING key(string input) -> hash -> index

    hash(str) = [(str.charCodeAt(0) * P^(n-1)) + (str.charCodeAt(1) * P^(n-2)) + ... ] % M

    P => any prime number, roughly equal to the total no of characters used in the key * e.g. if the key only uses lower-case alphabets => prime number closer to 26 => 31

    M => usually the size of bucket list, because probability of two strings colliding must be inversely proportional to M

    n => length of the key

    Parameters

    • key: string

    Returns number

set

  • Parameters

    • key: string
    • value: any

    Returns HashTable