简易版的堆的写法

个人认为重点写出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;

}



排序是一种基于比较的高效排序算法,利用了二叉的数据结构特性。它的时间复杂度为 O(n log n),并且不需要额外的空间开销(原地排序)。下面是一个简单的排序实现步骤及其伪代码: ### 排序的基本思想 1. 将无序数组构建成最大(大顶),使得每个节点都大于等于其左右子节点。 2. 把根节点(当前的最大值)与最后一个叶子节点交换,并将新的最后一位移除出范围。 3. 对剩余元素重新调整成最大,然后重复上述操作直到所有数据都被处理。 以下是具体的 Python 示例代码: ```python def heapify(arr, n, i): largest = i # 初始化最大的为根节点索引 left = 2 * i + 1 # 左孩子节点位置 right = 2 * i + 2 # 右孩子节点位置 if left < n and arr[left] > arr[largest]: largest = left if right < n and arr[right] > arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) # 主函数构建并执行排序 def heapSort(arr): n = len(arr) # 构建初始最大 for i in range(n // 2 - 1, -1, -1): # 从非叶结点开始自下而上构造 heapify(arr, n, i) # 调整使其满足性质 # 进行n次循环取出每轮的最大值放到序列尾部 for i in range(n-1, 0, -1): arr[0], arr[i] = arr[i], arr[0] # 最大数换到数组末尾 heapify(arr, i, 0) # 再对缩小后的规模继续维持属性 arr = [45, 78, 12, 69, 34, 21, 56] heapSort(arr) print("Sorted array is:", arr) ``` ### 解释说明: - **`heapify` 函数**:保证某个节点下的树保持最大的性质。 - **第一次遍历 (`for`) :由底至上建立最大** - 第二次通过不断把第一个元素移到最后的位置完成最终排列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值