📄 page391.html
字号:
<HTML><HEAD><TITLE>Union, Intersection, and Difference</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="tex2html5693" HREF="page392.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5691" HREF="page389.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5685" HREF="page390.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5695" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0012202000000000000000">Union, Intersection, and Difference</A></H3><P>Program <A HREF="page391.html#progsetAsArrayc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the three methods,<tt>__or__</tt>, <tt>__and__</tt>, and <tt>__sub__</tt>.These methods correspond to <IMG WIDTH=9 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66699" SRC="img1561.gif" >, <IMG WIDTH=9 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66701" SRC="img1562.gif" >, and -, respectively.<P><P><A NAME="28212"> </A><A NAME="progsetAsArrayc"> </A> <IMG WIDTH=575 HEIGHT=543 ALIGN=BOTTOM ALT="program27752" SRC="img1563.gif" ><BR><STRONG>Program:</STRONG> <tt>SetAsArray</tt> class <tt>__or__</tt>, <tt>__and__</tt> and <tt>__sub__</tt> methods.<BR><P><P>The set union method takes two arguments, <tt>self</tt> and <tt>set</tt>.The second argument is assumed to be a <tt>SetAsArray</tt> instance.The method computes the <tt>SetAsArray</tt> obtainedfrom the union of the sets <tt>self</tt> and <tt>set</tt>.The implementation given requires that the sets be compatible.Two sets are deemed to be compatible if they have the same universe.The result also has the same universe.Consequently, the <tt>bool</tt> array in all three setshas the same length, <I>N</I>.The set union method creates a result array of the required sizeand then computes the elements of the array as required.The <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > element of the result is <tt>true</tt>if either the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > element of <tt>self</tt>or the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > element of <tt>set</tt> is <tt>true</tt>.Thus, set union is implemented usingthe Boolean <em>or</em><A NAME=27782> </A><A NAME=27783> </A> operator,<tt>or</tt>.<P>The set intersection method is almost identical to set union,except that the elements of the result are computed usingthe Boolean <em>and</em><A NAME=27786> </A><A NAME=27787> </A> operator, <tt>and</tt>.The set difference method is also very similar.In this case, an item is an element of the resultonly if it is a member of <tt>self</tt> and not a member of <tt>set</tt>.<P>Because all three methods are almost identical,their running times are essentially the same.That is, the running time ofthe set union, intersection, and difference operations is <I>O</I>(<I>N</I>),where <IMG WIDTH=145 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66681" SRC="img1556.gif" >.<P><HR><A NAME="tex2html5693" HREF="page392.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5691" HREF="page389.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5685" HREF="page390.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5695" 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 + -