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

Assignment2

The document contains C++ code for implementing a Fibonacci Heap data structure, including node definitions and methods for inserting, extracting the minimum node, and consolidating the heap. Key operations such as linking nodes and maintaining the minimum node are detailed. The main function demonstrates the insertion of keys and extraction of the minimum value, showcasing the heap's functionality.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Assignment2

The document contains C++ code for implementing a Fibonacci Heap data structure, including node definitions and methods for inserting, extracting the minimum node, and consolidating the heap. Key operations such as linking nodes and maintaining the minimum node are detailed. The main function demonstrates the insertion of keys and extraction of the minimum value, showcasing the heap's functionality.

Uploaded by

Vandana Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <iostream>

#include <unordered_map>

#include <cmath>

#include <climits>

#include <vector>

using namespace std;

// Node structure for Fibonacci Heap

struct Node {

int key; // Key value of the node

int degree; // Number of children

bool mark;

Node *parent;

Node *child;

Node *left;

Node *right;

//construt node

Node(int k) {

key = k;

degree = 0;

mark = false;

parent = child = nullptr;

right=left = this;

};

class FibonacciHeap {

private:
Node* minNode; // Pointer to the minimum node

int nodeCount; // Total number of nodes in the heap

// Helper: Link two trees of the same degree

void link(Node* y, Node* x) {

cout << "linking node of " << y->key << " under the node " << x->key << endl;

// Remove y from the root list

y->left->right = y->right;

y->right->left = y->left;

// Make y a child of x

y->parent = x;

y->left = y->right = y;

if (!x->child) {

x->child = y;

} else {

y->left = x->child;

y->right = x-> child->right;

x->child->right ->left = y;

x-> child->right = y;

x->degree = x->degree + 1;

y->mark = false;

}
// Helper: Consolidate trees after extract-min

void consolidate() {

cout << "Consolidating the heap..." << endl;

// Calculate the maximum possible degree

int maxDegree = static_cast<int>(log2(nodeCount)) + 1;

vector<Node*> A(maxDegree, nullptr);

// Collect all nodes in the root list

vector<Node*> rootList;

Node * curr = minNode;

if ( curr) {

do {

rootList.push_back(curr);

curr = curr->right;

} while (curr != minNode);

// Merge trees of the same degree

for (Node* w : rootList) {

Node* x = w;

int d = x->degree;

while (A[d]) {

Node* y = A[d];

cout << "Merging trees with same degree " << d << endl;

if (x->key > y->key) swap(x, y); // Ensure x has the smaller key

link(y, x); // Link y under x

A[d] = nullptr;
d++;

A[d] = x;

// Rebuild the root list and find the new minimum

minNode = nullptr;

for (Node* a : A) {

if (a) {

if (!minNode) {

a->left = a->right = a;

minNode = a;

} else {

a->left = minNode;

a->right = minNode->right;

minNode->right->left = a;

minNode->right = a;

if (a->key < minNode->key) minNode = a;

public:

// Constructor to initialize an empty Fibonacci Heap

FibonacciHeap() : minNode(nullptr), nodeCount(0) {}

// Insert a new key into the heap


void insert(int key) {

cout << "Inserting key: " << key << endl;

Node* node = new Node(key);

if (minNode !=nullptr) {

minNode = node;

} else {

node->left = minNode;

node ->right = minNode->right;

minNode ->right->left = node;

minNode->right = node;

if (key < minNode->key) minNode = node;

nodeCount++;

// Get the minimum value

int getMin() {

cout <<'\n';

return minNode ? minNode->key : INT_MAX; // Return INT_MAX if the heap is empty

// Extract the minimum node

void extractMin() {

if (!minNode) {

cout << "Heap is empty. Nothing to extract." << endl;

return;

Node* z = minNode;
cout << "Extracting minimum node: " << z->key << '\n';

// Add children of z to the root list

if (z->child) {

cout << "adding childrn of node " << z->key << " to raot list" << endl;

Node* x = z->child;

do {

Node* next = x->right;

// Remove x from its sibling list

x->left->right = x->right;

x->right->left = x->left;

// Add x to the root list

x->left = minNode;

x ->right = minNode->right;

minNode ->right->left = x;

minNode-> right = x;

x->parent = nullptr; // Reset parent pointer

x = next;

while (x != z-> child);

// Remove z from the root list

z->left->right = z->right;

z->right->left = z->left;
if (z == z->right) {

minNode = nullptr; // Heap is now empty

} else {

minNode = z->right;

consolidate();

nodeCount--;

delete z;

cout << "Minimum node extracted." << endl;

// Print the root list of the heap

void print() {

cout << "root_list: ";

if (!minNode) {

cout << "empty " << '\n';

Node* curr = minNode;

do {

cout << curr->key << " ";

curr = curr ->right;

} while (curr != minNode);

cout << endl;

};
int main() {

FibonacciHeap heap;

// Insert keys into the heap

heap.insert(23);

heap.insert(17);

heap .insert(13);

heap.insert(27);

heap.insert(29);

// Display the minimum key

cout << "Minimum: " << heap.getMin() << endl;

heap.print();

cout << "\nExtracting Min...\n"; // Extract the minimum key

heap.extractMin();

// Print the root list after extraction

heap.print();

cout << "New Minimum: " << heap.getMin() << endl;

return 0;

You might also like