0% found this document useful (0 votes)
0 views

Assignment1

The document contains C++ code for implementing a binomial heap data structure, including functions for creating nodes, merging two binomial heaps, and printing the heap structure. It defines the structure of a node, merges two root lists, and performs a union operation on two binomial heaps. The main function demonstrates the creation of two heaps and their union, printing the results to the console.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Assignment1

The document contains C++ code for implementing a binomial heap data structure, including functions for creating nodes, merging two binomial heaps, and printing the heap structure. It defines the structure of a node, merges two root lists, and performs a union operation on two binomial heaps. The main function demonstrates the creation of two heaps and their union, printing the results to the console.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <iostream>

using namespace std;

// Structure of a node

struct Node {

int key;

int degree;

Node *parent;

Node*sibling;

Node*child;

Node(int c) {

key = c;

degree = 0;

parent = nullptr;

sibling = nullptr;

child = nullptr;

};

// Link two binomial trees of same degree

void binomial_Link(Node* y, Node* z) {

y->parent = z;

y->sibling = z->child;

z->child = y;

z->degree++;

// Merge two root list


Node* bionomialmerge(Node*h1,Node*h2){

if(!h1)return h2;

if(!h2)return h1;

Node*mergeHead = nullptr;

Node*tail = mergeHead;

while (h1 && h2){

Node * current = nullptr;

if(h1 <= h2){

current = h1;

h1 = h1->sibling;

else{

current = h2;

h2 = h2->sibling;

if(mergeHead == nullptr){

mergeHead = current;

tail = current;

else {tail->sibling = current;

tail = current;}

if(h1 != nullptr) {

tail->sibling = h1;

else if(h2!=nullptr){
tail->sibling = h2;

return mergeHead;

// Union of two binomial heaps

Node* binomial_Heapunion(Node* h1, Node* h2) {

Node* newHead = bionomialmerge(h1, h2);

if (!newHead) return nullptr;

Node *prev = nullptr, *curr = newHead, *next = curr->sibling;

while (next) {

if ((curr->degree != next->degree) ||

(next->sibling && next->sibling->degree == curr->degree)) {

prev = curr;

curr = next;

} else {

if (curr->key <= next->key) {

curr->sibling = next->sibling;

binomial_Link(next, curr);

} else {

if (prev) {

prev->sibling = next;

} else {

newHead = next;

binomial_Link(curr, next);
curr = next;

next = curr->sibling;

return newHead;

// Print the binomial heap tree

void printTree(Node* h, int indent = 0) {

while (h) {

for (int i = 0; i < indent; i++) cout << " ";

cout << h->key << "\n";

if (h->child)

printTree(h->child, indent + 4);

h = h->sibling;

// Create heap H1 as per the diagram

Node* createHeapH1() {

Node* n12 = new Node(12);

Node* n7 = new Node(72);

Node* n15 = new Node(85);

Node* n25 = new Node(85);

Node* n28 = new Node(88);

Node* n33 = new Node(33);


Node* n41 = new Node(41);

// Build tree structure for H1

n7->child = n25;

n25->parent = n7;

n15->child = n28;

n28->parent = n15;

n28->sibling = n33;

n33->parent = n15;

n28->child = n41;

n41->parent = n28;

n12->sibling = n7;

n7->sibling = n15;

return n12;

// Create heap H2 as per the diagram

Node* createHeapH2() {

Node* n18 = new Node(18);

Node* n3 = new Node(3);

Node* n6 = new Node(6);

Node* n37 = new Node(37);

Node* n30 = new Node(30);

Node* n45 = new Node(45);

Node* n55 = new Node(55);


Node* n32 = new Node(72);

Node* n23 = new Node(23);

Node* n22 = new Node(82);

Node* n24 = new Node(24);

Node* n48 = new Node(28);

Node* n50 = new Node(50);

Node* n8 = new Node(8);

Node* n29 = new Node(29);

Node* n31 = new Node(41);

Node* n10 = new Node(40);

Node* n17 = new Node(57);

Node* n44 = new Node(44);

// Build structure for 3-rooted tree

n3->child = n37;

n37->parent = n3;

n37->sibling = n30;

n30->parent = n3;

n30->child = n45;

n45->parent = n30;

n45->child = n55;

n55->parent = n45;

n30->sibling = n32;

n32->parent = n3;

// Build structure for 6-rooted tree

n6->child = n8;

n8->parent = n6;
n8->sibling = n29;

n29->parent = n6;

n29->sibling = n10;

n10->parent = n6;

n10->sibling = n44;

n44->parent = n6;

n8->child = n23;

n23->parent = n8;

n23->sibling = n22;

n22->parent = n8;

n22->sibling = n48;

n48->parent = n8;

n23->sibling->sibling = n50;

n50->parent = n8;

n23->sibling->sibling->sibling = n24;

n24->parent = n8;

n29->child = n31;

n31->parent = n29;

n31->sibling = n17;

n17->parent = n29;

n18->sibling = n3;

n3->sibling = n6;

return n18;

}
int main() {

Node* H1 = createHeapH1();

Node* H2 = createHeapH2();

cout << "Heap H1:\n";

printTree(H1);

cout << "\nHeap H2:\n";

printTree(H2);

Node* H = binomial_Heapunion(H1, H2);

cout << "\nUnion Heap:\n";

printTree(H);

return 0;

You might also like