最大堆的建立

 //最大堆的建立;

看源代码:

#include <iostream>
using namespace std;
const int max=11;
int dui[max]={0,4,1,3,2,16,9,10,14,8,7};
void swap(int longest,int i)//交换函数
{
	int temp;
	temp=dui[i];
	dui[i]=dui[longest];
	dui[longest]=temp;
}
void max_heapify(int i)
{
	int l=2*i;
	int r=l+1;
	int longest=i;
	if(l<=10&&dui[l]>dui[i])
		longest=l;
	if(r<=10&&dui[r]>dui[longest])
		longest=r;
	if(longest!=i)
	{
		swap(longest,i);
	    max_heapify(longest);
	}
}
void build_heap()
{
	for (int i=5;i>=1;i--)//控制循环,i=5是因为5是堆中元素个数的一半,也就是10的一半
	{
		max_heapify(i);//建立最大堆
	}
}
int main()
{
	build_heap();//建堆函数
	for(int i=1;i<11;i++)
		cout<<dui[i]<<' ';
	cout<<endl;
	return 0;
}

### 实现最大堆(大顶堆)与最小堆(小顶堆) #### 1. 最大堆(大顶堆) 最大堆是一种特殊的完全二叉树结构,在这种结构中,父节点的值总是大于或等于其子节点的值。为了实现最大堆,通常需要完成以下几个核心功能:插入新元素、删除堆顶元素以及调整堆。 以下是基于 Python 的最大堆实现: ```python class MaxHeap: def __init__(self): self.heap = [] def insert(self, value): self.heap.append(value) self._heapify_up(len(self.heap) - 1) def _heapify_up(self, index): parent_index = (index - 1) // 2 if index > 0 and self.heap[parent_index] < self.heap[index]: self.heap[parent_index], self.heap[index] = self.heap[index], self.heap[parent_index] self._heapify_up(parent_index) def extract_max(self): if not self.heap: return None max_value = self.heap[0] last_value = self.heap.pop() if self.heap: self.heap[0] = last_value self._heapify_down(0) return max_value def _heapify_down(self, index): size = len(self.heap) largest = index left_child = 2 * index + 1 right_child = 2 * index + 2 if left_child < size and self.heap[left_child] > self.heap[largest]: largest = left_child if right_child < size and self.heap[right_child] > self.heap[largest]: largest = right_child if largest != index: self.heap[largest], self.heap[index] = self.heap[index], self.heap[largest] self._heapify_down(largest) ``` 以上代码实现了最大堆的核心逻辑[^4]。 --- #### 2. 最小堆(小顶堆) 最小堆也是一种完全二叉树结构,其中每个父节点的值小于或等于其子节点的值。类似于最大堆,最小堆也需要支持插入、删除堆顶元素等功能。 以下是基于 Java 的最小堆实现: ```java import java.util.ArrayList; import java.util.List; public class MinHeap { private List<Integer> heap; public MinHeap() { this.heap = new ArrayList<>(); } public void insert(int value) { heap.add(value); heapifyUp(heap.size() - 1); } private void heapifyUp(int index) { int parentIndex = (index - 1) / 2; if (index > 0 && heap.get(index) < heap.get(parentIndex)) { swap(index, parentIndex); heapifyUp(parentIndex); } } public Integer extractMin() { if (heap.isEmpty()) return null; int min = heap.get(0); int lastIndex = heap.size() - 1; heap.set(0, heap.get(lastIndex)); heap.remove(lastIndex); heapifyDown(0); return min; } private void heapifyDown(int index) { int smallest = index; int leftChild = 2 * index + 1; int rightChild = 2 * index + 2; if (leftChild < heap.size() && heap.get(leftChild) < heap.get(smallest)) { smallest = leftChild; } if (rightChild < heap.size() && heap.get(rightChild) < heap.get(smallest)) { smallest = rightChild; } if (smallest != index) { swap(smallest, index); heapifyDown(smallest); } } private void swap(int i, int j) { int temp = heap.get(i); heap.set(i, heap.get(j)); heap.set(j, temp); } } ``` 此代码展示了如何用 Java 构建并维护一个小顶堆[^2]。 --- #### 总结 无论是最大堆还是最小堆,它们的关键在于维持堆性质的同时执行高效的插入和删除操作。通过不断向上或向下调整堆中的元素位置,可以确保堆始终满足相应的条件[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值