import{_ as l,o as p,c as o,k as s,a as n,X as a}from"./chunks/framework.6e839c56.js";const x=JSON.parse('{"title":"Heap","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/datastructure/heap.md","filePath":"en/api/packages/datastructure/heap.md"}'),e={name:"en/api/packages/datastructure/heap.md"},t=s("h1",{id:"Heap",tabindex:"-1"},[n("Heap "),s("a",{class:"header-anchor",href:"#Heap","aria-label":'Permalink to "Heap"'},"​")],-1),c=s("p",null,"Heap is a binary heap tree implemented by slice.",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"Source",tabindex:"-1"},[n("Source "),s("a",{class:"header-anchor",href:"#Source","aria-label":'Permalink to "Source"'},"​")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go")])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),A=a(`<h2 id="Usage" tabindex="-1">Usage <a class="header-anchor" href="#Usage" aria-label="Permalink to &quot;Usage&quot;">​</a></h2><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span></code></pre></div>`,2),E=s("div",{STYLE:"page-break-after: always;"},null,-1),u=a('<h2 id="Index" tabindex="-1">Index <a class="header-anchor" href="#Index" aria-label="Permalink to &quot;Index&quot;">​</a></h2><ul><li><a href="#MaxHeap">MaxHeap</a></li><li><a href="#Push">Push</a></li><li><a href="#Pop">Pop</a></li><li><a href="#Peek">Peek</a></li><li><a href="#Data">Data</a></li><li><a href="#Size">Size</a></li></ul>',2),v=s("div",{STYLE:"page-break-after: always;"},null,-1),d=a(`<h2 id="Documentation" tabindex="-1">Documentation <a class="header-anchor" href="#Documentation" aria-label="Permalink to &quot;Documentation&quot;">​</a></h2><h3 id="_1-MaxHeap" tabindex="-1">1. MaxHeap <a class="header-anchor" href="#_1-MaxHeap" aria-label="Permalink to &quot;1. MaxHeap&quot;">​</a></h3><p>MaxHeap is a binary heap tree implemented by slice, The key of the root node is both greater than or equal to the key value of the left subtree and greater than or equal to the key value of the right subtree.</p><h3 id="NewMaxHeap" tabindex="-1"><span id="NewMaxHeap">NewMaxHeap</span> <a class="header-anchor" href="#NewMaxHeap" aria-label="Permalink to &quot;&lt;span id=&quot;NewMaxHeap&quot;&gt;NewMaxHeap&lt;/span&gt;&quot;">​</a></h3><p>Return a NewMaxHeap pointer instance.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">MaxHeap</span><span style="color:#F6F6F4;">[T any] </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	data       []T</span></span>
<span class="line"><span style="color:#F6F6F4;">	comparator lancetconstraints.Comparator</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> NewMaxHeap[T any](comparator lancetconstraints.Comparator) </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">]</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	data       </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span></span>
<span class="line"><span style="color:#393A34;">	comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">]</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap)</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Push" tabindex="-1"><span id="Push">Push</span> <a class="header-anchor" href="#Push" aria-label="Permalink to &quot;&lt;span id=&quot;Push&quot;&gt;Push&lt;/span&gt;&quot;">​</a></h3><p>Push value into the heap</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">Push</span><span style="color:#F6F6F4;">(value T)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">value T</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">12</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap.</span><span style="color:#97E1F1;">Data</span><span style="color:#F6F6F4;">()) </span><span style="color:#7B7F8B;">//[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">12</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Data</span><span style="color:#999999;">())</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Pop" tabindex="-1"><span id="Pop">Pop</span> <a class="header-anchor" href="#Pop" aria-label="Permalink to &quot;&lt;span id=&quot;Pop&quot;&gt;Pop&lt;/span&gt;&quot;">​</a></h3><p>Pop return the largest value, and remove it from the heap if heap is empty, return zero value and fasle</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">Pop</span><span style="color:#F6F6F4;">() (T, </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Pop</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">12</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">    val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maxHeap.</span><span style="color:#97E1F1;">Pop</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val) </span><span style="color:#7B7F8B;">//12</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(ok) </span><span style="color:#7B7F8B;">//true</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">12</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Pop</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//12</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">ok</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//true</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Peek" tabindex="-1"><span id="Peek">Peek</span> <a class="header-anchor" href="#Peek" aria-label="Permalink to &quot;&lt;span id=&quot;Peek&quot;&gt;Peek&lt;/span&gt;&quot;">​</a></h3><p>Return the largest element from the heap without removing it, if heap is empty, it returns zero value and false.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">Peek</span><span style="color:#F6F6F4;">() (T, </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Peek</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">12</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">    val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maxHeap.</span><span style="color:#97E1F1;">Peek</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val) </span><span style="color:#7B7F8B;">//12</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap.</span><span style="color:#97E1F1;">Size</span><span style="color:#F6F6F4;">()) </span><span style="color:#7B7F8B;">//12</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">12</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Peek</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//12</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Size</span><span style="color:#999999;">())</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//12</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Data" tabindex="-1"><span id="Data">Data</span> <a class="header-anchor" href="#Data" aria-label="Permalink to &quot;&lt;span id=&quot;Data&quot;&gt;Data&lt;/span&gt;&quot;">​</a></h3><p>Return all element of the heap</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">Data</span><span style="color:#F6F6F4;">() []T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Data</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">12</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap.</span><span style="color:#97E1F1;">Data</span><span style="color:#F6F6F4;">()) </span><span style="color:#7B7F8B;">//[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">12</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Data</span><span style="color:#999999;">())</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Size" tabindex="-1"><span id="Size">Size</span> <a class="header-anchor" href="#Size" aria-label="Permalink to &quot;&lt;span id=&quot;Size&quot;&gt;Size&lt;/span&gt;&quot;">​</a></h3><p>Return the number of elements in the heap</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">Size</span><span style="color:#F6F6F4;">() </span><span style="color:#97E1F1;font-style:italic;">int</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Size</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap.</span><span style="color:#97E1F1;">Size</span><span style="color:#F6F6F4;">()) </span><span style="color:#7B7F8B;">//3</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Size</span><span style="color:#999999;">())</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//3</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="PrintStructure" tabindex="-1"><span id="PrintStructure">PrintStructure</span> <a class="header-anchor" href="#PrintStructure" aria-label="Permalink to &quot;&lt;span id=&quot;PrintStructure&quot;&gt;PrintStructure&lt;/span&gt;&quot;">​</a></h3><p>Print the tree structure of the heap</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (h </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">MaxHeap[T]) </span><span style="color:#62E884;">PrintStructure</span><span style="color:#F6F6F4;">()</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">h </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">MaxHeap</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">PrintStructure</span><span style="color:#999999;">()</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    heap </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">	val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">	val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	} </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">		</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    maxHeap </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> heap.NewMaxHeap[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{})</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">12</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">	</span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> _, v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> values {</span></span>
<span class="line"><span style="color:#F6F6F4;">		maxHeap.</span><span style="color:#97E1F1;">Push</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">	}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(maxHeap.</span><span style="color:#97E1F1;">PrintStructure</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#7B7F8B;">//        12</span></span>
<span class="line"><span style="color:#7B7F8B;">//    9       11</span></span>
<span class="line"><span style="color:#7B7F8B;">//  4   8   10   7</span></span>
<span class="line"><span style="color:#7B7F8B;">// 1 3 5 6 2</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#59873A;">heap</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/datastructure/heap</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">maxHeap</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> heap</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewMaxHeap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{})</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">12</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> values </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">		maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">Push</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">	</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">maxHeap</span><span style="color:#999999;">.</span><span style="color:#998418;">PrintStructure</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#A0ADA0;">//        12</span></span>
<span class="line"><span style="color:#A0ADA0;">//    9       11</span></span>
<span class="line"><span style="color:#A0ADA0;">//  4   8   10   7</span></span>
<span class="line"><span style="color:#A0ADA0;">// 1 3 5 6 2</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div>`,45),h=[t,c,r,y,F,i,A,E,u,v,d];function m(C,B,g,f,b,k){return p(),o("div",null,h)}const _=l(e,[["render",m]]);export{x as __pageData,_ as default};
