Hash table and heaps
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


Hash table and heaps



Basic info about hash and heaping techniques

Basic info about hash and heaping techniques



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • aaaa
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Hash table and heaps Presentation Transcript

  • 1. Hash Table and Heaps
  • 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. 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. Hash Table 10 2 4 8
  • 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. Hash Table 10 12 34 4 14 74 8 18
  • 7. Hash TableClosed hashing pos = x % H_SIZE; while(??){ pos++; } Items[pos] = x;
  • 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. Hash Table Open hashing  Array of linked-lists
  • 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. Heap
  • 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. Heap
  • 14. HeapInsertion (refer to board)
  • 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. HeapdeleteMin (refer to board)
  • 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; }}