Stack data structure (create, insert, delete, sort)

Heap-related notes

Reference Notes: Summary Liu recalcitrant chiefs https://www.liuchuo.net/archives/2277

A secondary reason summary

About secondary summarizes whether it is necessary, I think it is there, a reference to learn other people's knowledge, internalization process is a summary of the process. Each time the same person thinking reference trajectory can really cultivate thinking. The article notes finishing Rationale: PAT Advanced level of Heap Path a title, refer to the image on the heap of positive sequence traversal. Then take a look heap related notes.

Second, the main heap data structure algorithms (for example large root heap)

  • Creating the heap
void createHeap(){
    for(int i=n/2;i>=1;i--)
        downAjust(i,n);
}
  • Downward adjustment: The idea is, low behalf of children, high representative of the limit. We make adjustments [low, high] range, low and low + 1 is 2 * low children. If there is no exchange, it would break out, even above his father exchanged to compare.
void downAdjust(int low,int high){
    int i=low,j=i*2;//i为要调整的节点,j为左孩子
    while(j<=high){
        if(j+1<=high && heap[j+1]>heap[j]) j=j+1;
        if(heap[j]>heap[i]){
            swap(heap[j],heap[i]);
            i=j;j=i*2;
        }else break;
    }
}
  • Delete the top of the heap element
void deleteTop(){
    heap[1]=heap[n--];//用第n个数进行覆盖
    downAdjust(1,n);//之后进行向下调整第一个数
}
  • Add an element
void insert(int x){
    heap[++n]=x;
    upAdjust(1,n);
}
  • Upward adjustment
void upAdjust(int low,int high){
    int i=high,j=i/2;
    while(j>=low){
        if(heap[j]<heap[i]){
            swap(heap[j],heap[i]);
            i=j;j=i/2;
        }else break;
    }
}
  • Heapsort

The number and range of the first top exchange elements, for downward adjustment (1, i-1) until i == 2

void heapSort(){
    createHeap();
    for(int i=n;i>=2;i--){
        swap(heap[i],heap[1]);
        downAdjust(1,i-1);
    }
}

Heap sort time complexity: O (nlogn) space complexity is (1) O is unstable stability

Third, handwriting heap.h header file

#ifndef HEAP_H_INCLUDED
#define HEAP_H_INCLUDED

#define maxn 1000

using namespace std;//这里面有swap函数

int heap[maxn];

void upAdjust(int low,int high){
    int i=high,j=i/2;
    while(j>=low){
        if(heap[j]<heap[i]){
            swap(heap[j],heap[i]);
            i=j;j=i/2;
        }else break;
    }
}

void downAdjust(int low,int high){
    int i=low,j=i*2;//i为要调整的节点,j为左孩子
    while(j<=high){
        if(j+1<=high && heap[j+1]>heap[j]) j=j+1;
        if(heap[j]>heap[i]){
            swap(heap[j],heap[i]);
            i=j;j=i*2;
        }else break;
    }
}

void createHeap(int n){
    for(int i=n/2;i>=1;i--)
        downAdjust(i,n);
}


void deleteTop(int n){
    heap[1]=heap[n--];//用第n个数进行覆盖
    downAdjust(1,n);//之后进行向下调整第一个数
}

void insert(int x,int n){
    heap[++n]=x;
    upAdjust(1,n);
}


void heapSort(int n){
    createHeap(n);
    for(int i=n;i>=2;i--){
        swap(heap[i],heap[1]);
        downAdjust(1,i-1);
    }
}
#endif // HEAP_H_INCLUDED

Fourth, the test file

#include <iostream>
#include "heap.h"
using namespace std;
int main(){
    extern int heap[maxn];
    extern int n;
    scanf("%d",&n);
    /**我们的堆序列是从[1,n]的*/
    for(int i=1;i<=n;i++) scanf("%d",&heap[i]);
    createHeap();//生成大根堆
    insert(4);//插入堆顶元素
    deleteTop();//删除堆顶
    heapSort();//排序
    for(int i=1;i<=n;i++) printf("%d%s",heap[i],i==n?"\n":" ");
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11617612.html