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

Hash table and heaps

on

  • 2,550 views

Basic info about hash and heaping techniques

Basic info about hash and heaping techniques

Statistics

Views

Total Views
2,550
Views on SlideShare
2,550
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
1

0 Embeds 0

No embeds

Accessibility

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.

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

Hash table and heaps Hash table and heaps Presentation Transcript

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