首页 > 其他分享 >Heap

Heap

时间:2023-10-24 10:33:41浏览次数:27  
标签:Pasted image element Heap new png

dg-publish: true

study/datastructure


A Heap is a complete binary tree, where all levels of tree, except
Possibly the last level, are fully filled

![[Pasted image 20231023104516.png|300]]

Min Heap

  • A parent node has a key that is smaller than its child nodes
  • The root node has the smallest key

![[Pasted image 20231023104907.png|300]]

Common Operations

  • Get the element with the minimum or maximum key
  • Remove the element with the minimum or maximum key
  • Insert a new element

Applications

  • Priority Queues (e.g. CPU job-scheduling)
  • Sorting (via HeapSort)

Implementation

Use an array to represent a Min Heap
![[Pasted image 20231023110447.png|300]]

![[Pasted image 20231023110228.png|300]]

i=index of array
Parent node : i
	Left node: 2*i+1
	Right node: 2*i+2
Find a node's parents; (i-1)/2

Insert

Place new element 0 at the end of Heap
![[Pasted image 20231023132640.png]]

Compare new element 0 with parent element 4;swap as new element 0 is smaller
![[Pasted image 20231023132758.png]]

Swap new element 0 with parent element 1 as new element is smaller;stop as heap property is restored
![[Pasted image 20231023132941.png]]

Remove

Replace root element 1 with last element 5
![[Pasted image 20231023133035.png]]

Child element 2 is the smallest,sqap with target element 5
![[Pasted image 20231023133128.png]]

Child element 3 is the smallest,swap with target element 5; stop as heap property is restored
![[Pasted image 20231023133142.png]]

Heap Sort

  • Given a list of inputs, how to sort its key in ascending order?
    • Construct a Min Heap using the inputs
    • While Min Heap not empty
      • Extract the minimum key

标签:Pasted,image,element,Heap,new,png
From: https://www.cnblogs.com/HardisonDream/p/17784151.html

相关文章

  • springboot heapdump信息获取
    springboot信息泄露可能泄漏的路由/api-docs/v2/api-docs/swagger-ui.html/api.html/sw/swagger-ui.html/api/swagger-ui.html/template/swagger-ui.html/spring-security-rest/api/swagger-ui.html/spring-security-oauth-resource/swagger-ui.html/mappings/actua......
  • What causes "Invalid Address specified to RtlValidateHeap"?
    ForumVisualC++&C++ProgrammingVisualC++ProgrammingWhatcauses"InvalidAddressspecifiedtoRtlValidateHeap"?Ifthisisyourfirstvisit,besuretocheckoutthe FAQ byclickingthelinkabove.Youmayhaveto register or Login ......
  • Go - Creating Heaps
    Problem: Youwanttocreateaminheapdatastructure.Solution: Wrapastructaroundasliceofelementstorepresentaheap.Aftereachpushorpopontheheap,rearrangetheheapstructure. AHeapisaspecialTree-baseddatastructureinwhichthe......
  • "堆"(Heap)和"栈"(Stack)两个重要的内存管理概念
    在Delphi和其他编程语言中,"堆"(Heap)和"栈"(Stack)是两个重要的内存管理概念,用于存储和管理程序中的数据和变量。它们有不同的特性和用途:堆(Heap):堆是一块动态分配的内存区域,用于存储对象、数据结构和变量。堆内存的分配和释放是由程序员手动控制的,通常使用New和Dispose(或GetMem和......
  • FreeRTOS 原理 --- heap 堆内存的使用
    FreeRTOS一共提供了5种申请内存的方案heap1只申请不释放,内存利用率最高。申请出来的内存块,没有内存块头记录这个内存的大小,所以也无法释放,也正是没有内存块头,内存利用率高使用场景:不需要频繁申请内存heap2能申请能释放,不能合并内存块。每个内存块都有一个内存块头,有一个链表......
  • 8.4 ProcessHeap
    ProcessHeap是Windows进程的默认堆,每个进程都有一个默认的堆,用于在进程地址空间中分配内存空间。默认情况下ProcessHeap由内核进行初始化,该堆中存在一个未公开的属性,它被设置为加载器为进程分配的第一个堆的位置(进程堆标志),ProcessHeap标志位于PEB结构中偏移为0x18处,第一个堆头......
  • 使用MAT比较多个heap dump文件
    参考文档:https://www.cnblogs.com/melody-emma/p/4914832.html 1.步骤 2.生成结果 3.对比效果 ......
  • JVM堆内存(heap)详解
    JAVA堆内存管理是影响性能主要因素之一。堆内存溢出是JAVA项目非常常见的故障,在解决该问题之前,必须先了解下JAVA堆内存是怎么工作的。先看下JAVA堆内存是如何划分的,如图:Java堆内存又溢出了!教你一招必杀技   JVM内存划分为堆内存和非堆内存,堆内存分为年轻代(YoungGeneration)、......
  • tomcat出现Java heap space / PermGen space解决方法(详解)
    使用Java程序从数据库中查询大量的数据时出现异常:java.lang.OutOfMemoryError:Javaheapspace在JVM中如果98%的时间是用于GC且可用的Heapsize不足2%的时候将抛出此异常信息。JVM堆的设置是指java程序运行过程中JVM可以调配使用的内存空间的设置.JVM在启动的时候会自动设置Heap......
  • 0ctf babyheap
    文件信息pwndbg>checksec[*]'/mnt/hgfs/CTF/nightmare-master/modules/28-fastbin_attack/0ctf_babyheap/0ctfbabyheap'Arch:amd64-64-littleRELRO:FullRELROStack:CanaryfoundNX:NXenabledPIE:PIEenabled保护全开pwndbg>r......