📄 page257.html
字号:
<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="tex2html4160" HREF="page258.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4158" HREF="page251.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4152" HREF="page256.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4162" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION009300000000000000000">Binary Trees</A></H1><A NAME="sectreesbintree"> </A><P>In this section we consider an extremely important and usefulcategory of tree structure--<em>binary trees</em><A NAME=14925> </A>.A binary tree is an <I>N</I>-ary tree for which <I>N</I> is two.Since a binary tree is an <I>N</I>-ary tree,all of the results derived in the preceding section apply to binary trees.However, binary trees have some interesting characteristicsthat arise from the restriction that <I>N</I> is two.For example, there is an interesting relationship between binary treesand the binary number system.Binary trees are also very useful for the representation ofmathematical expressions involvingthe binary operations such as addition and multiplication.<P>Binary trees are defined as follows:<P><BLOCKQUOTE> <b>Definition (Binary Tree)</b><A NAME="defnbinarytree"> </A>A <em>binary tree</em><A NAME=14930> </A><A NAME=14931> </A> <I>T</I>is a finite set of <em>nodes</em><A NAME=14933> </A>with the following properties:<OL><LI> Either the set is empty, <IMG WIDTH=40 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline62991" SRC="img1064.gif" >; or<LI> The set consists of a root, <I>r</I>, and exactly two distinct binary trees <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63147" SRC="img1085.gif" > and <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63149" SRC="img1086.gif" >, <IMG WIDTH=108 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63151" SRC="img1087.gif" >.</OL>The tree <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63147" SRC="img1085.gif" > is called the <em>left subtree</em><A NAME=14937> </A> of <I>T</I>,and the tree <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63149" SRC="img1086.gif" > is calledthe <em>right subtree</em><A NAME=14939> </A> of <I>T</I>.</BLOCKQUOTE><P>Binary trees are almost always considered to be<em>ordered trees</em><A NAME=14942> </A><A NAME=14943> </A>.Therefore, the two subtrees <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63147" SRC="img1085.gif" > and <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline63149" SRC="img1086.gif" > are called the<em>left</em> and <em>right</em> subtrees, respectively.Consider the two binary trees shown in Figure <A HREF="page257.html#figtree4"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Both trees have a root with a single non-empty subtree.However, in one case it is the left subtree which is non-empty;in the other case it is the right subtree that is non-empty.Since the order of the subtrees matters,the two binary trees shown in Figure <A HREF="page257.html#figtree4"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> are different.<P><P><A NAME="15090"> </A><A NAME="figtree4"> </A> <IMG WIDTH=575 HEIGHT=132 ALIGN=BOTTOM ALT="figure14948" SRC="img1088.gif" ><BR><STRONG>Figure:</STRONG> Two distinct binary trees.<BR><P><P>We can determine some of the characteristics of binary treesfrom the theorems given in the preceding section by letting <I>N</I>=2.For example, Theorem <A HREF="page256.html#theoremtreesi"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> tells us that an binary tree with <IMG WIDTH=39 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58503" SRC="img238.gif" > internal nodes contains <I>n</I>+1 external nodes.This result is true regardless of the shape of the tree.Consequently, we expect that the storage overhead of associatedwith the empty trees will be <I>O</I>(<I>n</I>).<P>From Theorem <A HREF="page256.html#theoremtreesii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we learn thata binary tree of height <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63063" SRC="img1074.gif" > has at most <IMG WIDTH=58 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline63175" SRC="img1089.gif" > internal nodes.Conversely, the height of a binary tree with <I>n</I> internal nodesis at least <IMG WIDTH=106 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63179" SRC="img1090.gif" >.That is, the height of a binary tree with <I>n</I> nodes is <IMG WIDTH=55 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60139" SRC="img519.gif" >.<P>Finally, according to Theorem <A HREF="page256.html#theoremtreesiii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,a binary tree of height <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63063" SRC="img1074.gif" > has at most <IMG WIDTH=15 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline63187" SRC="img1091.gif" > leaves.Conversely, the height of a binary tree with <I>l</I> leavesis at least <IMG WIDTH=45 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63191" SRC="img1092.gif" >.Thus, the height of a binary tree with <I>l</I> leaves is <IMG WIDTH=50 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63195" SRC="img1093.gif" ><HR><A NAME="tex2html4160" HREF="page258.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html4158" HREF="page251.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html4152" HREF="page256.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html4162" 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 + -