📄 page502.html
字号:
<HTML><HEAD><TITLE>Building the Heap</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6939" HREF="page503.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6937" HREF="page497.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6933" HREF="page501.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6941" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0015530000000000000000">Building the Heap</A></H2><P>The <tt>buildHeap</tt> method shown in Program <A HREF="page502.html#progheapSorterb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>transforms an unsorted array into a max heap.It does so by calling the <tt>percolateDown</tt> methodfor <IMG WIDTH=257 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline69559" SRC="img2072.gif" >.<P><P><A NAME="40009"> </A><A NAME="progheapSorterb"> </A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program39925" SRC="img2073.gif" ><BR><STRONG>Program:</STRONG> <tt>HeapSorter</tt> class <tt>buildHeap</tt> method.<BR><P><P>Why does <tt>buildHeap</tt> start percolating at <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58273" SRC="img177.gif" >?A complete binary tree with <I>n</I> nodeshas exactly <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67953" SRC="img1774.gif" > leaves.Therefore, the last node in the array which has a childis in position <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58273" SRC="img177.gif" >.Consequently, the <tt>buildHeap</tt> method starts doing percolate downoperations from that point.<P>The <tt>buildHeap</tt> visits the array elements in reverse order.In effect the algorithm starts at the deepest node that has a childand works toward the root of the tree.Each array position visited is the root of a subtree.As each such subtree is visited,it is transformed into a max heap.Figure <A HREF="page502.html#figsort5"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how the <tt>buildHeap</tt> methodheapifies an array that is initially unsorted.<P><P><A NAME="40834"> </A><A NAME="figsort5"> </A> <IMG WIDTH=575 HEIGHT=631 ALIGN=BOTTOM ALT="figure39939" SRC="img2074.gif" ><BR><STRONG>Figure:</STRONG> Building a heap.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html6942" HREF="page503.html#SECTION0015531000000000000000">Running Time Analysis</A><LI> <A NAME="tex2html6943" HREF="page504.html#SECTION0015532000000000000000">The Sorting Phase</A></UL><HR><A NAME="tex2html6939" HREF="page503.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6937" HREF="page497.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6933" HREF="page501.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6941" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -