Hash table and heaps

3,016
-1

Published on

Basic info about hash and heaping techniques

Published in: Education, Technology, Business
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
3,016
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
34
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Hash table and heaps

  1. 1. Hash Table and Heaps
  2. 2. Still With Tree Traversalvoid BST::levelOrder(){ Queue<node*> q; q.enqueue(root); while(!q.isEmpty()){ node *p = q.dequeue(); cout<<p->item<<“ “; if(p->left!=NULL) q.enqueue(p->left); if(p->right!=NULL) q.enqueue(p->right); }}
  3. 3. Still On SearchThrough the AVL, search was improved to log n. But we perform better?Hash Table Hash function/ Hashing An array of some fixed containing the keys Each key is mapped into some number in range 0 to H_SIZE-1 This mapping is called a hash function
  4. 4. Hash Table 10 2 4 8
  5. 5. Hash TableHash Functions item % H_SIZE Problematic Might run out of space (array) An item might already exist (collision) Solutions Closed hashing (linear probing) Open hashing
  6. 6. Hash Table 10 12 34 4 14 74 8 18
  7. 7. Hash TableClosed hashing pos = x % H_SIZE; while(??){ pos++; } Items[pos] = x;
  8. 8. Hash TableThe keys of the hash table are normally stringsHash functions Sum up the ASCII codes of the characters before performing % (st[0] + 27*st[1] + 729*st[2]) % H_SIZE
  9. 9. Hash Table Open hashing  Array of linked-lists
  10. 10. Priority Queue (Heap)A data structure allows at least the following two operations insert – similar to enqueue deleteMin/deleteMax – heap’s equivalent of dequeue Implementation Represent as a binary tree that is completely filled, with the exception of the bottom level Completely filled from left to right Simplest way is to use an array
  11. 11. Heap
  12. 12. HeapHeap order property Every node X, the key in the parent of X is smaller (or equal to) the key in X With the exception of the root (which has no parent) min-heap
  13. 13. Heap
  14. 14. HeapInsertion (refer to board)
  15. 15. Heapbool Heap::insert(int x){ if(isFull()) return false; else{ int i = size++; while(items[i/2]>x){ items[i] = items[i/2]; i/=2; } items[i] = x; }
  16. 16. HeapdeleteMin (refer to board)
  17. 17. Heapbool Heap::deleteMin(){ if(isEmpty()) return false; else{ last = items[size--]; for(int i=1; i*2<=size; i=child){ child = i*2; if(child!=size && items[child+1] < items[child]) child++; if(last > items[child]) items[i] = items[child]; else break; } return true; }}

×