cs 225
play

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


  1. CS 225 Data Structures Oc October 28 28 – Ha Hashing Analysis G G Carl Evans

  2. Running g Times The expected number of probes for find(key) under SUHA Linear Probing: • Successful: ½(1 + 1/(1-α)) • Unsuccessful: ½(1 + 1/(1-α)) 2 Double Hashing: • Successful: 1/α * ln(1/(1-α)) • Unsuccessful: 1/(1-α)

  3. Re ReHashing What if the array fills?

  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?

  5. Running g Times Hash Table AVL Linked List SUHA: Find Worst Case: SUHA: Insert Worst Case: Storage Space

  6. st std da data struc uctur ures std::map

  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

  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

  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

  10. Se Secret, My Mystery D Data St Stru ructure ADT: insert remove isEmpty

  11. Pr Priority Queue Implementat ation insert removeMin O(n) O(n) unsorted O(1) O(n) O( lg(n) ) O(1) sorted O( lg(n) ) O(1)

  12. An Another possibly structure… 4 5 6 15 9 7 20 16 25 11 14 12

  13. (m (min)H )Heap 4 A complete binary tree T 5 6 is a min-heap if: 15 9 7 20 • T = {} or • T = {r, T L , T R } , where r is 16 25 11 14 12 less than the roots of { T L , T R } and { T L , T R } are min-heaps.

  14. (m (min)H )Heap 4 5 6 15 9 7 20 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11

  15. in inser ert 4 5 6 15 9 7 20 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11

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

  17. gr growArray 4 5 6 15 9 7 20 16 25 11 14 12

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

  19. re removeMin 4 5 6 15 9 7 20 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11

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

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

  22. Ar Array Ab Abstractions

Recommend


More recommend