SlideShare une entreprise Scribd logo
1  sur  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;
   }
}

Contenu connexe

Tendances

Tendances (20)

Interval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision makingInterval Type-2 fuzzy decision making
Interval Type-2 fuzzy decision making
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
07 regularization
07 regularization07 regularization
07 regularization
 
Newton's forward & backward interpolation
Newton's forward & backward interpolationNewton's forward & backward interpolation
Newton's forward & backward interpolation
 
K nearest neighbor
K nearest neighborK nearest neighbor
K nearest neighbor
 
The comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithmThe comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithm
 
Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Newton's forward difference
Newton's forward differenceNewton's forward difference
Newton's forward difference
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Fixed-point arithmetic
Fixed-point arithmeticFixed-point arithmetic
Fixed-point arithmetic
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)
 
Roll's theorem
Roll's theoremRoll's theorem
Roll's theorem
 
Lecture 6 radial basis-function_network
Lecture 6 radial basis-function_networkLecture 6 radial basis-function_network
Lecture 6 radial basis-function_network
 
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
Goodfellow, Bengio, Couville (2016) "Deep Learning", Chap. 7
 
Clustering
ClusteringClustering
Clustering
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Hashing
HashingHashing
Hashing
 
GAN - Theory and Applications
GAN - Theory and ApplicationsGAN - Theory and Applications
GAN - Theory and Applications
 
Chapter 5 - Fuzzy Logic
Chapter 5 - Fuzzy LogicChapter 5 - Fuzzy Logic
Chapter 5 - Fuzzy Logic
 

Similaire à 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.pdf
ahntagencies
 
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
farrahkur54
 
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
illyasraja7
 
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
charanjit1717
 
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
annaindustries
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
Data structures lab
Data structures labData structures lab
Data structures lab
Ragu Ram
 

Similaire à 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
 
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
 
Data structures lab
Data structures labData structures lab
Data structures lab
 

Plus de 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
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
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
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

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; } }