堆的基本操作

堆的定义

typedef int HPDataType;
typedef int(*pCompar)(HPDataType left, HPDataType right);
typedef struct Heap
{
	HPDataType* _hp; //保存堆的数组
	int _Capecity; //容量
	int _size;//元素个数
	pCompar _compar; //函数指针
}Heap, *pHeap;

辅助函数

小于比较
int Less(HPDataType left, HPDataType right)//小堆
{
	return left < right;
}
大于比较
int Greater(HPDataType left, HPDataType right)  
{
	return left > right;
}
交换函数
void Swap(HPDataType* Left, HPDataType* Right)
{
	HPDataType tmp = 0;
	tmp = *Left;
	*Left = *Right;
	*Right = tmp;
}
向下调整法
void AdjustDown(pHeap heap, int parent)
{
	assert(heap);
	int child; //标记最小的孩子,默认为左
	child = (parent << 1) + 1; //根据根结点计算孩子下标
	while (child <= heap->_size - 1)
	{
	//找到最小的孩子  
	if (child + 1 < heap->_size)//右孩子存在
	{
		if (heap->_compar((heap->_hp[child + 1]), (heap->_hp[child])))
			child = child + 1;
	}
	//检测parent是否满足堆的性质,parent>child
	if (heap->_compar((heap->_hp[child]), (heap->_hp[parent])))  //孩子小于父母
	{
		Swap(&(heap->_hp[parent]), &(heap->_hp[child]));//交换
		//继续向下检测
		parent = child; //继续向下比较,直到都满足堆的性质
		child = (parent << 1) + 1;
	}
	else
		return;
	}
}
向上调整法
void AdjustUp(pHeap heap, int child)
{
	assert(heap);
	int parent = (child - 1) >> 1;
	while(child > 0)
	{
		if (heap->_compar((heap->_hp[child]), (heap->_hp[parent])))
		{
			Swap(&(heap->_hp[parent]), &(heap->_hp[child]));//交换
			//继续向下检测
			child = parent; //继续向下比较,直到都满足堆的性质
			parent = (child - 1) >> 1;
		}
		else
			return;
	}
}

初始化

void Initheap(pHeap heap, HPDataType* arr, int size, pCompar compar)
{
	assert(heap);
	//开辟堆空间,将数组中的数据放入
	heap->_hp = (HPDataType*)malloc(size * sizeof(HPDataType));
	if (NULL == heap->_hp)
	{
		assert(0);
	}
	memcpy(heap->_hp, arr, size * sizeof(HPDataType));
	heap->_Capecity = size * sizeof(HPDataType);
	heap->_size = size;
	heap->_compar = compar;
}

创建堆

void Createheap(pHeap heap)
{
	assert(heap);
	//构成堆(向下调整法)
	//从最后一个非叶子结点开始调整
	int root = (heap->_size - 2) >> 1; //size为数组元素个数
	for (; root >= 0; root--)
		AdjustDown(heap, root); //向下调整
}

堆插入元素

void Insertheap(pHeap heap, HPDataType d)
{
	assert(heap);
	//先将元素放入最后一个元素之后
	//查看空间是否充足
	if (heap->_Capecity < (int)((heap->_size + 1) * sizeof(HPDataType)))
	{
		HPDataType* hp = (HPDataType*)malloc(heap->_size * 2 * sizeof(HPDataType));
		if (NULL == heap->_hp)
		{
			assert(0);
		}
		memcpy(hp, heap->_hp, heap->_size * 2 * sizeof(HPDataType));
		heap->_hp = hp;
		heap->_Capecity = heap->_size * 2 * sizeof(HPDataType);
		heap->_size = heap->_size + 1;
	}
	heap->_hp[heap->_size - 1] = d;
	//堆向上调整
	int child = heap->_size - 1;
	AdjustUp(heap, child);
}

删除堆顶元素

void Romoveheap(pHeap heap)
{
	assert(heap);
	if (heap->_size == 0)
	{
		return;
	}
	//交换堆顶与堆底元素
	Swap(&(heap->_hp[heap->_size - 1]), &(heap->_hp[0]));//交换
	//删除堆尾元素
	heap->_size--;
	AdjustDown(heap, 0); //向下调整
}

堆元素个数

int Sizeheap(pHeap heap)
{
	assert(heap);
	return heap->_size;
}

堆是否为空

int Emptyheap(pHeap heap)
{
	assert(heap);
	return heap->_size == 0;
}

堆顶元素

int Topheap(pHeap heap)
{
	assert(heap);
	return heap->_hp[0];
}

销毁堆

void Restoyheap(pHeap heap)
{
	assert(heap);
	free(heap->_hp);
	heap->_hp = NULL;
	heap->_Capecity = 0;
	heap->_size = 0;
}
最大排序的基本操作包括以下几个步骤: 1. 构建最大:将待排序的数组构造成一个最大。最大是一种满足父节点大于等于子节点的完全二叉树。构建最大的方法是从最后一个非叶子节点开始,依次向上调整每个节点,使其满足最大的性质。 2. 交换顶元素和最后一个元素:将最大顶元素与最后一个元素交换位置,即将最大的元素放到数组的末尾。 3. 调整:将剩下的元素重新调整为最大。从顶开始,比较左右子节点的值,将较大的子节点与父节点交换位置,然后再对交换后的子节点进行调整,直到整个重新满足最大的性质。 4. 重复步骤2和步骤3,直到所有元素都被排序。 下面是一个用C++实现基于最大排序的示例: ```cpp #include <iostream> using namespace std; // 调整 void adjustHeap(int arr[], int n, int i) { int largest = i; // 初始化最大值为根节点 int left = 2 * i + 1; // 左子节点 int right = 2 * i + 2; // 右子节点 // 如果左子节点大于根节点,更新最大值 if (left < n && arr[left] > arr[largest]) { largest = left; } // 如果右子节点大于根节点,更新最大值 if (right < n && arr[right] > arr[largest]) { largest = right; } // 如果最大值不是根节点,交换根节点和最大值 if (largest != i) { swap(arr[i], arr[largest]); // 递归调整交换后的子节点 adjustHeap(arr, n, largest); } } // 排序 void heapSort(int arr[], int n) { // 构建最大 for (int i = n / 2 - 1; i >= 0; i--) { adjustHeap(arr, n, i); } // 交换顶元素和最后一个元素,并调整 for (int i = n - 1; i > 0; i--) { swap(arr[0], arr[i]); adjustHeap(arr, i, 0); } } int main() { int arr[] = {91, 84, 72, 63, 55, 46, 37, 29, 20, 11}; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); cout << "Sorted array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值