page411.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 64 行
HTML
64 行
<HTML><HEAD><TITLE>Union by Height or Rank</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="tex2html5911" HREF="page412.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5909" HREF="page403.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5905" HREF="page410.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5913" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0012440000000000000000">Union by Height or Rank</A></H2><P>The union-by-size join algorithm described above controls the heightsof the trees indirectly by basing the join algorithm on the sizes of the trees.If we explicitly keep track of the height of a node in the node itself,we can accomplish the same thing.<P>Program <A HREF="page411.html#progpartitionAsForestV3a"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives an implementation of the <tt>join</tt>method that always attaches the shorter tree under the root of the taller one.This method assumes that the <tt>_rank</tt> instance attribute is used to keeptrack of the height of a node.(The reason for calling it <tt>_rank</tt> rather than <tt>height</tt>will become evident shortly).<P><P><A NAME="29716"> </A><A NAME="progpartitionAsForestV3a"> </A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program29499" SRC="img1653.gif" ><BR><STRONG>Program:</STRONG> <tt>PartitionAsForest</tt> class union-by-rank <tt>join</tt> method.<BR><P><P>The only time that the height of node increases is whenjoining two trees that have the same height.In this case, the height of the root increases by exactly one.If the two trees being joined have different heights,attaching the shorter tree under the root of the taller onehas no effect on the height of the root.<P>Unfortunately, there is a slight complication if we combine union-by-heightwith the collapsing find.Since the collapsing find works by moving nodes closer to the root,it affects potentially the height of any node moved.It is not at all clear how to recompute efficientlythe heights that have changed.The solution is not to do it at all!<P>If we don't recompute the heights during the collapsing find operations,then the height instance attributes will no longer be exact.Nevertheless, the quantities remain useful estimates of the heights of nodes.We call the estimated height of a node its <em>rank</em><A NAME=29509> </A>and the join algorithm which uses rank instead of heightis called <em>union by rank</em><A NAME=29511> </A>.<P>Fortunately, Theorem <A HREF="page410.html#theoremsetsi"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> applies equally well when whenunion-by-rank is used.That is, the height of tree which contains <I>n</I> nodes is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.Thus, the worst-case running time for the <tt>find</tt> operationgrows logarithmically with <I>n</I>.And as before, collapsing find only makes things better.<P><HR><A NAME="tex2html5911" HREF="page412.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5909" HREF="page403.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5905" HREF="page410.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5913" 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 + -
显示快捷键?