SlideShare uma empresa Scribd logo
1 de 10
/* 
* C++ Program to Implement Binomial Heap 
*/ 
#include <iostream> 
#include <cstdlib> 
using namespace std; 
/* 
* Node Declaration 
*/ 
struct node 
{ 
int n; 
int degree; 
node* parent; 
node* child; 
node* sibling; 
}; 
/* 
* Class Declaration 
*/ 
class BinomialHeap 
{ 
private: 
node *H; 
node *Hr; 
int count; 
public: 
node* Initializeheap(); 
int Binomial_link(node*, node*); 
node* Create_node(int); 
node* Union(node*, node*); 
node* Insert(node*, node*); 
node* Merge(node*, node*); 
node* Extract_Min(node*); 
int Revert_list(node*); 
int Display(node*); 
node* Search(node*, int); 
int Decrease_key(node*, int, int); 
int Delete(node*, int); 
BinomialHeap() 
{ 
H = Initializeheap(); 
Hr = Initializeheap(); 
int count = 1; 
} 
}; 
/* 
* Initialize Heap 
*/ 
node* BinomialHeap::Initializeheap() 
{ 
node* np; 
np = NULL; 
return np; 
}
/* 
* Linking nodes in Binomial Heap 
*/ 
int BinomialHeap::Binomial_link(node* y, node* z) 
{ 
y->parent = z; 
y->sibling = z->child; 
z->child = y; 
z->degree = z->degree + 1; 
} 
/* 
* Create Nodes in Binomial Heap 
*/ 
node* BinomialHeap::Create_node(int k) 
{ 
node* p = new node; 
p->n = k; 
return p; 
} 
/* 
* Insert Nodes in Binomial Heap 
*/ 
node* BinomialHeap::Insert(node* H, node* x) 
{ 
node* H1 = Initializeheap(); 
x->parent = NULL; 
x->child = NULL; 
x->sibling = NULL; 
x->degree = 0; 
H1 = x; 
H = Union(H, H1); 
return H; 
} 
/* 
* Union Nodes in Binomial Heap 
*/ 
node* BinomialHeap::Union(node* H1, node* H2) 
{ 
node *H = Initializeheap(); 
H = Merge(H1, H2); 
if (H == NULL) 
return H; 
node* prev_x; 
node* next_x; 
node* x; 
prev_x = NULL; 
x = H; 
next_x = x->sibling; 
while (next_x != NULL) 
{ 
if ((x->degree != next_x->degree) || ((next_x->sibling != NULL) 
&& (next_x->sibling)->degree == x->degree)) 
{ 
prev_x = x;
x = next_x; 
} 
else 
{ 
if (x->n <= next_x->n) 
{ 
x->sibling = next_x->sibling; 
Binomial_link(next_x, x); 
} 
else 
{ 
if (prev_x == NULL) 
H = next_x; 
else 
prev_x->sibling = next_x; 
Binomial_link(x, next_x); 
x = next_x; 
} 
} 
next_x = x->sibling; 
} 
return H; 
} 
/* 
* Merge Nodes in Binomial Heap 
*/ 
node* BinomialHeap::Merge(node* H1, node* H2) 
{ 
node* H = Initializeheap(); 
node* y; 
node* z; 
node* a; 
node* b; 
y = H1; 
z = H2; 
if (y != NULL) 
{ 
if (z != NULL) 
{ 
if (y->degree <= z->degree) 
H = y; 
else if (y->degree > z->degree) 
H = z; 
} 
else 
H = y; 
} 
else 
H = z; 
while (y != NULL && z != NULL) 
{ 
if (y->degree < z->degree) 
{ 
y = y->sibling; 
} 
else if (y->degree == z->degree)
{ 
a = y->sibling; 
y->sibling = z; 
y = a; 
} 
else 
{ 
b = z->sibling; 
z->sibling = y; 
z = b; 
} 
} 
return H; 
} 
/* 
* Display Binomial Heap 
*/ 
int BinomialHeap::Display(node* H) 
{ 
if (H == NULL) 
{ 
cout<<"The Heap is empty"<<endl; 
return 0; 
} 
cout<<"The root nodes are: "<<endl; 
node* p; 
p = H; 
while (p != NULL) 
{ 
cout<<p->n; 
if (p->sibling != NULL) 
cout<<"-->"; 
p = p->sibling; 
} 
cout<<endl; 
} 
/* 
* Extract Minimum 
*/ 
node* BinomialHeap::Extract_Min(node* H1) 
{ 
Hr = NULL; 
node* t = NULL; 
node* x = H1; 
if (x == NULL) 
{ 
cout<<"Nothing to Extract"<<endl; 
return x; 
} 
int min = x->n; 
node* p = x; 
while (p->sibling != NULL) 
{ 
if ((p->sibling)->n < min) 
{ 
min = (p->sibling)->n;
t = p; 
x = p->sibling; 
} 
p = p->sibling; 
} 
if (t == NULL && x->sibling == NULL) 
H1 = NULL; 
else if (t == NULL) 
H1 = x->sibling; 
else if (t->sibling == NULL) 
t = NULL; 
else 
t->sibling = x->sibling; 
if (x->child != NULL) 
{ 
Revert_list(x->child); 
(x->child)->sibling = NULL; 
} 
H = Union(H1, Hr); 
return x; 
} 
/* 
* Reverse List 
*/ 
int BinomialHeap::Revert_list(node* y) 
{ 
if (y->sibling != NULL) 
{ 
Revert_list(y->sibling); 
(y->sibling)->sibling = y; 
} 
else 
{ 
Hr = y; 
} 
} 
/* 
* Search Nodes in Binomial Heap 
*/ 
node* BinomialHeap::Search(node* H, int k) 
{ 
node* x = H; 
node* p = NULL; 
if (x->n == k) 
{ 
p = x; 
return p; 
} 
if (x->child != NULL && p == NULL) 
p = Search(x->child, k); 
if (x->sibling != NULL && p == NULL) 
p = Search(x->sibling, k); 
return p; 
} 
/*
* Decrease key of a node 
*/ 
int BinomialHeap::Decrease_key(node* H, int i, int k) 
{ 
int temp; 
node* p; 
node* y; 
node* z; 
p = Search(H, i); 
if (p == NULL) 
{ 
cout<<"Invalid choice of key"<<endl; 
return 0; 
} 
if (k > p->n) 
{ 
cout<<"Error!! New key is greater than current key"<<endl; 
return 0; 
} 
p->n = k; 
y = p; 
z = p->parent; 
while (z != NULL && y->n < z->n) 
{ 
temp = y->n; 
y->n = z->n; 
z->n = temp; 
y = z; 
z = z->parent; 
} 
cout<<"Key reduced successfully"<<endl; 
} 
/* 
* Delete Nodes in Binomial Heap 
*/ 
int BinomialHeap::Delete(node* H, int k) 
{ 
node* np; 
if (H == NULL) 
{ 
cout<<"nHEAP EMPTY!!!!!"; 
return 0; 
} 
Decrease_key(H, k, -1000); 
np = Extract_Min(H); 
if (np != NULL) 
cout<<"Node Deleted Successfully"<<endl; 
} 
/* 
* Main Contains Menu 
*/ 
int main() 
{ 
int n, m, l, i; 
BinomialHeap bh; 
node* p;
node *H; 
H = bh.Initializeheap(); 
char ch; 
while (1) 
{ 
cout<<"----------------------------"<<endl; 
cout<<"Operations on Binomial heap"<<endl; 
cout<<"----------------------------"<<endl; 
cout<<"1)Insert Element in the heap"<<endl; 
cout<<"2)Extract Minimum key node"<<endl; 
cout<<"3)Decrease key of a node"<<endl; 
cout<<"4)Delete a node"<<endl; 
cout<<"5)Display Heap"<<endl; 
cout<<"6)Exit"<<endl; 
cout<<"Enter Your Choice: "; 
cin>>l; 
switch(l) 
{ 
case 1: 
cout<<"Enter the element to be inserted: "; 
cin>>m; 
p = bh.Create_node(m); 
H = bh.Insert(H, p); 
break; 
case 2: 
p = bh.Extract_Min(H); 
if (p != NULL) 
cout<<"The node with minimum key: "<<p->n<<endl; 
else 
cout<<"Heap is empty"<<endl; 
break; 
case 3: 
cout<<"Enter the key to be decreased: "; 
cin>>m; 
cout<<"Enter new key value: "; 
cin>>l; 
bh.Decrease_key(H, m, l); 
break; 
case 4: 
cout<<"Enter the key to be deleted: "; 
cin>>m; 
bh.Delete(H, m); 
break; 
case 5: 
cout<<"The Heap is: "<<endl; 
bh.Display(H); 
break; 
case 6: 
exit(1); 
default: 
cout<<"Wrong Choice"; 
} 
} 
return 0; 
}
$ g++ binomialheap.cpp 
$ a.out 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 1 
Enter the element to be inserted: 9 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 1 
Enter the element to be inserted: 8 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 1 
Enter the element to be inserted: 7 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 1 
Enter the element to be inserted: 6 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node
5)Display Heap 
6)Exit 
Enter Your Choice: 1 
Enter the element to be inserted: 5 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 2 
The node with minimum key: 5 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 4 
Enter the key to be deleted: 6 
Key reduced successfully 
Node Deleted Successfully 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 2 
The node with minimum key: 5 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node 
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 5 
The root nodes are: 
5 
---------------------------- 
Operations on Binomial heap 
---------------------------- 
1)Insert Element in the heap 
2)Extract Minimum key node
3)Decrease key of a node 
4)Delete a node 
5)Display Heap 
6)Exit 
Enter Your Choice: 6 
------------------ 
(program exited with code: 1) 
Press return to continue

