page287.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 63 行
HTML
63 行
<HTML><HEAD><TITLE>Binary 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="tex2html4508" HREF="page288.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4506" HREF="page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4500" HREF="page286.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4510" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION009650000000000000000">Binary Trees</A></H2><A NAME="sectreesbintreeimp"> </A><P>This section presents an implementation of binary treesin the sense of Definition <A HREF="page257.html#defnbinarytree"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.A binary tree is essentially a <I>N</I>-ary tree where <I>N</I>=2.Therefore, it is possible to implemente binary treesusing the <tt>NaryTree</tt> class presented in the preceding section.However, because the <tt>NaryTree</tt> class implementationis a general implementation which can accommodate any value of <I>N</I>,it is somewhat less efficient in both time and spacethan an implementation which is designed specifically for the case <I>N</I>=2.Since binary trees occur quite frequently in practice,it is important to have a good implementation.<P>Another consequence of restricting <I>N</I> to twois that we can talk of the left and right subtrees of a tree.Consequently the interface provided by a binary tree class is quitedifferent from the general interface provided by an <I>N</I>-ary tree class.<P>Figure <A HREF="page287.html#figtree9"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows how thebinary tree given in Figure <A HREF="page258.html#figtree5"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is be represented.The basic idea is that each node of the treecontains two instance attributes that refer to the subtrees of that node.Just as we did for <I>N</I>-ary trees,we represent explicitly the empty trees.Since an empty tree node contains neither root nor subtreesit is represented by a structure in which all the instance attributes are <tt>None</tt>.<P><P><A NAME="16482"> </A><A NAME="figtree9"> </A> <IMG WIDTH=575 HEIGHT=337 ALIGN=BOTTOM ALT="figure16316" SRC="img1131.gif" ><BR><STRONG>Figure:</STRONG> Representing binary trees.<BR><P><P>The <tt>BinaryTree</tt> class is introduced in Program <A HREF="page287.html#progbinaryTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The <tt>BinaryTree</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="16696"> </A><A NAME="progbinaryTreea"> </A> <IMG WIDTH=575 HEIGHT=485 ALIGN=BOTTOM ALT="program16490" SRC="img1132.gif" ><BR><STRONG>Program:</STRONG> <tt>BinaryTree</tt> class <tt>__init__</tt> and <tt>purge</tt> methods.<BR><P><BR> <HR><UL> <LI> <A NAME="tex2html4511" HREF="page288.html#SECTION009651000000000000000">Instance Attributes</A><LI> <A NAME="tex2html4512" HREF="page289.html#SECTION009652000000000000000"><tt>__init__</tt> method</A><LI> <A NAME="tex2html4513" HREF="page290.html#SECTION009653000000000000000"><tt>purge</tt> Method</A><LI> <A NAME="tex2html4514" HREF="page291.html#SECTION009654000000000000000"><tt>right</tt> and <tt>left</tt> Properties</A></UL><HR><A NAME="tex2html4508" HREF="page288.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4506" HREF="page267.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4500" HREF="page286.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4510" 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 + -
显示快捷键?