简易版的堆的写法

部署运行你感兴趣的模型镜像

个人认为重点写出max_heapify和parent_heapify两个函数即可,这个版本内存管理的功能显得特别简单:

#include<iostream>
#include<stdio.h>

using namespace std;

class Heap {
public:
  int size, capacity;
  int *ele;
  void max_heapify(int i,int heap[],int len){//数组从0开始   
    int l,r,largest;  
    l=2*i+1;   r=2*i+2;  
    if( l<len && heap[l]>heap[i] )  
      largest=l;  
    else  
      largest=i;  
    if( r<len && heap[r]>heap[largest] )  
      largest=r;  
    if(largest!=i){  
      swap( heap[largest],heap[i] );  
      max_heapify(largest,heap,len);  
    }  
  }
  void parent_heapify(int i,int heap[],int len){//和parent结点不断交换   
    int parent = i / 2;  
    while (heap[i] > heap[parent]) {  
      swap(heap[i], heap[parent]);  
      i = parent, parent = i / 2;  
    }  
  } 

  Heap(int capacity, int heap[], int len) {
    this->capacity = capacity, this->size = len;
    ele = heap;
    for(int i = size/2-1 ; i >= 0; i--)  
      max_heapify(i,ele,size);
  }
  bool push(int val) {
    if (size < capacity) {
      ele[size++] = val;
      parent_heapify(size - 1, ele, size);
      return true;
    }
    else
      return false;
  }
  void pop() {
    swap(ele[0], ele[size--]);
    max_heapify(0, ele, size);
  }
  int top() {
    return ele[0];
  }
};

int main() {
  int a[10] = {1,2,3,4,5,6,7};
  int len = sizeof(a) / sizeof(a[0]);
  Heap heap(10, a, 7);
  heap.pop();
  heap.push(10);

  return 0;
}

Above code isn't right in the function parent_heapify: parent = (i-1) / 2;

完整版:

#include<iostream>
#include<stdio.h>

using namespace std;

class Heap {
 private:
  int *arr, size, capacity;
 public:
  
  void max_heapify(int heap[], int i, int size) {
    int l = 2 * i + 1, largest = i;
    if (l < size && heap[l] > heap[i])
      largest = l;
    int r = 2 * i + 2;
    if (r < size && heap[r] > heap[largest])
      largest = r;
    if (largest != i) {
      swap(heap[largest], heap[i]);
      max_heapify(heap, largest, size);      
    }
  }
  void parent_heapify(int heap[], int i, int size) {
    if (i >= size)
      return;
    int parent = (i - 1) / 2;
    while (i != 0 && heap[i] > heap[parent]) {
      swap(heap[i], heap[parent]);
      i = parent, parent = (i - 1) / 2;
    }
  }
  Heap(int heap[], int len, int cap) {
    if (len> cap)
      this->capacity = this->size = len;
    else
      this->capacity = cap, this->size = len;
    arr = new int[capacity];
    memcpy(arr, heap, len * sizeof(int));
    for (int i = len / 2 - 1; i >= 0; --i) 
      max_heapify(arr, i, size);
  }
  ~Heap() {
    delete[] arr;
    arr = NULL;
  }
  void push(int val) {
    if (size >= capacity) {
      capacity *= 2;
      int *tmp = arr;
      arr= new int[capacity];
      memcpy(arr, tmp, sizeof(arr[0]) * size);
      delete[] tmp;
    }
    arr[size++] = val;
    parent_heapify(arr, size - 1, size);
  }
  void pop() {
    if (size == 0)
      return;
    else {
      swap(arr[0], arr[--size]);
      max_heapify(arr, 0, size);
    }
  }
  int top() {
    return size == 0 ? -1 : arr[size - 1];
  }
};

int main() {
  int a[] = {0,1,2,3,4,5,6};
  Heap heap(a, sizeof(a) / sizeof(a[0]), 10);
  heap.push(7);
  heap.push(8);
  heap.push(9);
  heap.push(10);
  heap.pop();
  int res = heap.top();
  return 0;

}



您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

在 C++ STL 中,优先队列(`priority_queue`)默认实现为大根(max-heap),即顶元素是最大的。若需要实现小根(min-heap),可以通过指定第三个模板参数为 `greater<int>` 来实现。 ### 小根的实现方法 #### 基本写法 使用 `priority_queue` 时,通过指定比较函数对象为 `greater<int>`,可以构建一个以整数为基础的小根: ```cpp #include <iostream> #include <queue> using namespace std; int main() { priority_queue<int, vector<int>, greater<int>> minHeap; // 插入元素 minHeap.push(3); minHeap.push(1); minHeap.push(4); minHeap.push(2); // 输出顶元素(最小值) cout << "Top element: " << minHeap.top() << endl; // 输出 1 return 0; } ``` #### 自定义数据类型的实现 如果希望将小根应用于自定义数据类型,例如结构体,可以通过重载比较运算符或提供自定义比较类来实现。以下是一个使用结构体并基于某个字段构建小根的示例: ```cpp #include <iostream> #include <queue> using namespace std; struct Node { int value; Node(int v) : value(v) {} }; // 自定义比较函数对象 struct CompareNode { bool operator()(const Node& a, const Node& b) { return a.value > b.value; // 小根 } }; int main() { priority_queue<Node, vector<Node>, CompareNode> minHeap; // 插入自定义结构体对象 minHeap.push(Node(10)); minHeap.push(Node(5)); minHeap.push(Node(20)); // 输出顶元素的值 cout << "Top element: " << minHeap.top().value << endl; // 输出 5 return 0; } ``` ### 应用场景 小根常用于需要频繁获取最小值的场景,例如: - **Top K 最小元素**:通过维护大小为 K 的小根,可以快速获取数据流中前 K 个最小值。 - **Dijkstra 算法**:图中最短路径问题中,通常使用小根来选择当前距离最小的节点[^3]。 ### 注意事项 - 默认情况下,`priority_queue` 使用 `vector` 作为底层容器,而 `greater<T>` 是用于构建小根的标准比较器。 - 对于复杂类型,必须自定义比较逻辑以确保的行为符合预期[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值