Mais conteúdo relacionado

Mais procurados

Encrypt all transports
Encrypt all transportsEncrypt all transports
Encrypt all transportsEleanor McHugh
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RIThe Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RIEleanor McHugh
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giantsIan Barber
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionIan Barber
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful weddingStéphane Wirtel
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional creditRaihan Bin-Mofidul
 
寫程式?那些老師沒教的事
寫程式?那些老師沒教的事寫程式?那些老師沒教的事
寫程式?那些老師沒教的事均民 戴
 
寫程式?那些老師沒教的事 (Git 部分節錄)
寫程式?那些老師沒教的事 (Git 部分節錄)寫程式?那些老師沒教的事 (Git 部分節錄)
寫程式?那些老師沒教的事 (Git 部分節錄)均民 戴
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQAFest
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)Youssef Dirani
 

Mais procurados (20)

Encrypt all transports
Encrypt all transportsEncrypt all transports
Encrypt all transports
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Whispered secrets
Whispered secretsWhispered secrets
Whispered secrets
 
Whispered secrets
Whispered secretsWhispered secrets
Whispered secrets
 
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RIThe Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
 
C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional credit
 
寫程式?那些老師沒教的事
寫程式?那些老師沒教的事寫程式?那些老師沒教的事
寫程式?那些老師沒教的事
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
寫程式?那些老師沒教的事 (Git 部分節錄)
寫程式?那些老師沒教的事 (Git 部分節錄)寫程式?那些老師沒教的事 (Git 部分節錄)
寫程式?那些老師沒教的事 (Git 部分節錄)
 
