0% found this document useful (0 votes)
4 views3 pages

Heap Implementation

The document contains C code for implementing a min-heap data structure using a custom node type. It includes functions for swapping nodes, heapifying, building the heap, inserting new nodes, removing the minimum node, and printing the heap. The main function demonstrates the creation of a heap with sample nodes and performs operations such as building the heap, inserting a new node, and removing the minimum node.

Uploaded by

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

Heap Implementation

The document contains C code for implementing a min-heap data structure using a custom node type. It includes functions for swapping nodes, heapifying, building the heap, inserting new nodes, removing the minimum node, and printing the heap. The main function demonstrates the creation of a heap with sample nodes and performs operations such as building the heap, inserting a new node, and removing the minimum node.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

typedef struct node {


struct node *left; // Pointer to left child
struct node *right; // Pointer to right child
struct node *parent; // Pointer to parent
int weight; // Weight (priority) of node
short symbol; // Symbol at leaf node
} NODE;

NODE nodes[6];
// Function to swap two nodes
void swap(NODE *a, NODE *b) {
NODE temp = *a;
*a = *b;
*b = temp;
}

// Function to heapify a subtree rooted with node i


void minHeapify(int n, int i) {
int smallest = i; // Initialize smallest as root
int left = 2 * i + 1; // Left child
int right = 2 * i + 2; // Right child

// If left child is smaller than root


if (left < n && (nodes + left)->weight < (nodes + smallest)->weight)
smallest = left;

// If right child is smaller than current smallest


if (right < n && (nodes + right)->weight < (nodes + smallest)->weight)
smallest = right;

// If smallest is not root


if (smallest != i) {
swap((nodes + i), (nodes + smallest));
// Recursively heapify the affected sub-tree
minHeapify(n, smallest);
}
}

// Function to build a heap


void buildMinHeap(int n) {
// Index of the last non-leaf node
int i = (n - 1) / 2;

// Perform reverse level order traversal


// and heapify each node
for (i; i >= 0; i--) {
minHeapify(n, i);
}
}

// Function to insert a new node into the heap


void insertIntoHeap(int *n, NODE element) {
(*n)++;
int i = *n - 1;
*(nodes+i) = element;
// Fix the min heap property if it is violated
while (i != 0 && (nodes + ((i - 1) / 2))->weight > (nodes + i)->weight) {
swap((nodes + i), (nodes + ((i - 1) / 2)));
i = (i - 1) / 2;
}
}

// Function to remove the minimum node from the heap


NODE removeFromHeap(int *n) {
if (*n <= 0)
return (NODE){NULL, NULL, NULL, 0, 0}; // Heap is empty

if (*n == 1) {
(*n)--;
return *(nodes);
}

// Store the minimum node, and remove it from heap


NODE root = *(nodes);
*(nodes) = *(nodes + (*n - 1));
(*n)--;
minHeapify(*n, 0);

return root;
}

// Function to print the elements of the heap


void printHeap(int n) {
printf("Heap: ");
for (int i = 0; i < n; i++)
printf("(%d, %d) ", (nodes + i)->weight, (nodes + i)->symbol);
printf("\n");
}

int main() {

int n = sizeof(nodes) / sizeof(nodes[0]);

// Sample nodes
NODE node1 = {NULL, NULL, NULL, 5, 65};
NODE node2 = {NULL, NULL, NULL, 6, 66};
NODE node3 = {NULL, NULL, NULL, 4, 67};
NODE node4 = {NULL, NULL, NULL, 3, 68};
NODE node5 = {NULL, NULL, NULL, 1, 69};
NODE node6 = {NULL, NULL, NULL, 2, 70};

nodes[0] = node1;
nodes[1] = node2;
nodes[2] = node3;
nodes[3] = node4;
nodes[4] = node5;
nodes[5] = node6;

printf("Original Array: ");


printHeap(n);

// Build heap
buildMinHeap(n);
printf("Min Heap: ");
printHeap(n);

// Sample new node to insert


NODE newNode = {NULL, NULL, NULL, 0, 71};
insertIntoHeap(&n, newNode);
printf("After inserting (0, 71): ");
printHeap(n);

// Remove minimum node


NODE minNode = removeFromHeap(&n);
printf("Removed minimum node: (%d, %d)\n", minNode.weight, minNode.symbol);
printf("Heap after removal: ");
printHeap(n);

return 0;
}

what I got: 0x2c = 0010 1100


WhatI need: 0x16 = 0001 0110

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy