Data Structures: Linked Lists and Hash Tables

Linked Lists

Linked Lists

This is a visual representation of a linked list using int values

Linked Lists vs Arrays

This is a visual representation of a linked list using int values

Why would we want to use it instead of an Array?

Operations with LinkedLists


In big O notation

Operations in a LinkedList have a running time of ==> O(n)

Operations with Arrays



How can we get both fast access and variable size?

If access is fast on an array, can we do something similar for search, insertion and deletion?

Hash tables

Hash tables

You can think of hash tables as doing the inverse operation that you do with an array.

In an array you use an value (e.g. an int number) and you get a key (e.g. a String)

In a HashTable you use a key (e.g. a String ) to get an value (e.g. an int number)

Hash tables

Hash tables will allow us to combine some good things about arrays, with some good things about LinkedLists

In a HashTable you use a key (e.g. a String ) to get an value (e.g. an int number)

To do this we will use a hashing function

Hashing function

A hashing function maps input data of arbitrary size to output data of fixed size

We call the input a key. We call the output a Hash Value

We can use the has value as an index to find data, e.g., in an array

Hashing function

A hashing function maps input data of arbitrary size to output data of fixed size

Arbitrary size: Person names in English, Fixed Size: Groups of names by their first letter

Implementing a HashTable

A hash table will consist of a collection of entries, a hashing function, and methods to insert and remove data from it

We will use an array to store the entries in our data structure

Implementing a HashTable

A hash table will consist of an collection of entries, a hashing function, and methods to insert and remove data from it


We use an array to store the entries in our data structure

Collision in Hash Tables

When two elements have the same has value, we will have to put them in the same entry

We call this situation a collision

How do we deal with collisions

Hash tables with multiple elements per entry

Replace each entry in the hash table with a LinkedList

Hash tables with multiple elements per entry

Replace each entry in the hash table with a LinkedList

Hash tables with multiple elements per entry

Replace each entry in the hash table with a LinkedList

Some remarks about Hash tables



In big O notation

Operations in a Hash Table with n elements have a running time of ==> O(k)

  • k is size of the longest LinkedList in the HashTable.


Operations in a Hash Table with n elements have a running time of ==> O(1)

  • When k is considerably smaller than n

You can view a HashTable as a Dictionary

We map names ( keys ) to Nodes in the HashTable ( values )

But you could use this data structure to map arbitrary types of data

  • Names to phone numbers
  • Phone numbers to names
  • English words to their definitions
  • English words to words in French
  • Musical note letters to their pitch frequency

HashTables in Java

Java has a generic implementation of a HashTable

To try for yourself

Try to implement an English to French dictionary using a HashTable<String,String> object

Resources

/