page349.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 105 行
HTML
105 行
<HTML><HEAD><TITLE>Exercises</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="tex2html5207" HREF="page350.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5205" HREF="page298.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5199" HREF="page348.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5209" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0010900000000000000000">Exercises</A></H1><P><OL><LI> <A NAME="exercisesrchtreei"> </A> For each of the following key sequences determine the binary search tree obtained when the keys are inserted one-by-one in the order given into an initially empty tree: <OL><LI> 1, 2, 3, 4, 5, 6, 7.<LI> 4, 2, 1, 3, 6, 5, 7.<LI> 1, 6, 7, 2, 4, 3, 5. </OL><LI> <A NAME="exercisesrchtreeii"> </A> For each of the binary search trees obtained in Exercise <A HREF="page349.html#exercisesrchtreei"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> determine the tree obtained when the root is withdrawn.<LI> Repeat Exercises <A HREF="page349.html#exercisesrchtreei"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> and <A HREF="page349.html#exercisesrchtreeii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> for AVL trees.<LI> Derive an expression for the total space needed to represent a tree of <I>n</I> internal nodes using each of the following classes: <OL><LI> <tt>BinarySearchTree</tt> introduced in Program <A HREF="page311.html#progbinarySearchTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,<LI> <tt>AVLTree</tt> introduced in Program <A HREF="page320.html#progavlTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,<LI> <tt>MWayTree</tt> introduced in Program <A HREF="page332.html#progmWayTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>, and<LI> <tt>BTree</tt> introduced in Program <A HREF="page341.html#progbTreea"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>. </OL> <b>Hint</b>: For the <tt>MWayTree</tt> and <tt>BTree</tt> assume that the tree contains are <I>k</I> keys, where <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65427" SRC="img1366.gif" >.<LI> To delete a non-leaf node from a binary search tree, we swap it either with the smallest key its right subtree or with the largest key in its left subtree and then recursively delete it from the subtree. In a tree of <I>n</I> nodes, what its the maximum number of swaps needed to delete a key?<LI> Devise an algorithm to compute the internal path length of a tree. What is the running time of your algorithm?<LI> Devise an algorithm to compute the external path length of a tree. What is the running time of your algorithm?<LI> Suppose that you are given a sorted sequence of <I>n</I> keys, <IMG WIDTH=144 HEIGHT=21 ALIGN=MIDDLE ALT="tex2html_wrap_inline65433" SRC="img1367.gif" >, to be inserted into a binary search tree. <OL><LI> What is the minimum height of a binary tree that contains <I>n</I> nodes.<LI> Devise an algorithm to insert the given keys into a binary search tree so that the height of the resulting tree is minimized.<LI> What is the running time of your algorithm? </OL><LI> Devise an algorithm to construct an AVL tree of a given height <I>h</I> that contains the minimum number of nodes. The tree should contain the keys <IMG WIDTH=92 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline65439" SRC="img1368.gif" >, where <IMG WIDTH=20 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline64249" SRC="img1257.gif" > is given by Equation <A HREF="page319.html#eqnsrchtreeavl"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<LI> Consider what happens when we insert the keys <IMG WIDTH=132 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline65443" SRC="img1369.gif" > one-by-one in the order given into an initially empty AVL tree for <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline63063" SRC="img1074.gif" >. Prove that the result is always a perfect tree of height <I>h</I>.<LI> <A NAME="exercisesrchtreefind"> </A> The <tt>find</tt> method defined in Program <A HREF="page313.html#progbinarySearchTreeb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is recursive. Write a non-recursive method to find a given item in a binary search tree.<LI> Repeat Exercise <A HREF="page349.html#exercisesrchtreefind"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> for the <tt>min</tt> method defined in Program <A HREF="page313.html#progbinarySearchTreeb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<LI> Devise an algorithm to select the <IMG WIDTH=20 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline61477" SRC="img821.gif" > key in a binary search tree. For example, given a tree with <I>n</I> nodes, <I>k</I>=0 selects the smallest key, <I>k</I>=<I>n</I>-1 selects the largest key, and <IMG WIDTH=95 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65457" SRC="img1370.gif" > selects the median key.<LI> Devise an algorithm to test whether a given binary search tree is AVL balanced. What is the running time of your algorithm?<LI> Devise an algorithm that takes two values, <I>a</I> and <I>b</I> such that <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65463" SRC="img1371.gif" >, and which visits all the keys <I>x</I> in a binary search tree such that <IMG WIDTH=67 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65467" SRC="img1372.gif" >. The running time of your algorithm should be <IMG WIDTH=90 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65469" SRC="img1373.gif" >, where <I>N</I> is the number of keys visited and <I>n</I> is the number of keys in the tree.<LI> Devise an algorithm to merge the contents of two binary search trees into one. What is the running time of your algorithm?<LI> <A NAME="exercisesrchtreecomplete"> </A> (This question should be attempted <em>after</em> reading Chapter <A HREF="page351.html#chappqueues"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>). Prove that a <em>complete binary tree</em> (Definition <A HREF="page354.html#defncompletebt"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>) is AVL balanced.<LI> Do Exercise <A HREF="page249.html#exercisehashingtree"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<LI> <A NAME="exercisesrchtreeiii"> </A> For each of the following key sequences determine the 3-way search tree obtained when the keys are inserted one-by-one in the order given into an initially empty tree: <OL><LI> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.<LI> 3, 1, 4, 5, 9, 2, 6, 8, 7, 0.<LI> 2, 7, 1, 8, 4, 5, 9, 0, 3, 6. </OL><LI> Repeat Exercise <A HREF="page349.html#exercisesrchtreeiii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> for B-trees of order 3.</OL><HR><A NAME="tex2html5207" HREF="page350.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5205" HREF="page298.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5199" HREF="page348.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5209" 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 + -
显示快捷键?