【自制演示代码】Basic Operations on a Binary Min-Heap

本文介绍了二叉最小堆的基本属性,如结构和堆序,以及在C语言中实现的插入、删除最小值等操作。代码展示了如何使用堆对输入序列进行排序,同时提供了注意事项和函数定义。
摘要由CSDN通过智能技术生成

Properties of a Binary Min-Heap

1. Structure Property

A binary min-heap has to be a complete binary tree.

2. Heap Order Property

The value of any node in a binary min-heap has to be no greater than its two children's values(if exists).

Operations Included in the Code

In the following code, you will see the operations below:

- BuildHeap

- PercolateDown

- PercolateUp

- Insert

- DeleteMin

- PrintHeap

Also, you will see the simple typedef of:

- Heap

Notes and Declarations

The code will sort the input sequence with a heap.

The language used is C.

The purpose of posting this code online is to make a quick note of my studying progress.

For students at school, DO NOT copy this code into your homework or projects. Otherwise, I will not be responsible for any punishments because of this.

Some bugs might be included in the code. The author(I) will NOT be responsible for any results brought out from this code.

Code

/* Operations on a Binary Min Heap */
#include <stdio.h>
#include <stdlib.h>

#define MAXN_SIZE 128

typedef struct Heap
{
	int Size;
	int *Elements;
}Heap;

void PercolateDown( Heap *H, int index );
void PercolateUp( Heap *H, int index );
void Insert( Heap *H, int value );
int DeleteMin( Heap *H );
Heap BuildHeap( int size, int *seq );
void PrintHeap( Heap H );

int main()
{
	printf("SORT BY HEAP\n");
	printf("To build a heap, how many numbers do you want?\n>>>");
	
	int size = 0;
	int i = 0;
	
	scanf("%d", &size);
	
	int *seq = (int *)malloc(sizeof(int *) * size);
	
	printf("Please input your numbers:\n>>>");
	
	for (i = 0; i < size; i++)
	{
		scanf("%d", &seq[i]);
	}
	
	Heap heap = BuildHeap(size, seq);
	
	printf("Heap built!\n");
	
	while (heap.Size > 0)
	{
		//PrintHeap(heap);
		printf("%d ", DeleteMin(&heap));
	}
	
	return 0;
	
}


void PercolateDown( Heap *H, int index )
{
	int i = 0, LastElement = H->Elements[index];
	int Child = 0;
	
	for (i = index; i * 2 <= H->Size; i = Child)
	{
		Child = i * 2;
		if (Child != H->Size && H->Elements[Child + 1] < H->Elements[Child])
		{
			// Right child exists and is smaller than the left one
			Child += 1;
		}
		if (LastElement > H->Elements[Child])
		{
			// Copy the smaller child's value upwards
			H->Elements[i] = H->Elements[Child];
		}
		else
		{
			// The iteration is enough
			break;
		}
	}
	
	H->Elements[i] = LastElement;
}

void PercolateUp( Heap *H, int index )
{
	int targ_key = H->Elements[index];
	int i = 0;
	
	for (i = index; i > 1 && H->Elements[i / 2] > targ_key; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	
	H->Elements[i] = targ_key;
}

void Insert( Heap *H, int value )
{
	H->Size += 1;
	
	if (H->Size >= MAXN_SIZE)
	{
		printf("OVERSIZING OCCURRED. INSERTION FAILED.\n");
		return;
	}
	
	H->Elements[H->Size] = value;
	
	PercolateUp(H, H->Size);
}

int DeleteMin( Heap *H )
{
	int minn = H->Elements[1];
	
	H->Elements[1] = H->Elements[H->Size];
	H->Size -= 1;
	
	PercolateDown(H, 1);
	
	return minn;
}

Heap BuildHeap( int size, int *seq )
{
	
	Heap res;
	int i = 0;
	
	
	res.Size = size;
	res.Elements = (int *)malloc(MAXN_SIZE * sizeof(int *));
	
	for (i = 1; i <= size; i++)
	{
		res.Elements[i] = seq[i - 1];
	}
	
	
	for (i = res.Size / 2; i >= 1; i--)
	{
		PercolateDown(&res, i);
	}
	
	return res;
}

void PrintHeap( Heap H )
{
	int i = 0, s = H.Size;
	
	for (i = 1; i <= s; i++)
	{
		printf("%d ", H.Elements[i]);
	}
	
	printf("\n");
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值