Asssignment2
Asssignment2 Asssignment2
Asssignment2
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 

Destaque

Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps Naseeba P P
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentationHafsa.Naseem
 
Autismo y edad de diagnóstico
Autismo y edad de diagnósticoAutismo y edad de diagnóstico
Autismo y edad de diagnósticoAndrea Lerchundi
 
Домашняя среда для детей 3-6 лет. Материалы семинара
Домашняя среда для детей 3-6 лет. Материалы семинараДомашняя среда для детей 3-6 лет. Материалы семинара
Домашняя среда для детей 3-6 лет. Материалы семинараSunrise Montessori school
 
Как обустроить дом для ребенка с рождения до 6 лет?
Как обустроить дом для ребенка с рождения до 6 лет?Как обустроить дом для ребенка с рождения до 6 лет?
Как обустроить дом для ребенка с рождения до 6 лет?Sunrise Montessori school
 
Marie Boran PCSTss2015
Marie Boran PCSTss2015Marie Boran PCSTss2015
Marie Boran PCSTss2015Marie Boran
 

Destaque (8)

Leftist heap
Leftist heapLeftist heap
Leftist heap
 
Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps
 
Fibonacci Heap
Fibonacci HeapFibonacci Heap
Fibonacci Heap
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 
Autismo y edad de diagnóstico
Autismo y edad de diagnósticoAutismo y edad de diagnóstico
Autismo y edad de diagnóstico
 
Домашняя среда для детей 3-6 лет. Материалы семинара
Домашняя среда для детей 3-6 лет. Материалы семинараДомашняя среда для детей 3-6 лет. Материалы семинара
Домашняя среда для детей 3-6 лет. Материалы семинара
 
