- 算法设计与分析
- 首页
- 算法基础
- 算法导论 - 算法介绍
- 算法导论 - 算法分析
- 算法导论 - 分析方法
- 算法导论 - 渐近记号与先验分析
- 算法导论 - 时间复杂度
- 算法导论 - 主定理
- 算法导论 - 空间复杂度
- 分治法
- 算法导论 - 分治算法
- 算法导论 - 最大最小问题
- 算法导论 - 归并排序算法
- 算法导论 - Strassen矩阵乘法
- 算法导论 - Karatsuba算法
- 算法导论 - 汉诺塔
- 贪心算法
- 算法导论 - 贪心算法
- 算法导论 - 旅行商问题
- 算法导论 - Prim最小生成树
- 算法导论 - Kruskal最小生成树
- 算法导论 - Dijkstra最短路径算法
- 算法导论 - 地图着色算法
- 算法导论 - 分数背包问题
- 算法导论 - 带截止时间的作业排序
- 算法导论 - 最优合并模式
- 动态规划
- 算法导论 - 动态规划
- 算法导论 - 矩阵链乘法
- 算法导论 - Floyd-Warshall算法
- 算法导论 - 0-1背包问题
- 算法导论 - 最长公共子序列算法
- 算法导论 - 使用动态规划的旅行商问题
- 随机化算法
- 算法导论 - 随机化算法
- 算法导论 - 随机化快速排序算法
- 算法导论 - Karger最小割算法
- 算法导论 - Fisher-Yates洗牌算法
- 近似算法
- 算法导论 - 近似算法
- 算法导论 - 顶点覆盖问题
- 算法导论 - 集合覆盖问题
- 算法导论 - 旅行推销员近似算法
- 排序技术
- 算法导论 - 冒泡排序算法
- 算法导论 - 插入排序算法
- 算法导论 - 选择排序算法
- 算法导论 - 希尔排序算法
- 算法导论 - 堆排序算法
- 算法导论 - 桶排序算法
- 算法导论 - 计数排序算法
- 算法导论 - 基数排序算法
- 算法导论 - 快速排序算法
- 搜索技术
- 算法导论 - 搜索技术介绍
- 算法导论 - 线性搜索
- 算法导论 - 二分搜索
- 算法导论 - 插值搜索
- 算法导论 - 跳跃搜索
- 算法导论 - 指数搜索
- 算法导论 - 斐波那契搜索
- 算法导论 - 子列表搜索
- 算法导论 - 哈希表
- 图论
- 算法导论 - 最短路径
- 算法导论 - 多阶段图
- 算法导论 - 最优代价二叉搜索树
- 堆算法
- 算法导论 - 二叉堆
- 算法导论 - 插入方法
- 算法导论 - 堆化方法
- 算法导论 - 提取方法
- 复杂度理论
- 算法导论 - 确定性与非确定性计算
- 算法导论 - 最大团
- 算法导论 - 顶点覆盖
- 算法导论 - P类和NP类
- 算法导论 - Cook定理
- 算法导论 - NP难和NP完全类
- 算法导论 - 爬山算法
- 算法导论有用资源
- 算法导论 - 快速指南
- 算法导论 - 有用资源
- 算法导论 - 讨论
从堆中提取根元素
extract方法用于提取堆的根元素。以下是算法。
伪代码
Heap-Extract-Max (numbers[]) max = numbers[1] numbers[1] = numbers[heapsize] heapsize = heapsize – 1 Max-Heapify (numbers[], 1) return max
示例
让我们考虑前面讨论的相同示例。现在我们要提取一个元素。此方法将返回堆的根元素。
删除根元素后,最后一个元素将移动到根位置。
现在,将调用Heapify函数。Heapify之后,将生成以下堆。
实现
以下是此操作在各种编程语言中的实现:
#include <stdio.h>
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void maxHeapify(int arr[], int size, int i) {
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
int largest = i;
if (leftChild < size && arr[leftChild] > arr[largest])
largest = leftChild;
if (rightChild < size && arr[rightChild] > arr[largest])
largest = rightChild;
if (largest != i) {
swap(arr, i, largest);
maxHeapify(arr, size, largest); // Recursive call to continue heapifying
}
}
int extractMax(int arr[], int *heapSize) {
if (*heapSize < 1) {
printf("Heap underflow!\n");
return -1;
}
int max = arr[0];
arr[0] = arr[*heapSize - 1];
(*heapSize)--;
maxHeapify(arr, *heapSize, 0); // Heapify the updated heap
return max;
}
int main() {
int arr[] = { 55, 50, 30, 40, 20, 15, 10 }; // Max-Heap
int heapSize = sizeof(arr) / sizeof(arr[0]);
int max = extractMax(arr, &heapSize); // Extract the max element from the heap
printf("Extracted Max Element: %d\n", max);
// Print the updated Max-Heap
printf("Updated Max-Heap: ");
for (int i = 0; i < heapSize; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
输出
Extracted Max Element: 55 Updated Max-Heap: 50 40 30 10 20 15
#include <iostream>
#include <vector>
void swap(std::vector<int>& arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void maxHeapify(std::vector<int>& arr, int size, int i) {
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
int largest = i;
if (leftChild < size && arr[leftChild] > arr[largest])
largest = leftChild;
if (rightChild < size && arr[rightChild] > arr[largest])
largest = rightChild;
if (largest != i) {
swap(arr, i, largest);
maxHeapify(arr, size, largest); // Recursive call to continue heapifying
}
}
int extractMax(std::vector<int>& arr, int& heapSize) {
if (heapSize < 1) {
std::cout << "Heap underflow!" << std::endl;
return -1;
}
int max = arr[0];
arr[0] = arr[heapSize - 1];
heapSize--;
maxHeapify(arr, heapSize, 0); // Heapify the updated heap
return max;
}
int main() {
std::vector<int> arr = { 55, 50, 30, 40, 20, 15, 10 }; // Max-Heap
int heapSize = arr.size();
int max = extractMax(arr, heapSize); // Extract the max element from the heap
std::cout << "Extracted Max Element: " << max << std::endl;
// Print the updated Max-Heap
std::cout << "Updated Max-Heap: ";
for (int i = 0; i < heapSize; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}
输出
Extracted Max Element: 55 Updated Max-Heap: 50 40 30 10 20 15
import java.util.Arrays;
public class MaxHeap {
public static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void maxHeapify(int arr[], int size, int i) {
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
int largest = i;
if (leftChild < size && arr[leftChild] > arr[largest])
largest = leftChild;
if (rightChild < size && arr[rightChild] > arr[largest])
largest = rightChild;
if (largest != i) {
swap(arr, i, largest);
maxHeapify(arr, size, largest); // Recursive call to continue heapifying
}
}
public static int extractMax(int arr[], int heapSize) {
if (heapSize < 1) {
System.out.println("Heap underflow!");
return -1;
}
int max = arr[0];
arr[0] = arr[heapSize - 1];
heapSize--;
maxHeapify(arr, heapSize, 0); // Heapify the updated heap
return max;
}
public static void main(String args[]) {
int arr[] = { 55, 50, 30, 40, 20, 15, 10 }; // Max-Heap
int heapSize = arr.length;
int max = extractMax(arr, heapSize); // Extract the max element from the heap
System.out.println("Extracted Max Element: " + max);
// Print the updated Max-Heap
System.out.print("Updated Max-Heap: ");
for (int i = 0; i < heapSize; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
输出
Extracted Max Element: 55 Updated Max-Heap: 50 40 30 10 20 15 10
def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def max_heapify(arr, size, i):
left_child = 2 * i + 1
right_child = 2 * i + 2
largest = i
if left_child < size and arr[left_child] > arr[largest]:
largest = left_child
if right_child < size and arr[right_child] > arr[largest]:
largest = right_child
if largest != i:
swap(arr, i, largest)
max_heapify(arr, size, largest) # Recursive call to continue heapifying
def extract_max(arr, heap_size):
if heap_size < 1:
print("Heap underflow!")
return -1
max_element = arr[0]
arr[0] = arr[heap_size - 1]
heap_size -= 1
max_heapify(arr, heap_size, 0) # Heapify the updated heap
return max_element
arr = [55, 50, 30, 40, 20, 15, 10] # Max-Heap
heap_size = len(arr)
max_element = extract_max(arr, heap_size) # Extract the max element from the heap
print("Extracted Max Element:", max_element)
# Print the updated Max-Heap
print("Updated Max-Heap:", arr)
输出
Extracted Max Element: 55 Updated Max-Heap: [50, 40, 30, 10, 20, 15, 10]
广告