---
title: "编程练习——堆heap"
author: "holoblog"
date: "2008-10-25T08:37:00Z"
tags: []
slug: "heap"
draft: false
lastmod: "2011-11-11T11:21:56Z"
id: 1251925
url: "https://holoblog.iteye.com/blog/1251925"
---

因为vs2005中c++的Heap构造[有些问题](http://blog.csdn.net/cctt_1/archive/2008/10/14/3074616.aspx)，所以才重写了heap类。

不知道vs2008中这个是不是个bug。

001. /\*createdbychicochen
002. \*date2008/10/25
003. \*/
004. template&lt;classT,classCmp&gt;
005. classHeap
006. {
007. private:
008. vector&lt;T&gt;heap;
009. voidShiftDown(intindex)
010. {
011. while(!IsLeaf(index))
012. {
013. intl=Lchild(index);
014. intr=Rchild(index);
015. if(r!=-1)
016. {
017. if(Cmp::Up(heap\[r],heap\[l]))
018. {
019. //puttheupnodetotheleft
020. swap(heap\[l],heap\[r]);
021. }
022. }
023. if(Cmp::Up(heap\[l],heap\[index]))
024. {
025. //uptheleftchild
026. swap(heap\[l],heap\[index]);
027. index=l;
028. }
029. else
030. {
031. break;
032. }
033. }
034. }
035. voidShiftUp(intindex)
036. {
037. intparent=-1;
038. while(index!=0)
039. {
040. parent=Parent(index);
041. if(Cmp::Up(heap\[index],heap\[parent]))
042. {
043. swap(heap\[index],heap\[parent]);
044. index=parent;
045. }
046. else
047. {
048. break;
049. }
050. }
051. }
052. voidReHeapAll()
053. {
054. for(inti=Parent(heap.size()-1);i&gt;=0;i--)
055. {
056. ShiftDown(i);
057. }
058. }
059. voidReHeapOne(intindex)
060. //setonepriorandre-positiontheindexnode
061. {
062. Tdata=heap\[index];
063. ShiftDown(index);
064. if(Cmp::Eq(data,heap\[index]))
065. {
066. ShiftUp(index);
067. }
068. }
069. intParent(intindex)
070. //parentnodeoftheindexnode
071. {
072. return(index-1)/2;
073. }
074. intLchild(intindex)
075. //Leftchildoftheindexnode
076. {
077. if(!IsLeaf(index))
078. returnindex\*2+1;
079. return-1;
080. }
081. intRchild(intindex)
082. //Rightchildoftheindexnode
083. {
084. if(!IsLeaf(index))
085. {
086. intr=index\*2+2;
087. if(r&lt;heap.size())
088. returnr;
089. }
090. return-1;
091. }
092. intIsLeaf(intindex)
093. {
094. assert(index&lt;heap.size());
095. if(heap.size()==1)
096. returntrue;
097. intlastNode=Parent(heap.size()-1);
098. returnlastNode&lt;index;
099. }
100. intFindData(T&data)
101. {
102. intlen=heap.size();
103. for(inti=0;i&lt;len;i++)
104. {
105. if(Cmp::Eq(data,heap\[i]))
106. {
107. returni;
108. }
109. }
110. return-1;//cannotfindthedata
111. }
112. voidprintTree(intindex,intcount)
113. {
114. if(index&lt;heap.size()&&index&gt;=0)
115. {
116. printTree(Rchild(index),count+4);
117. for(inti=0;i&lt;count;i++)
118. {
119. cout&lt;&lt;"";
120. }
121. cout&lt;&lt;heap\[index]&lt;&lt;endl;
122. printTree(Lchild(index),count+4);
123. }
124. }
125. public:
126. Heap()
127. {
128. heap=vector&lt;T&gt;();
129. }
130. ~Heap()
131. {
132. //heapdestroyed
133. }
134. boolIsEmpty()
135. {
136. returnheap.size()==0;
137. }
138. voidInsert(T&data)
139. {
140. heap.push\_back(data);
141. ShiftUp(heap.size()-1);
142. }
143. voidDelete(intindex)
144. {
145. intlast=heap.size()-1;
146. if(index&gt;=0&&index&lt;=last)
147. {
148. swap(heap\[index],heap\[last]);
149. heap.pop\_back();
150. ReHeapOne(index);
151. }
152. }
153. voidDelete(T&data)
154. {
155. intindex=FindData(data);
156. if(index!=-1)
157. Delete(index);
158. }
159. voidRemoveTop()
160. {
161. if(!IsEmpty()&&heap.size()&gt;1)
162. {
163. swap(heap\[0],heap\[heap.size()-1]);
164. heap.pop\_back();
165. ShiftDown(0);
166. }
167. elseif(heap.size()==1)
168. {
169. heap.pop\_back();
170. }
171. }
172. T&Top()
173. {
174. returnheap\[0];
175. }
176. voidPrint()
177. {
178. printTree(0,1);
179. }
180. };