Как обустроить дом для ребенка с рождения до 6 лет?
Как обустроить дом для ребенка с рождения до 6 лет?Как обустроить дом для ребенка с рождения до 6 лет?
Как обустроить дом для ребенка с рождения до 6 лет?
 
Marie Boran PCSTss2015
Marie Boran PCSTss2015Marie Boran PCSTss2015
Marie Boran PCSTss2015
 

Semelhante a Binomial heap

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdffantoosh1
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfamarndsons
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programaMario José
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfaassecuritysystem
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfHIMANSUKUMAR12
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxJason0x0Scottw
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 

Semelhante a Binomial heap (20)

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Opp compile
Opp compileOpp compile
Opp compile
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Modificacion del programa
Modificacion del programaModificacion del programa
Modificacion del programa
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 

Último

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 ConsultingTechSoup
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
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 FellowsMebane Rash
 
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...Association for Project Management
 
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.pptxheathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
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 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Último (20)

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
 
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
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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
 
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...
 
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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Binomial heap

  • 1. /* * C++ Program to Implement Binomial Heap */ #include <iostream> #include <cstdlib> using namespace std; /* * Node Declaration */ struct node { int n; int degree; node* parent; node* child; node* sibling; }; /* * Class Declaration */ class BinomialHeap { private: node *H; node *Hr; int count; public: node* Initializeheap(); int Binomial_link(node*, node*); node* Create_node(int); node* Union(node*, node*); node* Insert(node*, node*); node* Merge(node*, node*); node* Extract_Min(node*); int Revert_list(node*); int Display(node*); node* Search(node*, int); int Decrease_key(node*, int, int); int Delete(node*, int); BinomialHeap() { H = Initializeheap(); Hr = Initializeheap(); int count = 1; } }; /* * Initialize Heap */ node* BinomialHeap::Initializeheap() { node* np; np = NULL; return np; }
  • 2. /* * Linking nodes in Binomial Heap */ int BinomialHeap::Binomial_link(node* y, node* z) { y->parent = z; y->sibling = z->child; z->child = y; z->degree = z->degree + 1; } /* * Create Nodes in Binomial Heap */ node* BinomialHeap::Create_node(int k) { node* p = new node; p->n = k; return p; } /* * Insert Nodes in Binomial Heap */ node* BinomialHeap::Insert(node* H, node* x) { node* H1 = Initializeheap(); x->parent = NULL; x->child = NULL; x->sibling = NULL; x->degree = 0; H1 = x; H = Union(H, H1); return H; } /* * Union Nodes in Binomial Heap */ node* BinomialHeap::Union(node* H1, node* H2) { node *H = Initializeheap(); H = Merge(H1, H2); if (H == NULL) return H; node* prev_x; node* next_x; node* x; prev_x = NULL; x = H; next_x = x->sibling; while (next_x != NULL) { if ((x->degree != next_x->degree) || ((next_x->sibling != NULL) && (next_x->sibling)->degree == x->degree)) { prev_x = x;
  • 3. x = next_x; } else { if (x->n <= next_x->n) { x->sibling = next_x->sibling; Binomial_link(next_x, x); } else { if (prev_x == NULL) H = next_x; else prev_x->sibling = next_x; Binomial_link(x, next_x); x = next_x; } } next_x = x->sibling; } return H; } /* * Merge Nodes in Binomial Heap */ node* BinomialHeap::Merge(node* H1, node* H2) { node* H = Initializeheap(); node* y; node* z; node* a; node* b; y = H1; z = H2; if (y != NULL) { if (z != NULL) { if (y->degree <= z->degree) H = y; else if (y->degree > z->degree) H = z; } else H = y; } else H = z; while (y != NULL && z != NULL) { if (y->degree < z->degree) { y = y->sibling; } else if (y->degree == z->degree)
  • 4. { a = y->sibling; y->sibling = z; y = a; } else { b = z->sibling; z->sibling = y; z = b; } } return H; } /* * Display Binomial Heap */ int BinomialHeap::Display(node* H) { if (H == NULL) { cout<<"The Heap is empty"<<endl; return 0; } cout<<"The root nodes are: "<<endl; node* p; p = H; while (p != NULL) { cout<<p->n; if (p->sibling != NULL) cout<<"-->"; p = p->sibling; } cout<<endl; } /* * Extract Minimum */ node* BinomialHeap::Extract_Min(node* H1) { Hr = NULL; node* t = NULL; node* x = H1; if (x == NULL) { cout<<"Nothing to Extract"<<endl; return x; } int min = x->n; node* p = x; while (p->sibling != NULL) { if ((p->sibling)->n < min) { min = (p->sibling)->n;
  • 5. t = p; x = p->sibling; } p = p->sibling; } if (t == NULL && x->sibling == NULL) H1 = NULL; else if (t == NULL) H1 = x->sibling; else if (t->sibling == NULL) t = NULL; else t->sibling = x->sibling; if (x->child != NULL) { Revert_list(x->child); (x->child)->sibling = NULL; } H = Union(H1, Hr); return x; } /* * Reverse List */ int BinomialHeap::Revert_list(node* y) { if (y->sibling != NULL) { Revert_list(y->sibling); (y->sibling)->sibling = y; } else { Hr = y; } } /* * Search Nodes in Binomial Heap */ node* BinomialHeap::Search(node* H, int k) { node* x = H; node* p = NULL; if (x->n == k) { p = x; return p; } if (x->child != NULL && p == NULL) p = Search(x->child, k); if (x->sibling != NULL && p == NULL) p = Search(x->sibling, k); return p; } /*
  • 6. * Decrease key of a node */ int BinomialHeap::Decrease_key(node* H, int i, int k) { int temp; node* p; node* y; node* z; p = Search(H, i); if (p == NULL) { cout<<"Invalid choice of key"<<endl; return 0; } if (k > p->n) { cout<<"Error!! New key is greater than current key"<<endl; return 0; } p->n = k; y = p; z = p->parent; while (z != NULL && y->n < z->n) { temp = y->n; y->n = z->n; z->n = temp; y = z; z = z->parent; } cout<<"Key reduced successfully"<<endl; } /* * Delete Nodes in Binomial Heap */ int BinomialHeap::Delete(node* H, int k) { node* np; if (H == NULL) { cout<<"nHEAP EMPTY!!!!!"; return 0; } Decrease_key(H, k, -1000); np = Extract_Min(H); if (np != NULL) cout<<"Node Deleted Successfully"<<endl; } /* * Main Contains Menu */ int main() { int n, m, l, i; BinomialHeap bh; node* p;
  • 7. node *H; H = bh.Initializeheap(); char ch; while (1) { cout<<"----------------------------"<<endl; cout<<"Operations on Binomial heap"<<endl; cout<<"----------------------------"<<endl; cout<<"1)Insert Element in the heap"<<endl; cout<<"2)Extract Minimum key node"<<endl; cout<<"3)Decrease key of a node"<<endl; cout<<"4)Delete a node"<<endl; cout<<"5)Display Heap"<<endl; cout<<"6)Exit"<<endl; cout<<"Enter Your Choice: "; cin>>l; switch(l) { case 1: cout<<"Enter the element to be inserted: "; cin>>m; p = bh.Create_node(m); H = bh.Insert(H, p); break; case 2: p = bh.Extract_Min(H); if (p != NULL) cout<<"The node with minimum key: "<<p->n<<endl; else cout<<"Heap is empty"<<endl; break; case 3: cout<<"Enter the key to be decreased: "; cin>>m; cout<<"Enter new key value: "; cin>>l; bh.Decrease_key(H, m, l); break; case 4: cout<<"Enter the key to be deleted: "; cin>>m; bh.Delete(H, m); break; case 5: cout<<"The Heap is: "<<endl; bh.Display(H); break; case 6: exit(1); default: cout<<"Wrong Choice"; } } return 0; }
  • 8. $ g++ binomialheap.cpp $ a.out ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 1 Enter the element to be inserted: 9 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 1 Enter the element to be inserted: 8 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 1 Enter the element to be inserted: 7 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 1 Enter the element to be inserted: 6 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node
  • 9. 5)Display Heap 6)Exit Enter Your Choice: 1 Enter the element to be inserted: 5 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 2 The node with minimum key: 5 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 4 Enter the key to be deleted: 6 Key reduced successfully Node Deleted Successfully ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 2 The node with minimum key: 5 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 5 The root nodes are: 5 ---------------------------- Operations on Binomial heap ---------------------------- 1)Insert Element in the heap 2)Extract Minimum key node
  • 10. 3)Decrease key of a node 4)Delete a node 5)Display Heap 6)Exit Enter Your Choice: 6 ------------------ (program exited with code: 1) Press return to continue