<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><font size="6" color="#FF0000"><span lang="en-ca"><b>Min-Max 
Heap</b></span><b>£¨1£©</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">first edition of my min-max-heap and in order to try other approach I need to save this edition first.</span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>How to construct a template-based min-heap and max-heap </span>in a <span lang="en-ca">universal</span> format<span lang="en-ca">?</span></b></font></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  ¡¡</div>
<p><span lang="en-ca"><b>The first approach is just like text book---using 
compare class in template parameter to differ the </b></span></p>
<p><span lang="en-ca"><b>min and max heap. But can you pass parameter twice when 
declare? No, unless you use the &quot;bool&quot; flag.</b></span></p>
<p><span lang="en-ca"><b>This is what I want to try later.</b></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font size="3"><span lang="en-ca">1. heap.h</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2. heap.cpp  (main)</b></font></span></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: heap.h</b></font></span></pre>
</div>
<pre>template&lt;class Elem&gt;
void swap(Elem* array, int i, int j)
{
	Elem temp;
	temp=array[i];
	array[i]=array[j];
	array[j]=temp;
}

/*
template&lt;class Key, class Elem&gt;
class EEComp
{
public:
	static bool before(Elem e1, Elem e2);
	static bool same(Elem e1, Elem e2);
	static bool after(Elem e1, Elem e2);
};
*/

template&lt;class Key, class Elem,  class KEComp, class EEComp&gt;
class Heap
{
private:
	Elem* array;
	int maxLength;
	int curLength;
	void siftDown(int index);
	void siftUp(int index);
	int parent(int index) { return (index-1)/2;}
	int left(int index) { return index*2+1;}
	int right(int index) { return index*2+2;}
	bool isLeaf(int index) { return left(index)&gt;=curLength;}
public:
	void buildHeap();
	void display();
	Heap(Elem* theArray, int size){ array=theArray; curLength=maxLength=size;}
};


template&lt;class Key, class Elem,  class KEComp, class EEComp&gt;
void Heap&lt;Key, Elem, KEComp, EEComp&gt;::buildHeap()
{
	for (int i=(maxLength-1)/2; i&gt;=0; i--)
	{
		siftDown(i);
	}
}


template&lt;class Key, class Elem,  class KEComp, class EEComp&gt;
void Heap&lt;Key, Elem, KEComp, EEComp&gt;::display()
{
	for (int i=0; i&lt;maxLength; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot; &quot;;
	}
	cout&lt;&lt;endl;
}

template&lt;class Key, class Elem,  class KEComp, class EEComp&gt;
void Heap&lt;Key, Elem, KEComp, EEComp&gt;::siftUp(int index)
{
	int p;
	if (index&gt;0)
	{
		p=parent(index);
		//swap only if son is &quot;before&quot; parent
		if (EEComp::before(array[index], array[p]))
		{
			swap(array, index, p);
			siftUp(p);
		}
	}
}


template&lt;class Key, class Elem,  class KEComp, class EEComp&gt;
void Heap&lt;Key, Elem, KEComp, EEComp&gt;::siftDown(int index)
{
	int l, r, result;
	l=left(index);
	r=right(index);

	if (l&lt;=curLength)//index is not a leaf
	{
		result=l;
		if (r&lt;=curLength)//if right is valid
		{
			//swap index only if right is &quot;before&quot; left
			if (EEComp::before(array[r], array[l]))//if right is before left
			{
				result = r;
			}
		}
		//
		if (EEComp::before(array[result], array[index]))//compare the &quot;bigger&quot; with parent
		{
			swap(array, result, index);
			siftDown(result);
		}
		//if not, simply end here
	}
}

</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: </b></font></span><font size="3" color="#FF0000"><b>heap.cpp (main)</b></font></pre>
<pre>#include &lt;iostream&gt;
#include &quot;Heap.h&quot;

using namespace std;


class intMaxComp
{
public:
	static bool before(int i, int j) { return i&gt;j;} 
	static bool same(int i, int j) { return i==j;}
	static bool after(int i, int j) { return i&lt;j;}
};

class intMinComp
{
public:
	static bool before(int i, int j) { return i&lt;j;} 
	static bool same(int i, int j) { return i==j;}
	static bool after(int i, int j) { return i&gt;j;}
};


int main()
{
	const int Length=20;
	int myArray[Length];
	for (int i=0; i&lt;Length; i++)
	{
		myArray[i]=rand()%100+1;//1--100
	}
	Heap&lt;int, int, intMaxComp, intMinComp&gt; H(myArray, Length);
	H.buildHeap();
	H.display();
	return 0;
}
</pre>
<pre>¡¡</pre>
<pre><font color="#0000FF"><b>Here is the result:<span lang="en-ca"> The result is a min-heap and by changing the declaration of Heap, say,</span></b></font></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>replace &quot;int</b></font></span><font color="#0000FF"><b><span lang="en-ca">MinComp&quot; with &quot;intMaxComp&quot;, you can easily build a max-heap. </span></b></font><span lang="en-ca"><font color="#0000FF"><b> </b></font></span></pre>

<pre>1 6 25 28 20 35 28 59 43 37 70 46 82 79 62 92 96 68 63 42
Press any key to continue</pre>

<pre></pre>
<pre>	
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>