CS 225 Data Structures Oc October 28 28 Ha Hashing Analysis G - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Oc October 28 28 Ha Hashing Analysis G - - PowerPoint PPT Presentation

CS 225 Data Structures Oc October 28 28 Ha Hashing Analysis G G Carl Evans Running g Times The expected number of probes for find(key) under SUHA Linear Probing: Successful: (1 + 1/(1-)) Unsuccessful: (1 + 1/(1-)) 2


slide-1
SLIDE 1

CS 225

Data Structures

Oc October 28 28 – Ha Hashing Analysis

G G Carl Evans

slide-2
SLIDE 2

Running g Times

Linear Probing:

  • Successful: ½(1 + 1/(1-α))
  • Unsuccessful: ½(1 + 1/(1-α))2

Double Hashing:

  • Successful: 1/α * ln(1/(1-α))
  • Unsuccessful: 1/(1-α)

The expected number of probes for find(key) under SUHA

slide-3
SLIDE 3

Re ReHashing

What if the array fills?

slide-4
SLIDE 4

Which collision resolution strategy is better?

  • Big Records:
  • Structure Speed:

What structure do hash tables replace? What constraint exists on hashing that doesn’t exist with BSTs? Why talk about BSTs at all?

slide-5
SLIDE 5

Running g Times

Hash Table AVL Linked List

Find

SUHA: Worst Case:

Insert

SUHA: Worst Case:

Storage Space

slide-6
SLIDE 6

st std da data struc uctur ures

std::map

slide-7
SLIDE 7

st std da data struc uctur ures

std::map ::operator[] ::insert ::erase ::lower_bound(key) è Iterator to first element ≤ key ::upper_bound(key) è Iterator to first element > key

slide-8
SLIDE 8

st std da data struc uctur ures

std::unordered_map ::operator[] ::insert ::erase ::lower_bound(key) è Iterator to first element ≤ key ::upper_bound(key) è Iterator to first element > key

slide-9
SLIDE 9

st std da data struc uctur ures

std::unordered_map ::operator[] ::insert ::erase ::lower_bound(key) è Iterator to first element ≤ key ::upper_bound(key) è Iterator to first element > key ::load_factor() ::max_load_factor(ml) è Sets the max load factor

slide-10
SLIDE 10

Se Secret, My Mystery D Data St Stru ructure

ADT: insert remove isEmpty

slide-11
SLIDE 11

Pr Priority Queue Implementat ation

insert removeMin

O(n) O(n) O(1) O(n) O( lg(n) ) O(1) O( lg(n) ) O(1)

unsorted sorted

slide-12
SLIDE 12

An Another possibly structure…

5 15 9 25 4 6 7 20 11 16 12 14

slide-13
SLIDE 13

(m (min)H )Heap

5 15 9 25 4 6 7 20 11 16 12 14

A complete binary tree T is a min-heap if:

  • T = {} or
  • T = {r, TL, TR}, where r is

less than the roots of {TL, TR} and {TL, TR} are min-heaps.

slide-14
SLIDE 14

(m (min)H )Heap

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-15
SLIDE 15

in inser ert

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-16
SLIDE 16

template <class T> void Heap<T>::_insert(const T & key) { // Check to ensure there’s space to insert an element // ...if not, grow the array if ( size_ == capacity_ ) { _growArray(); } // Insert the new element at the end of the array item_[++size] = key; // Restore the heap property _heapifyUp(size); } 1 2 3 4 5 6 7 8 9 10 11 12

in inser ert

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-17
SLIDE 17

gr growArray

5 15 9 25 4 6 7 20 11 16 12 14

slide-18
SLIDE 18

template <class T> void Heap<T>::_heapifyUp( _________________ ) { if ( index > _________ ) { if ( item_[index] < item_[ parent(index) ] ) { std::swap( item_[index], item_[ parent(index) ] ); _heapifyUp( ________________ ); } } } 1 2 3 4 5 6 7 8 9

in inser ert t - he heapi pifyUp

template <class T> void Heap<T>::_insert(const T & key) { // Check to ensure there’s space to insert an element // ...if not, grow the array if ( size_ == capacity_ ) { _growArray(); } // Insert the new element at the end of the array item_[++size] = key; // Restore the heap property _heapifyUp(size); } 1 2 3 4 5 6 7 8 9 10 11 12

slide-19
SLIDE 19

re removeMin

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-20
SLIDE 20

template <class T> void Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_]; size--; // Restore the heap property heapifyDown(); // Return the minimum value return minValue; } 1 2 3 4 5 6 7 8 9 10 11 12 13

re removeMin

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-21
SLIDE 21

template <class T> void Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_]; size--; // Restore the heap property _heapifyDown(); // Return the minimum value return minValue; } template <class T> void Heap<T>::_heapifyDown(int index) { if ( !_isLeaf(index) ) { T minChildIndex = _minChild(index); if ( item_[index] ___ item_[minChildIndex] ) { std::swap( item_[index], item_[minChildIndex] ); _heapifyDown( ________________ ); } } } 1 2 3 4 5 6 7 8 9 10

re removeMin - he heapi pifyDown wn

1 2 3 4 5 6 7 8 9 10 11 12 13

slide-22
SLIDE 22

Ar Array Ab Abstractions