page281.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 62 行
HTML
62 行
<HTML><HEAD><TITLE>N-ary Trees</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="tex2html4439" HREF="page282.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4437" HREF="page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4431" HREF="page280.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4441" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION009640000000000000000"><I>N</I>-ary Trees</A></H2><P>We now turn to the implementation of <I>N</I>-ary treesas given by Definition <A HREF="page256.html#defnnarytree"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.According to this definition,an <I>N</I>-ary tree is either an empty treeor it is a tree comprised of a root and exactly <I>N</I> subtrees.The implementation follows the design patternestablished in the preceding section.Specifically, we view an <I>N</I>-ary tree as a container.<P>Figure <A HREF="page281.html#figtree8"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the way in which<I>N</I>-ary trees can be represented.The figure gives the representation of the tertiary (<I>N</I>=3) tree<P> <IMG WIDTH=321 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath63307" SRC="img1126.gif" ><P>The basic idea is that each node has associated with itan array of length <I>N</I> of pointers to the subtrees of that node.An array is used because we assume that the <em>arity</em><A NAME=16070> </A>of the tree, <I>N</I>, is known <em>a priori</em>.<P><P><A NAME="16186"> </A><A NAME="figtree8"> </A> <IMG WIDTH=575 HEIGHT=240 ALIGN=BOTTOM ALT="figure16072" SRC="img1127.gif" ><BR><STRONG>Figure:</STRONG> Representing <I>N</I>-ary trees using pointer arrays.<BR><P><P>Notice that we explicitly represent the empty trees.That is, a separate object instance is allocated for each empty tree.Of course, an empty tree contains neither root nor subtrees.<P>Program <A HREF="page281.html#prognaryTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introduces the the <tt>NaryTree</tt> classwhich represents <I>N</I>-ary trees as specified by Definition <A HREF="page256.html#defnnarytree"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>NaryTree</tt> class extends the abstract <tt>Tree</tt> classintroduced in Program <A HREF="page267.html#progtreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="16326"> </A><A NAME="prognaryTreea"> </A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program16195" SRC="img1128.gif" ><BR><STRONG>Program:</STRONG> <tt>NaryTree</tt> class <tt>__init__</tt> and <tt>purge</tt> methods.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html4442" HREF="page282.html#SECTION009641000000000000000">Instance Attributes</A><LI> <A NAME="tex2html4443" HREF="page283.html#SECTION009642000000000000000"><tt>__init__</tt> and <tt>purge</tt> Methods</A><LI> <A NAME="tex2html4444" HREF="page284.html#SECTION009643000000000000000"><tt>getIsEmpty</tt> Method</A><LI> <A NAME="tex2html4445" HREF="page285.html#SECTION009644000000000000000"><tt>getKey</tt>, <tt>AttachKey</tt> and <tt>DetachKey</tt> Methods</A><LI> <A NAME="tex2html4446" HREF="page286.html#SECTION009645000000000000000"><tt>getSubtree</tt>, <tt>attachSubtree</tt> and <tt>detachSubtree</tt> Methods</A></UL><HR><A NAME="tex2html4439" HREF="page282.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4437" HREF="page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4431" HREF="page280.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4441" 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 + -
显示快捷键?