page367.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 60 行
HTML
60 行
<HTML><HEAD><TITLE>Removing Items from a Leftist 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="tex2html5413" HREF="page368.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5411" HREF="page361.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5407" HREF="page366.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5415" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0011350000000000000000">Removing Items from a Leftist Heap</A></H2><P>The <tt>getMin</tt> method returnsthe item with the smallest key in a given priority queueand the <tt>dequeueMin</tt> method removes it from the queue.Since the smallest item in a heap is found at the root,the <tt>getMin</tt> method is easy to implement.Program <A HREF="page367.html#progleftistHeapd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows how it can be done.Clearly, the running time of <tt>getMin</tt> is <I>O</I>(1).<P><P><A NAME="26078"> </A><A NAME="progleftistHeapd"> </A> <IMG WIDTH=575 HEIGHT=161 ALIGN=BOTTOM ALT="program25981" SRC="img1440.gif" ><BR><STRONG>Program:</STRONG> <tt>LeftistHeap</tt> class <tt>getMin</tt> method.<BR><P><P>Since the smallest item in a heap is at the root,the <tt>dequeueMin</tt> operation must delete the root node.Since a leftist heap is a binary heap,the root has at most two children.In general when the root is deleted,we are left with two non-empty leftist heaps.Since we already have an efficient way to merge leftist heaps,the solution is to simply merge the two children of the rootto obtain a single heap again!Program <A HREF="page367.html#progleftistHeape"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows how the <tt>dequeueMin</tt> operationof the <tt>LeftistHeap</tt> class can be implemented.<P><P><A NAME="26080"> </A><A NAME="progleftistHeape"> </A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program25994" SRC="img1441.gif" ><BR><STRONG>Program:</STRONG> <tt>LeftistHeap</tt> class <tt>dequeueMin</tt> method.<BR><P><P>The running time of Program <A HREF="page367.html#progleftistHeape"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is determinedby the time required to merge the two children of the root (line 11)since the rest of the work in <tt>dequeueMin</tt> can be done in constant time.Consider the running time to delete the root of a leftist heap <I>T</I>with <I>n</I> internal nodes.The running time to merge the left and right subtrees of <I>T</I><P> <IMG WIDTH=390 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath65919" SRC="img1442.gif" ><P>where <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline65827" SRC="img1423.gif" > and <IMG WIDTH=17 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline65829" SRC="img1424.gif" > are the null path lengths of the left and rightsubtrees <I>T</I>, respectively.In the worst case, <IMG WIDTH=48 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline65935" SRC="img1443.gif" > and <IMG WIDTH=90 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65937" SRC="img1444.gif" >.If we assume that <IMG WIDTH=107 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65777" SRC="img1414.gif" >,the running time for <tt>dequeueMin</tt> is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.<P><HR><A NAME="tex2html5413" HREF="page368.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5411" HREF="page361.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5407" HREF="page366.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5415" 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 + =
减小字号Ctrl + -
显示快捷键?