SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Hash Table and Heaps
Still With Tree Traversal
void 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)
Heap
bool 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)
Heap
bool 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;
   }
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Homomorphic Encryption
Homomorphic EncryptionHomomorphic Encryption
Homomorphic Encryption
 
Cryptographic hash function md5
Cryptographic hash function md5Cryptographic hash function md5
Cryptographic hash function md5
 
single linked list
single linked listsingle linked list
single linked list
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
MD5
MD5MD5
MD5
 
Birch
BirchBirch
Birch
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Css Ppt
Css PptCss Ppt
Css Ppt
 
Hash Function
Hash FunctionHash Function
Hash Function
 
Public Key Encryption & Hash functions
Public Key Encryption & Hash functionsPublic Key Encryption & Hash functions
Public Key Encryption & Hash functions
 
ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
MD5 ALGORITHM.pptx
MD5 ALGORITHM.pptxMD5 ALGORITHM.pptx
MD5 ALGORITHM.pptx
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 

Ähnlich wie Hash table and heaps

Do the following program in C++- Create a item class... with and i.pdf
Do the following program in C++- Create a item class... with and i.pdfDo the following program in C++- Create a item class... with and i.pdf
Do the following program in C++- Create a item class... with and i.pdfahntagencies
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and SetIntro C# Book
 
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
package edu-ser222-m03_02-   ---  - A binary search tree based impleme.docxpackage edu-ser222-m03_02-   ---  - A binary search tree based impleme.docx
package edu-ser222-m03_02- --- - A binary search tree based impleme.docxfarrahkur54
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfillyasraja7
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docxajoy21
 
Which XXX condition generates the following output Not foun.pdf
Which XXX condition generates the following output Not foun.pdfWhich XXX condition generates the following output Not foun.pdf
Which XXX condition generates the following output Not foun.pdfsolutions6
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfcharanjit1717
 
It is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfIt is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfannaindustries
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonUC San Diego
 

Ähnlich wie Hash table and heaps (20)

Hash function
Hash functionHash function
Hash function
 
Do the following program in C++- Create a item class... with and i.pdf
Do the following program in C++- Create a item class... with and i.pdfDo the following program in C++- Create a item class... with and i.pdf
Do the following program in C++- Create a item class... with and i.pdf
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Array
ArrayArray
Array
 
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
package edu-ser222-m03_02-   ---  - A binary search tree based impleme.docxpackage edu-ser222-m03_02-   ---  - A binary search tree based impleme.docx
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
(C++ exercise) 1.Implement a circular, doubly linked list with a has.docx
 
Which XXX condition generates the following output Not foun.pdf
Which XXX condition generates the following output Not foun.pdfWhich XXX condition generates the following output Not foun.pdf
Which XXX condition generates the following output Not foun.pdf
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Hashing
HashingHashing
Hashing
 
It is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfIt is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdf
 
Heaps
HeapsHeaps
Heaps
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Stacks queues
Stacks queuesStacks queues
Stacks queues
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 

Mehr von Katang Isip

Reflection paper
Reflection paperReflection paper
Reflection paperKatang Isip
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tipsKatang Isip
 
Class list data structure
Class list data structureClass list data structure
Class list data structureKatang Isip
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 

Mehr von Katang Isip (7)

Reflection paper
Reflection paperReflection paper
Reflection paper
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tips
 
3 act story
3 act story3 act story
3 act story
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Time complexity
Time complexityTime complexity
Time complexity
 

Kürzlich hochgeladen

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 

Kürzlich hochgeladen (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

Hash table and heaps

  • 2. Still With Tree Traversal void 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
  • 15. Heap bool 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; }
  • 17. Heap bool 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; } }