Lecture 012

Array Usage:

  1. as a set (adding, searching, for-loop)
  2. as a dictionary (insert, update, loop up)

Dictionary:

Dictionary

Dictionary

Self-Resizing Array Complexity unsorted sorted by key sorted by data
look up O(n) O(log n) O(n)
insert O(1) amortized O(n) O(1)

(Operations are fast when we know where to loop)

For a dictionary of capacity k, we mod our key by k first. If keys conflict, we can look up the value if the right key is in value.

Collision

Open Addressing - more common

linear probing: use next free index quadratic probing: try index +1, +4, and +9... (collisions are unavoidable)

Chaining (Separate Chains)

We store a linked list (chain) in each index

Resizing

Worst case: look up O(n)

Best case layout

Amortized Cost for Add and Resizing

Hashing (Hash Dictionary)

To get the best possible layout

Random Hash

Random Hash

Hash Table

Hash Table

Hash Table Complexity

Hash Table Complexity

Hash Set

Generic Data Structure (Generic Pointer)

Solution in C0

Client Interface // typedef ____ elem; input by the client, however, the definition need to be compiled before the library

C1

Generic Pointer in C1

new pointer type: void*

Generic vs. Specific

Generic vs. Specific

Cannot dereference, allocate, and wrong cast

Cannot dereference, allocate, and wrong cast

NULL

//@assert \hastag(void*,pv); will always be false

Tags: C1 check safety by maintaining tags

Casting diagram

Diagram

Diagram

Table of Content