°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡level¡¡after¡¡another¡£¡¡You¡¡stop¡¡traversing¡¡down¡¡the¡¡tree¡¡once¡¡you¡¡reach¡¡a¡¡room¡¡that¡¡you¡¡have¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡already¡¡traversed¡£¡¡Then¡¡you¡¡go¡¡back¡¡one¡¡level¡¡and¡¡traverse¡¡the¡¡room¡¡beside¡¡the¡¡room¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡have¡¡already¡¡traversed¡£¡¡This¡¡could¡¡mean¡¡the¡¡search¡¡path¡¡found¡¡by¡¡the¡¡puter¡¡would¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡similar¡¡to¡¡Figure¡¡4¡1¡£¡¡That¡¯s¡¡because¡¡the¡¡puter¡¡is¡¡as¡¡dumb¡¡as¡¡you¡¡are£»¡¡albeit¡¡the¡¡puter¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡saying¡¡to¡¡itself£»¡¡¡°If¡¡only¡¡I¡¡had¡¡started¡¡in¡¡the¡¡hall¡£¡±¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Realize¡¡that¡¡there¡¡is¡¡no¡¡magic¡¡wand¡¡to¡¡find¡¡the¡¡keys¡¡for¡¡you¡£¡¡The¡¡technique¡¡that¡¡you¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡puter¡¡used¡¡is¡¡called¡¡brute¡¡force£»¡¡and¡¡it¡¡is¡¡putationally¡¡expensive¡¡and¡¡typically¡¡avoided¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡case¡¡though£»¡¡brute¡¡force¡¡is¡¡the¡¡only¡¡real¡¡solution£»¡¡because¡¡you¡¡don¡¯t¡¡know¡¡where¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡keys¡¡are¡ªthey¡¡could¡¡be¡¡anywhere¡¡in¡¡the¡¡house¡£¡¡It¡¡just¡¡happened¡¡to¡¡be¡¡your¡¡bad¡¡luck¡¡that¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡keys¡¡were¡¡found¡¡in¡¡the¡¡last¡¡place¡¡you¡¡looked¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡try¡¡to¡¡improve¡¡the¡¡situation¡£¡¡Imagine¡¡for¡¡a¡¡moment¡¡that¡¡your¡¡keys¡¡are¡¡tagged¡¡with¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡clapper¡£¡¡The¡¡idea¡¡is¡¡that¡¡with¡¡your¡¡keys¡¡going¡¡¡°beep£»¡¡beep£»¡¡beep£»¡±¡¡you¡¡will¡¡be¡¡able¡¡to¡¡instantly¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡find¡¡them¡¡and¡¡avoid¡¡the¡¡brute¡force¡¡searching¡£¡¡But¡¡suppose¡¡that¡¡the¡¡keys¡¡are¡¡in¡¡the¡¡upper¡right¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡corner¡¡of¡¡the¡¡child¡¡bedroom¡£¡¡Listening¡¡for¡¡the¡¡beeping£»¡¡you¡¡think¡¡that¡¡you¡¡hear¡¡the¡¡keys¡¡either¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡the¡¡kitchen¡¡or¡¡the¡¡child¡¡bedroom¡£¡¡So£»¡¡where¡¡do¡¡you¡¡go¡¡first£¿¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡103¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡81¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Which¡¡path¡¡es¡¡first¡¡or¡¡is¡¡the¡¡most¡¡efficient¡¡is¡¡a¡¡mon¡¡problem£»¡¡and¡¡you¡¡may¡¡witness¡¡¡¡
it¡¡every¡¡day¡¡if¡¡you¡¡have¡¡a¡¡global¡¡positioning¡¡system¡¡£¨GPS£©¡¡for¡¡you¡¡car¡£¡¡A¡¡search¡¡algorithm¡¡is¡¡¡¡
monly¡¡used¡¡in¡¡GPS¡¡devices¡£¡¡You¡¡punch¡¡in¡¡a¡¡set¡¡of¡¡coordinates£»¡¡and¡¡the¡¡GPS¡¡device¡¡will¡¡¡¡
attempt¡¡to¡¡find¡¡the¡¡quickest¡¡or¡¡shortest¡¡way¡£¡¡In¡¡the¡¡abstract¡¡sense£»¡¡the¡¡search¡¡algorithm¡¡the¡¡GPS¡¡¡¡
makers¡¡apply¡¡is¡¡identical¡¡to¡¡the¡¡search¡¡algorithm¡¡that¡¡we¡¡are¡¡going¡¡to¡¡develop¡¡in¡¡this¡¡chapter¡£¡¡
Implementing¡¡User¡Defined¡¡Types¡¡
The¡¡data¡¡structure¡¡on¡¡which¡¡the¡¡algorithm¡¡will¡¡operate¡¡is¡¡a¡¡user¡defined¡¡type¡£¡¡In¡¡the¡¡examples¡¡¡¡
up¡¡to¡¡this¡¡point£»¡¡we¡¡have¡¡been¡¡using¡¡types¡¡like¡¡Double¡¡and¡¡String£»¡¡which¡¡are¡¡CLR¡provided¡¡¡¡
types¡£¡¡For¡¡the¡¡example¡¡in¡¡this¡¡chapter£»¡¡we¡¡will¡¡define¡¡our¡¡own¡¡type£»¡¡which¡¡we¡¡will¡¡use¡¡to¡¡repre
sent¡¡a¡¡node¡¡in¡¡the¡¡tree¡£¡¡
Declaring¡¡Structures¡¡and¡¡Classes¡¡
A¡¡user¡defined¡¡type¡¡can¡¡be¡¡implemented¡¡in¡¡one¡¡of¡¡two¡¡ways£º¡¡as¡¡a¡¡structure¡¡or¡¡as¡¡a¡¡class¡£¡¡An¡¡¡¡
example¡¡of¡¡each¡¡is¡¡shown¡¡in¡¡Figure¡¡4¡3¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡A¡¡type¡¡declaration¡¡requireA¡¡type¡¡declaration¡¡requiress¡¡a¡¡¡¡a¡¡CCLRLR¡¡In¡¡Visual¡¡Basic£»¡¡an¡¡appropriate¡¡
¡¡¡¡¡¡¡¡conconsstruct¡¡£¨e¡£g¡££»¡¡truct¡¡£¨e¡£g¡£¡¡SStructure¡¡or¡¡tructure¡¡or¡¡CClalassss£©¡¡and£©¡¡and¡¡keyword¡¡£¨e¡£g¡££»¡¡Class£©¡¡opens¡¡a¡¡block¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡an¡¡identifier¡¡£¨e¡£g¡££»¡¡Node£©an¡¡identifier¡¡£¨e¡£g¡£¡¡Node£©¡¡To¡¡close¡¡the¡¡block£»¡¡the¡¡keyword¡¡is¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bined¡¡with¡¡the¡¡End¡¡identifier¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Everything¡¡in¡¡between¡¡belongs¡¡to¡¡
¡¡Structure¡¡Node¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡Node¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡keyword¡£¡¡So£»¡¡if¡¡the¡¡keyword¡¡is¡¡
¡¡End¡¡Structure¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class£»¡¡everything¡¡within¡¡the¡¡block¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡belongs¡¡to¡¡the¡¡creation¡¡of¡¡a¡¡class¡¡
¡¡¡¡¡¡Structure¡¡is¡¡used¡¡to¡¡define¡¡
¡¡¡¡¡¡a¡¡custom¡¡value¡¡type¡¡£¨e¡£g¡££»¡¡
¡¡¡¡¡¡¡¡Double£©£»¡¡which¡¡has¡¡some¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡is¡¡used¡¡to¡¡define¡¡a¡¡custom¡¡reference¡¡type¡¡
¡¡¡¡restrictions¡¡and¡¡is¡¡used¡¡only¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨e¡£g¡££»¡¡String£©£»¡¡which¡¡is¡¡the¡¡most¡¡mon¡¡type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡certain¡¡situations¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡used¡¡for¡¡development¡¡
Figure¡¡4¡3¡£¡¡Choices¡¡for¡¡implementing¡¡the¡¡Node¡¡custom¡¡type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡As¡¡shown¡¡in¡¡Figure¡¡4¡3£»¡¡you¡¡can¡¡choose¡¡to¡¡create¡¡a¡¡user¡defined¡¡type¡¡as¡¡a¡¡value¡¡type¡¡¡¡
£¨Structure£©¡¡or¡¡reference¡¡type¡¡£¨Class£©¡¡type¡£¡¡For¡¡the¡¡most¡¡part£»¡¡developers¡¡use¡¡a¡¡reference¡¡type£»¡¡¡¡
because¡¡it¡¡has¡¡fewer¡¡constraints¡¡and¡¡is¡¡easiest¡¡to¡¡use¡¡in¡¡a¡¡general¡¡context¡£¡¡A¡¡value¡¡type¡¡has¡¡some¡¡¡¡
constraints¡¡due¡¡its¡¡behavior¡¡of¡¡storing¡¡everything¡¡on¡¡the¡¡stack£»¡¡as¡¡discussed¡¡in¡¡the¡¡following¡¡¡¡
sections¡£¡¡
Value¡¡Type¡¡Constraints¡¡
The¡¡constraints¡¡of¡¡using¡¡a¡¡value¡¡type¡¡relate¡¡to¡¡the¡¡fact¡¡that¡¡data¡¡is¡¡copied¡£¡¡This¡¡has¡¡an¡¡impact¡¡on¡¡¡¡
what¡¡happens¡¡when¡¡you¡¡embed¡¡reference¡¡types¡¡in¡¡value¡¡types¡¡and¡¡use¡¡value¡¡types¡¡as¡¡parame
ters¡¡to¡¡methods¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡104¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
82¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Effects¡¡of¡¡Copying¡¡Data¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡one¡¡¡¡value¡¡type¡¡is¡¡assigned¡¡to¡¡another¡¡value¡¡type£»¡¡the¡¡contents¡¡of¡¡the¡¡value¡¡types¡¡are¡¡copied¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡see¡¡this¡¡in¡¡action£»¡¡consider¡¡the¡¡declarations¡¡in¡¡Figure¡¡4¡4¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaration¡¡of¡¡the¡¡identifier¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Custom¡¡value¡¡type¡¡declaration¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡is¡¡inside¡¡the¡¡block£»¡¡so¡¡it¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡belongs¡¡to¡¡the¡¡custom¡¡type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Structure¡¡MyValueType¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Declaration¡¡of¡¡value¡¡is¡¡associated¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Structure¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡a¡¡type¡¡identifier¡¡£¨Integer£©¡¡and¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡MyReferenceType¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡scope¡¡identifier¡¡£¨Public£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡value¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Custom¡¡reference¡¡type¡¡declaration¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Both¡¡custom¡¡types¡¡declare¡¡a¡¡variable¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡is¡¡a¡¡value¡¡type£»¡¡which¡¡is¡¡called¡¡a¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡member¡¡because¡¡it¡¡belongs¡¡to¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡custom¡¡type¡¡declaration¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡4¡£¡¡Declaring¡¡custom¡¡types¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