数据结构与算法


I     简要目录    1  00.课程大纲     8  01.绪论  8  (a) 计算  15  (b) 大 O 记号  32  (c) 算法分析  45  (d) 迭代与递归  65  (x1) 局限  75  (x2) 排序与下界     86  02.向量  86  (a) 接口与实现  95  (b) 可扩充向量  102  (c) 无序向量  112  (d) 有序向量  137  (e) 起泡排序  141  (f) 归并排序     148  03.列表  148  (a) 接口与实现  159  (b) 无序列表  168  (c) 有序列表  171  (d) 选择排序  177  (e) 插入排序  182  (f) 归并排序  185  (x1) 游标实现  188  (x2) Java 序列  195 (x3) Python 列表    200 04.栈与队列  200 (a) 栈接口与实现  204 (b) 栈与递归  210 (c1) 栈应用:进制转换  216 (c2) 栈应用:括号匹配  228 (c3) 栈应用:中缀表达式求值  240 (c4) 栈应用:逆波兰表达式  248 (d1) 试探回溯法:八皇后  259 (d2) 试探回溯法:迷宫寻径  264 (e) 队列接口与实现  268 (f) 队列应用  272 (x) Steap+Queap    275 05.树  275 (a) 概述  282 (b) 树的表示  288 (c) 二叉树概述  294 (d) 二叉树实现  303 (e1) 先序遍历  314 (e2) 中序遍历  324 (e3) 后序遍历  335 (e4) 层次遍历  342 (f) PFC 编码树  349 (g) Huffman 树    II     362  06.图  362  (a) 概述  367  (b) 接口与实现  387  (c) 广度优先搜索  398  (d) 深度优先搜索  416  (e) 拓扑排序  423  (f) 优先级搜索  427  (g) Prim 算法  441  (h) Dijkstra 算法  457  (x1) 关节点与双连通分量  470  (x2) Kruskal 算法  484  (x3) Floyd‐Warshall 算法     491  07.二叉搜索树  491  (a) 接口  497  (b) 算法及实现  507  (c) 平衡性与等价性  514  (d) AVL 树     528  08.高级搜索树  528  (a) 伸展树  546  (b) B‐树  580  (x1) 红黑树  612  (x2) kd‐树  633  (x3) 更多变种     647  09.词典  647  (a) 循值访问  657  (b) 散列  667 (c) 散列函数  678 (d) 排解冲突  693 (e) 桶排序与基数排序  702 (x1) 跳转表  716 (x2) MD5    723 10.优先级队列  723 (a) 基本实现  731 (b) 完全二叉堆  748 (c) 锦标赛排序  754 (d) 堆排序  761 (x1) 左式堆  773 (x2) 多叉堆    780 11.串  780 (a) ADT  785 (b) 串匹配  794 (c) KMP 算法  813 (d) BM 算法  830 (e) Karp‐Rabin 算法    839 12.排序  839 (a) 快速排序  856 (b) 中位数  865 (x1) 选取  879 (x2) 希尔排序    8 E.Dijkstra whysurgeryisnotcalledknifescience. computingscience,forthesamereason Computerscienceshouldbecalled (a)(a)ਗ਼ॠਗ਼ॠ 1.1.ঠ੥ঠ੥ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 7 55 к৊যઞࡼк৊যઞࡼ...... բ▁গ߄ոॠࡄͧҿѼؗࢻЈؿࣼբͧஏ壝Ѫગૹբ▁গ߄ոॠࡄͧҿѼؗࢻЈؿࣼբͧஏ壝Ѫગૹ http://thudsa.3322.org/~deng/ds/demohttp://thudsa.3322.org/~deng/ds/demo 55 ݟ؅ࢇठݟ؅ࢇठ http://thudsa.3322.org/oj/http://thudsa.3322.org/oj/ 55 ঩सҀЎ֧঎੪࡚঩सҀЎ֧঎੪࡚ ग़ࣟԙग़ࣟԙ””ͣफЇͤͣफЇͤ““ۋͤڏҷͣۋͤڏग़ࣟ͵ग़ࣟ͵ ““੿स੝੥੿स੝੥””ͣҷ ঩੯澝૜ਸ਼ܯ঩੯澝૜ਸ਼ՔࣻܯՔࣻ Жठٖ҅सיЖठٖ҅सיіऑ͵іऑ͵ VSVS--20082008੕ӏݱߥ੕ӏݱߥDSACPP.slnDSACPP.slnͧլͧլ5050 ੣Т͵੣Т͵ ࣔ׾୒੾࢝澝ۙԬੇਜ਼࢝ࣔ׾୒੾࢝澝ۙԬੇਜ਼࢝ 55 যছ؅ׂযছ؅ׂ ؅Эઞࡼ؅Эઞࡼ Data Structures & Algorithms (Fall 2012), Tsinghua University 6 6 7-302-29652-2 愍⚾恁 ॕз࢝ޤ9ٵ2012 গ߄ͣC++੶੗࢝ͤܨݤ 7-111-17775-4 J. E. Hopcroft, et al Computer Algorithms The Design & Analysis of 7-302-18696-0 E. Horowitzॗਚͧ㬃ќ䂸੯ গ߄ׁकͣC੶੗࢝ͤܨݤ 7-115-13984-9 M. A. WeissԼਚͧୟભݘ঩ গ߄ЉॠࡄӢ߅ܨݤ 7-302-14811-1 ॗނ㼝ъ গ߄ͣC++੶੗࢝ͤܨݤ 7-302-14751-0ͣο30ଡ଼CDͤ 7-302-02368-5ͣο22ͤ Д噺ݞॗ গ߄ͣC੶੗࢝ͤܨݤ ݟ޶Љݟોݟ޶Љݟો Data Structures & Algorithms (Fall 2012), Tsinghua University 5 5 ЈѪҕۜشݦ澝Ӕगͧ২؎ރЈѪҕۜՐੌࡉشݦ澝Ӕगͧ২؎ރՐੌࡉ ЗߗࡉЗߗࡉ””ރבіऑރב㞱ЈӔͺࡥՂࣩъճ澝ݧࢳ澝ઞࡼॗͧஏ֧““іऑܔڅ㞱ЈӔͺࡥՂࣩъճ澝ݧࢳ澝ઞࡼॗͧஏ֧ؽܔڅ55 ؽ ࡚ੰ࣐࡚҅ੰ࣐҅ গ߄ࣩ੦ਗ਼Љ૲࣐ݱߥܨগ߄ࣩ੦ਗ਼Љ૲࣐ݱߥॠࡄՂݤܨॠࡄՂݤ ࣂ੕Ղ੕ங㓢ષڿࣂ੕Ղ੕ங㓢ષஙڿ55 ьСՔјрࡗͺьСՔјрࡗͺ ங ৩୹բࣩѰіऑجરјۋ৩୹բࣩѰіऑࡼіऑͧجરјۋ55 ьСЈՔјͺьСЈՔјͺ ࡼіऑͧ 澝DFSDFSॗॗٽܭૻڼ澝澝ٽܭૻڼзӢߓۨ澝KMPKMP澝澝DijkstraDijkstra澝͵טзӢߓۨ澝ߗӔॠࡄୣ͵טߗӔॠࡄୣ http://http://MOSS.stanford.eduMOSS.stanford.edu ———— څڅіऑіऑ୹բ୹բ Ӕग֩ͧӭؓьСͤޟѽӭؓͣױӔग֩ͧӭؓьСͤޟѽӭؓͣױ 55 نժբࣀ澝բॸ澝բފՄݱբ੥ͧЈҀԙӢͶݸ੥نժբࣀ澝բॸ澝բފՄݱբ੥ͧЈҀԙӢͶݸ੥ ীஙӢҕۜͣ੢--100100ͤͤૣͧۉ؎ীஙӢҕۜͣ੢঩सҀЎஏࢮैૣͧۉ؎55 ঩सҀЎஏࢮै ি੪Ղੌ࠭ি੪Ղੌ࠭ Data Structures & Algorithms (Fall 2012), Tsinghua University 4 4 ޶ݲݟ֛޶ݲݟ ӸГմӸГմӃܶрЖъफ़Զո࣓੻ӃܶрЖъफ़Զո࣓੻֛ 55 څૡٰࠖڅࢮै㓢িࢮै㓢ি + + ૡٰࠖ + + ڿҀЎӪڿ55 ԅӢԅӢ ԿЉрࡗԿЉрࡗ + + ҀЎӪ Ͷׁޯݱࡄո۪ࣩ٘䍫ࡕ૜࣐ܸܬͶׁޯݱࡄո۪ࣩ٘䍫ࡕ૜ׁ࣐ޯऍ੫䎯ࣩҬӢܸܬ55 ॒ੰ॒ੰ ׁޯऍ੫䎯ࣩҬӢ ݿӃ֧ग़ࣟԙܶӞ࣓੬֧ग़ࣟԙܶӞ࣓੬سݿӃس働ҷ١УդͧՔи7272ۉ働ҷ١УդͧՔиۉ ࣲࣧ澝ுࣲ࡚ੰগ՟ͧগ߈ࠔग㓬ӂஐіऑ঩Ӊઔଞࣲࣧ澝ுࣲ࡚ੰগ՟ͧগ߈ࠔग㓬ӂஐіऑ঩Ӊઔଞ ݿ15%15%ॽਗ਼۲ۜॽਗ਼۲ۜسݿسࠠ1212ܘ働ۉࠠЅݺӃՔ֧ग़ࣟԙ਼рͧܘ働ۉЅݺӃՔ֧ग़ࣟԙ਼рͧ ੠բݿઘӞMD5MD5ऑऑڎ੠բݿઘӞڎрͧܶͤޏմҸ֧יрͧୡݿͣܶͤޏմҸ֧י55 ঩सҀЎ঩सҀЎ ୡݿͣ 0024007400240074͵͵ ঩सҀЎ঩सҀЎ x 4x 4ࠊࠊ x 4x 4ஙங + + ԅӢԅӢ ঩सҀЎ঩सҀЎ x 3x 3ࠊࠊ x 5x 5ஙங + + ॒ੰ॒ੰ x 2x 2ࠊࠊ + + ԅӢԅӢ ͵͵3024018430240184 ͤઁڲͤઁڲؗଢ଼㖊ӐͧޫޮՔৗҞ㔀Ѽ㔀Ѽܨؗଢ଼㖊ӐͧޫޮՔৗҞৼͣߠܨ55 িߟࢺিߟࢺৼͣߠ ি੪Ղੌ࠭ি੪Ղੌ࠭ Data Structures & Algorithms (Fall 2012), Tsinghua University 3 3 Уј㔗ܕҴਙؗͧۯУј㔗ܕҴਙؗͧۯ ੝੥ࣩЭ㖴י澝ەԇי㓢ি澝י੝੥ࣩЭ㖴י澝ەԇי㓢ি澝י ոٴ㓆ڵगͧރոࣹߗٴ㓆ڵगͧރ࣐ࣩҼય ࣹߗڀਗ਼ॠ޲य؅Љت࣐ࣩҼયڀਗ਼ॠ޲य؅Љت গ߄ܨݤװগ߄ৗժ؅ܨݤװ޻ѝ————ৗժ؅יޟ޻ѝיޟ 55 ࡚ੰۊդ▁ࠊ঩सҀЎͧૡ▁ࠖ১ޣ࡚ੰࠊոۊդ▁ࠊ঩सҀЎͧૡ▁ࠖ১ޣॕॕ00ࠊոنکنکੰҞੰҞ ࣩ঩सׁकؓ▁ޥ੦ਗ਼ͧٽࣩ঩सׁक੶੗सؓ▁ޥ੦ਗ਼ͧٽٜҒٜҒC/C++C/C++੶੗स গ߄ܨগ߄Քժ૲Ғݤܨ55 ׁޯ޻ѝׁޯ޻ѝ————Քժ૲Ғݤ ޳澝ஸ߶ոளݝࣩ૆ѝו੐ࠆ澝ל޳澝ஸ߶ոளݝࣩ૆ѝ૆ѝٖस͵ԿЉ֜୓঩Ӊו੐ࠆ澝ל૆ѝٖस͵ԿЉ֜୓঩Ӊ ࣩॠࡄܨ੐ࠆݤלࣂדগ߄Љॠࡄ͵ؗࢻளݝܨࣩॠࡄݤܨ੐ࠆݤלࣂדগ߄Љॠࡄ͵ؗࢻளݝܨݤ ٽ੦ਗ਼੶੗͵঩ӉӞ՟ࡄࣩसٽसٽ੦ਗ਼੶੗͵঩ӉӞ՟ࡄࣩसٽस গ߄ܨժ୻ੌ૲Ғݤފগ߄ܨժ୻ੌ૲Ғݤފ————55 ࣹߗؓѸࣹߗؓѸ Ј૲ҒފЈ૲Ғ૲Ғͧ૟ފ૲Ғͧ૟ Data Structures & Algorithms (Fall 2012), Tsinghua University 2 2 㬮୩⮩ Ө஧恟 ㆰ朣 ؋Ӻ ⾗䰕ސ 愍⛮恁 ݟ٣ЉԈݟݟ٣ЉԈݟ Data Structures & Algorithms (Fall 2012), Tsinghua University 1 1 ইלই੿सל0.0.੿स JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 16 ձІԞમԍٮૹޯۉਗ਼ॠͧלձІԞમԍ੐ࠆ۞ٮૹޯۉਗ਼ॠͧל੐ࠆ۞ ૝ܯЬޯۉ૝ͧਗ਼ॠܯ૝੐ࠆܯЬޯۉ૝ͧਗ਼ॠܯ੐ࠆ ࣩОੌ֛ॹޯۉӏؓਗ਼ॠފککࣩОੌ֛ॹͧޯۉӏؓਗ਼ॠފککୋஙࣩؗ҅੐ࠆ੐ࠆͧ͵اୋஙࣩؗ҅੏͵ا55 ੏ ͺ܍ঈࠃڠѽױͺ܍ঈࠃڠѽױ יימՔৗӞࢻࣩୋஙؗ҅מࠣौ......ՔৗӞࢻࣩୋஙؗ҅ͧלТЈڿࠣौͧלТЈڿ ޯۉࣩਗ਼ॠޯۉTTAA(P)=(P)=ॠࡄॠࡄAA࠭੕ୋஙؗ҅࠭੕ୋஙؗ҅PPࣩਗ਼ॠ͵اি͵ا55 ি ѽࠢ૊ͺױଞͺڅѽױѽࠢ૊ͺױଞͺڅѽױ Ҥी୎؁୻ےҤी୎؁୻ے++ ૜ਸ਼ݿ୎૜ਸ਼ݿ୎ ͵͵ޯۉޯۉ ......ͺՔЈଊСफ़ԤރͺՔЈଊСफ़Ԥݤ؅੩ރݤ؅੩ ࠔग㓬͵ࠔग㓬͵ ॠࡄԄৗЉୋஙੌ࠭▁৩ͺॠࡄԄৗЉୋஙੌ࠭▁৩ͺ 55 ГЖОੌݱ஄ГЖОੌݱ஄ ॠࡄӢ߅ॠࡄӢ߅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 15 LordKelvin Ifyoucannotmeasureit,youcannotimproveit. Tomeasureistoknow. ރȉȉ୦䄞 டںҍࠌ࢐ͧڿѪޥࠠ ੾ЯЈ࠭࣎੕װ OO੢ՙ੢ՙלל((b)(b 1.1.ঠ੥ঠ੥ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 14 વ઼澝Ӣ㢉Ӣ߅ڠવ઼澝Ӣ㢉Ӣ߅૶ܱһॸ澝૶ڠࡰૡӢ߅Љࣼһ੢ՙࡰૡӢ߅Љࣼһ੢ՙ૶ܱһॸ澝૶ Ӣ߅ݱࡄڅ޳וӢ߅ݱࡄׁޯࣩڅ޳ו55 ׁޯࣩ 澝Ӿ߉澝֚䆵ͧԇ㓆੐ӥܮ澝Ӿ߉澝֚䆵ͧԇ㓆੐ӥੰܮੰ 澝Ӣ࠿澝Ӗ࠿ڠ૶ͧڵ澝Ӣ࠿澝Ӗ࠿૨і澝帙ڠ૶ͧڵ૨і澝帙 ړӀֹࣩॠࡄ੦ਗ਼ज़ࣛЉࠆړ55 Ӏֹࣩॠࡄ੦ਗ਼ज़ࣛЉࠆ ԘକͧݢӦͧӘѽߓۨړԘକͧݢӦͧӘѽߓۨࠆړࠆ Љ૲ՇͧଁԶٽܭոзӢߓۨͧٽЉ૲ՇͧଁԶஎٽܭոзӢߓۨͧٽஎ ࣐۪٘ٮݝॠࡄࠆֱࣩޥ࣐۪٘߄ૼٮݝॠࡄࠆֱࣩޥ55 ߄ૼ ࣐ڀӀͣݢӦ਽֢ͤͧ澝ࣼһॠࡄՂ؀ͧͤ׃࣐ѨҮ୓ӦͣڀӀͣݢӦ਽֢ͤͧ澝ࣼһॠࡄՂ؀ͧͤ׃ѨҮ୓Ӧͣ ਺зӢߓۨߚͤٴӦͣզଞ澝Ӧ਽ͤͧߚͣअଜиٽ਺зӢߓۨߚͤٴӦͣզଞ澝Ӧ਽ͤͧߚͣअଜиٽ ՌՂҾЈբؗࢻܯՌՂҾЈբؗࢻܯগ߄ࣩADTADTܨগ߄ࣩ՞ݤܨ55 ՞ݤ উ੒؟উ੒Ӄ؟Ӄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 13 গ߄ͧ੕ӏؗଢ଼ୋஙܨࣩݤڀتগ߄ͧ੕ӏؗଢ଼ୋங䍫ࡕ֩૲࣐՞६ׁͣޯͤॠࡄՂܨࣩݤڀت䍫ࡕ֩૲࣐՞६ׁͣޯͤॠࡄՂ ࣐ࢺ׊ڀগ߄૯࣐ࣩܨ࣐ࢺ׊г੕՞६ݤڀগ߄૯࣐ࣩܨг੕՞६ݤ গ߄У୎ࣩӃ֧৊ॸܨগ߄У୎ࣩӃ֧৊ॸࣂ੕՞६ׁͣޯͤॠࡄЉЈբݤܨࣂ੕՞६ׁͣޯͤॠࡄЉЈբݤ Ռܯগ߄Ԅৗ澝਽ठ澝ؗࢻոׁޯݒҀܨ՞६ݤܸܬՌܯগ߄Ԅৗ澝਽ठ澝ؗࢻոׁޯݒҀܨ՞६ݤܸܬ ܨݤܦҤ澝ঐ঒澝ѭ૶ոૃ؁ݝ֩ޥளݝ֩Ӯ࣐ਗ਼ॠ޲ͧܨݤܦҤ澝ঐ঒澝ѭ૶ոૃ؁ݝ֩ޥளݝ֩Ӯ࣐ਗ਼ॠ޲ͧ ؅Ѫੌފগ߄ͧػܨ؅Ѫ؅Эݤੌފগ߄ͧػܨ55 ؅Эݤ ॠࡄ੦ਗ਼ոӢ߅ࣩОੌ۪٘Љٖҿॠࡄ੦ਗ਼ոӢ߅ࣩОੌ۪٘Љٖҿ গ߄੦ਗ਼ոؗࢻׁࣩޯԼࣂЉݱࡄܨগ߄੦ਗ਼ոؗࢻׁࣩޯԼࣂЉݱࡄ՞६ݤܨਇ֟ ՞६ݤܪਇ֟੣ܪ55 ੣ ੦ਗ਼Љؗࢻளݝॠࡄׁࣩऐފ੦ਗ਼Љؗࢻளݝॠࡄׁࣩऐފ ଑Ӣۉਗ਼ॠ޲य؅ࣩଜੌঐފ଑Ӣۉਗ਼ॠ޲य؅ࣩଜੌঐފ Ѹ঴ڵиߟדѸ঴֧ਗ਼ॠ޲ࣼһЊЎ੿सѼॸЗͧ▁ࣻڵиߟדগ߄ ֧ਗ਼ॠ޲ࣼһЊЎ੿सѼॸЗͧ▁ࣻܨগ߄ݤܨ55 ݤ Нѽੌ؅ͺ؅ьСͺ؅ЭࣹߗͺНѽੌ؅ͺ؅ьСͺ؅Эࣹߗͺ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 12 (Algorithms+DataStructures)xEfficiency=Computation(Algorithms+DataStructures)xEfficiency=Computation Algorithms+DataStructures=ProgramsAlgorithms+DataStructures=Programs //N.Wirth,1976//N.Wirth,1976 شҤी୎ؿՔৗ؁ͶڼؿՔৗڅૻشҤी୎ؿՔৗ؁ͶڼؿՔৗڅ55 ݝࢴ͵ݝࢴ͵ ૻ 55 Ք੾͵Ք੾͵ গ߄Ԗগ߄Ԗ ++ӔगշգӔगշգ ++ࡉଚࡉଚ +...+... ૭ӞٮࣂͧুЈ৩ஂࠔדڡҞ૯ٶ૭Ӟৗ૓ӯЈ՟ࡄࣩ૎ҴٮࣂͧুЈ৩ஂࠔדڡҞ૯ٶ55 Ҡ⺡͵Ҡ⺡͵ ৗ૓ӯЈ՟ࡄࣩ૎Ҵ ՟ࡄࣩ૎Ҵڿࣂџדࠔगכ՟ࡄࣩ૎Ҵৗڿࣂџדࠔगכৗ ࣂ૭Ԗࣩ૎Ҵדࠔगכࣂ૭Ԗࣩ૎Ҵৗדࠔगכৗ ࣂ▁ৱ㓬ࣩ૎Ҵדࠔगכࣂ▁ৱ㓬ࣩ૎Ҵৗדࠔगכৗ ੐ࠆࣩ૎Ҵלࣂדࠔगכ੐ࠆࣩ૎Ҵৗלࣂדࠔगכৗ ࣂफ़Ԥࣩ૎Ҵדࠔगכࣂफ़Ԥࣩ૎Ҵৗדࠔगכৗ ܯ঩੯澝଴כ՟੶ࡄͧৗ॔ܯ঩੯澝଴כ55 ࠔग͵ࠔग͵ ॔՟੶ࡄͧৗ ॠࡄװॠࡄװ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 11 ...... ...... Ӱ૎ӞڰिࠊׁޯݒҀͧ଒Քјޥиџѽ૎ҴͧকتӰ૎ӞڰिࠊׁޯݒҀͧ଒Քјޥиџѽ૎Ҵͧকت ि㓬ޥि㓬ޥ ۉ؎ݤݿ୎Ӄٮࠠ▁ׁޯݒҀ଒ՔؗࢻͧЋ֧ۉ؎ݤݿ୎ӃٮՔਸ਼㓬Քਸ਼㓬 ࠠ▁ׁޯݒҀ଒ՔؗࢻͧЋ֧ ӦٽࣩۉӦџ▁ॠࡄ଒Քјܵ૩Н▁Жׁ࣒ޯݒҀঐٽࣩۉगؓ㓬गؓ㓬 џ▁ॠࡄ଒Քјܵ૩Н▁Жׁ࣒ޯݒҀঐ ࣩୋஙؓܗࣩୋஙࣩगՔј੕ӏؓܗࠔग㓬ࠔग㓬 ࣩगՔј੕ӏ ࣂࣩґ㔴ͣग़ߥͤדࣂࣩґ㔴ͣग़ߥͤকד૎Ӟ૎Ӟ ক ࣂࣩґ㔴ͣୋஙͤדڬࣂࣩґ㔴ͣୋஙͤדڬ ૎Ҵ૎Ҵ Ӧٽїܗ઄ॠࡄͧԮࢤؓਗ਼ॠࠆֹЇͧݽ֧੕ӏࢤؓୋஙࣩےӦٽїܗ઄ॠࡄͧԮࢤؓਗ਼ॠࠆֹЇͧݽ֧੕ӏࢤؓୋஙࣩے 55 ࣂٖҿ ////ؽ੐޲澝僕ॺ޲澝ؽ੐޲澝僕ॺ޲澝֢䍫޲֢䍫޲......דࣂٖҿґ㔴ד55 ਗ਼ॠࠆֹਗ਼ॠࠆֹ ==ਗ਼ॠ޲ਗ਼ॠ޲ ==ґ㔴 ࣂדࣂґ㔴ד55 ਗ਼ॠਗ਼ॠ ==ґ㔴 ॠࡄॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 10 A B DC B' D' C' A B  ਸ਼঎ٴ䎯ͧҞ▁ט૚ࣻ঎͵ٽਸ਼঎׾सٴ䎯ͧҞ▁ט૚ࣻ঎͵ٽ55 ׾स Јৗ੕ӏьСୋஙͺЈৗ੕ӏьСୋஙͺ ////ЅॗӢ੔ЅॗӢ੔ //ҔݱҔݱ /.../... ੕ӏьСୋஙͺכ੕ӏьСୋஙͺ؈ৗכ؈ৗ ьСͺފьСͺૠଛࣩਗ਼ॠ޲ފ55 ૠଛࣩਗ਼ॠ޲ ਸ਼঎ͧрABABииCCٴਸ਼঎ͧрࣩٴককC'C'ҞҞB'BB'Bࣩ ਸ਼঎ͧрABABииDDٴਸ਼঎ͧрࣩٴককD'D'ҞҞB'BB'Bࣩ B'BB'Bܯ৊ܯ৊ ֧֧ІՇІՇAC'=C'D'=D'B'AC'=C'D'=D'B' ঎د঎Јଜ՟ࣩد55 ॠࡄॠࡄ ёёAAՆӞ▁޻ЉՆӞ▁޻ЉABABЈଜ՟ࣩ ABABЅॗӢЅॗӢذ͵૎Ӟذ͵૎Ӟ ஄Іџ▁঎ࠝABABٴ͵஄Іџ▁঎ࠝ૎Ҵٴ͵55 ૎Ҵ ؽ੐ਗ਼ॠ޲ՂҾॠࡄؽ੐ਗ਼ॠ޲ՂҾॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 9 C B l A l A ੕ӏьСୋஙͺЈৗ੕ӏьСୋஙͺכ੕ӏьСୋஙͺЈৗ੕ӏьСୋஙͺ؈ৗכ؈ৗ ьСͺފьСͺૠଛࣩਗ਼ॠ޲ފ55 ૠଛࣩਗ਼ॠ޲ ZZ ............ ӽѾࣩ88ࠝ僕ॺ㝄ࣻࠝ僕ॺ㝄ࣻذӽѾࣩशԇੴ䎯ͧذZZ शԇੴ䎯ͧ ZZ ࡀՏ▁ݱզۨӰॕࡀՏ▁ݱզۨӰॕ33ࠝ僕ॺࣩও䎯ࠝ僕ॺࣩও䎯 ؓ֠ٶ㝄ࣻؓ֠ٶ44ࠝ僕ॺࡀࠝ僕ॺࡀll㝄ࣻذ䎯ફͧذZZ ёёAA䎯ફͧ ࢺۉܯ৊ـࢺࠝॗେࣩ僕ॺͧநۉܯ৊ـZZ ՇՇ1212ࠝॗେࣩ僕ॺͧந 55 ॠࡄͣॠࡄͣ2000B.C.2000B.C.ֻͧՂъֻͤͧՂъͤ ૎Ӟ͵ক૚૎Ӟ͵ক૚AAҞҞllࣩ▁޻ַ঎ࣩ▁޻ַ঎ 55 ୋஙୋங ૎Ҵ͵џচࣻ঎૎Ҵ͵џচࣻ঎llՂҾІ▁䎯ՂҾІ▁䎯AA 僕ॺਗ਼ॠ޲ՂҾॠࡄ僕ॺਗ਼ॠ޲ՂҾॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 24 ͜څ͜୰څѹгͺ //P//P୰מժފѹгͺૠЖߗӔמժފૠЖߗӔ ࢐ু......ͧڿੜНٜՔїъࢁٮ࢐ুૠ६ॠࡄࣩݝࢴૹͧڿੜНٜՔїъࢁٮ55 ૠ६ॠࡄࣩݝࢴૹ 55 ㍖͵㍖͵sqrt(nsqrt(n20122012 24n24n20112011 +512n+512n20102010 +...+1978)=+...+1978)=OO(n(n10061006)) 55 ёёOO(n)(n)ӰӰOO(n(n22)) OO(n)(n)६Ӡݤ६Ӡݤޥےޥے͵ͤ͵55 ঎㓬ͣ঎㓬ͣlinearfunctionlinearfunctionͤ ZZ ▁ৱ֩͵▁ৱ֩͵aakknnkk +a+akk11nnkk11 +...+a+...+a11n+an+a00 ==OO(n(nkk),a),akk >0>0 ZZ (2012n(2012n22 20)/(1999n20)/(1999n 1)=1)=OO(n(n22/n)=/n)=OO(n)(n) ZZ (100n(100n 500)(20n500)(20n22 300n+2012)=300n+2012)=OO(n(n nn22)=)=OO(n(n33)) ZZ 100n+200=100n+200=OO(n)(n) polynomialfunctionpolynomialfunctionͤͤͣړ஍יͣړ஍י 55 OO(n(ncc)) DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 23  c>0,logc>0,lognn ==OO((nncc)) ݤٮ૝иܯݸୡڅ޳וݝͧޥٮݤૠ६ॠࡄஂٮ૝иܯݸୡڅ޳וݝͧޥٮ55 ૠ६ॠࡄஂ 123*log123*log321321nn +log+log105105((nn22nn+1)+1) ployploylogfunctionlogfunctionͤͤͣړ஍יݤتͣړ஍יݤت 55  c>0,c>0,loglognncc ==cc44loglognn ==(logn)(logn) ઄ےݤࠊ㍖ݸٮ઄ےݤࠊ㍖ݸٮ 55  a,b>0,a,b>0,loglogaann ==loglogaabb 44 loglogbbnn ==((loglogbbnn)) ઄ےݤݸځٮ઄ےݤݸځٮ 55 lnlnnn ||lglgnn |log|log100100nn |log|log20122012nn ݤͺځރݤͺНѽЈࡉځރݤ OO(log(lognn)) ////НѽЈࡉتݤت 55 OO(log(logccnn)) DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 22 ZZ ...... ਸ਼ԮՔͺ۝ٽஎފਸ਼ԮՔͺҵ۝ٽஎފZZ ҵ ࣐ͺઁͤڠ૶ͣޥ࣐ͺ▁ؓЈৗઁͤڠ૶ͣޥZZ ▁ؓЈৗ ZZ ▁ؓЈլӢݖૃզͺ▁ؓЈլӢݖૃզͺ if(1+1!=2)gotoUNREACHABLE;if(1+1!=2)gotoUNREACHABLE; /*log/*log**n*/n*/ for(i=n;i>2012;i=for(i=n;i>2012;i=log(ilog(i));)); ࢺͺ for(i=0;ic>c22>0>0ͧͧnn฾฾22դͧդͧcc11f(n)>f(n)>T(nT(n)>c)>c22f(n)f(n) T(nT(n)=)=((f(nf(n))))͵͵ c>0c>0ͧͧnn฾฾22դͧդͧT(nT(n)>)>ccf(nf(n)) 55 Ҿ؈੢ՙҾ؈੢ՙ ࡰૡӢ߅ࡰૡӢ߅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 20 ࣛ͵OO(n(naa ++nnbb)=)=OO(n(naa),a>b>0),a>b>0ھࣛ͵ѹࠊ஍Քھѹࠊ஍Ք ࣛ͵OO(f(n(f(n))=))=OO(c(c  f(nf(n))))ھॸݤՔٮ͵ࣛھॸݤՔٮ Ӹীࣩ׌େમԍކНफ़ࡍͧѷ҈࢐ՅޟӸীࣩ׌େમԍކНफ़ࡍͧѷ҈࢐Յޟ((55 ЉЉT(nT(n))ࣼࠢͧࣼࠢͧf(nf(n nn฾฾22դͧդͧT(nT(n)<)0c>0ͧ OO੢ՙͣ੢ՙͣbigbigOO notationnotationͤͤלל 55 ՔЈিਨͧНьСͺٮՔЈিਨͧНьСͺૹٮҤԤҪݤS(nS(n)=?)=? ////ૹ؁ҤԤҪݤ୻Ԩ࣐ࣩ؁୻Ԩ࣐ࣩ ਸ਼ׁࣩޯݒҀࠊݤT(nT(n)=?)=?۝ਸ਼ׁࣩޯݒҀࠊݤ୻۝୻ и੐ࠆНnn૎Ҵͧॠࡄ૎Ҵͧॠࡄتи੐ࠆНդͧتnn฾฾22դͧڡ͵ڡ͵AsymptoticanalysisAsymptoticanalysis ѽ׌େͺױޯۉդͧਗ਼ॠלכѽ׌େͺࡰૡӢ߅͵֧ୋங੐ࠆરױޯۉդͧਗ਼ॠלכ55 ࡰૡӢ߅͵֧ୋங੐ࠆર ࣩ׌େમԍޯۉاࣩ׌େમԍࣩୋஙͧࡉଜিޯۉاࣩୋஙͧࡉଜিלכરלכરڵһޟૠଛ͵ڿࡉڵһޟૠଛ͵ڿࡉ ѽ׌େͺױޯۉѽ׌େͺ֚ӰԼҮࣩୋங͵୩अୋங੐ࠆࣩ׌େͧਗ਼ॠױޯۉ55 ֚ӰԼҮࣩୋங͵୩अୋங੐ࠆࣩ׌େͧਗ਼ॠ ࡰૡӢ߅ࡰૡӢ߅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 19 ୻ׁࣩޯݒҀࠊݤے୻ׁࣩޯݒҀࠊݤୋஙےT(nT(n)=)=ॠࡄ֧ॠࡄ֧RAMRAMЗ࠭੕੐ࠆНЗ࠭੕੐ࠆНnnୋங ਸ਼ׁࣩޯݒҀࠊݤׁޯݒҀࠊݤ۝ਸ਼ࣩॠࡄ୻ੌ۝ॠࡄࣩॠࡄࣩ૜ਸ਼ݿ୎૜ਸ਼ݿ୎  ॠࡄ୻ੌ 55 ֧֧RAMRAMࠆֹЗࠆֹЗ ॠࡄࣩݝࢴҞՔґࣩࠢ૊Љ੪ӭتՕͧٴњՔјࢮैиҿѼࣩۊॠࡄࣩݝࢴҞՔґࣩࠢ૊Љ੪ӭ҄تՕͧٴњՔјࢮैиҿѼࣩۊ҄ ઊۻՕࣩफ़ԖЉٴৱਗ਼ॠ▁ފઊࠆֹۻՕࣩफ़ԖЉٴৱਗ਼ॠ▁ފ55 RAMRAMࠆֹ ੾ӉͣટҚͤ澝֙Ө૜ॠ澝ࠢ૊澝gotogoto澝澝callcall澝澝returnreturn֗؁أ੾ӉͣટҚͤ澝֙Ө૜ॠ澝ࠢ૊澝֗؁أ ાஂׁޯݒҀޯٽࢺՂ׾सڱાஂׁޯݒҀޯٽࢺՂ׾सڱ//// ͵ݤݿ୎ٮݤݿ୎͵э୻ٮࠠ▁ࠠ▁ׁޯݒҀׁޯݒҀэ୻ ࠕױہࠕѷױہୡӱ ////ѷޥ঩ՙͧ㔀ݤ࠻ٽஎ֗؁أୡӱޥ঩ՙͧ㔀ݤ࠻ٽஎ֗؁أ Ѽॸগ߄ͣ19451945ͤͧॗўиૹ࣐֢䍫޲ͤͧॗўиૹ࣐֢䍫޲ޠѼॸগ߄ͣ੽҈ޠ////ࡼ১⡦ࡼ১⡦੽҈ 55 RandomAccessMachineRandomAccessMachine (J.(J.SheperdsonSheperdson &H.Sturgis,1963)&H.Sturgis,1963) RAMRAM DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 18 Ӕग֩ܵ૩ॠࡄুܯӔग֩ܵ૩ॠࡄёুࣻুܯёুࣻ ੶੗ॗ֛ॹٽҿѼࣩͣளঃͤसۋ੶੗ॗ֛ॹЈ҈ઢиъٽҿѼࣩͣளঃͤसۋЈ҈ઢиъ ࠆֹۋՕٴࠆֹНচӞؙ੏ࣩ੪ӭͧ୻ੌ▁Жࣂ㖸ࣩۋՕٴ55 НচӞؙ੏ࣩ੪ӭͧ୻ੌ▁Жࣂ㖸ࣩ ૜ਸ਼иЈբࣩѼॸগ߄澝ݒҀॸঞ ......ٶ૜ਸ਼иЈբࣩѼॸগ߄澝ݒҀॸঞբ▁ॠࡄͧՔৗؗࢻٶբ▁ॠࡄͧՔৗؗࢻ բ▁ॠࡄͧՔৗ࣒Јբъ澝࣐Јբ੶੗澝কЈբ঩੯֗ؗࢻբ▁ॠࡄͧՔৗ࣒Јբъ澝࣐Јբ੶੗澝কЈբ঩੯֗ؗࢻ иЈբ६ֹࣩ૎Ҵڀ૯ޟиЈբ६ֹࣩ૎ҴЈբࣩॠࡄͧՔৗڀ૯ޟЈբࣩॠࡄͧՔৗ иЈբ੐ࠆࣩ૎Ҵڀ૯ޟиЈբ੐ࠆࣩ૎ҴЈբࣩॠࡄͧՔৗڀ૯ޟЈբࣩॠࡄͧՔৗ ͜כ͜Јરכ55 Јર ॠࡄࣩँࠔݝࢴͺކࣩݱࡄͧѷરјӔगՅܯࣻޣފॠࡄࣩँࠔݝࢴͺؗயঞਗ਼ކࣩݱࡄͧѷરјӔगՅܯࣻޣފ55 ؗயঞਗ਼ ѽ੪ӭҾѨ⤠ͺױमॠࡄͧיޥٮѽ੪ӭҾѨ⤠ͺբ▁ୋஙૹױमॠࡄͧיޥٮ55 բ▁ୋஙૹ ॠࡄॠࡄ vs.vs.ਗ਼ॠݝࢴਗ਼ॠݝࢴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 17 ՇT(nT(n)=)=max(T(Pmax(T(P)||P|=n})||P|=n}ފՇҐ୤ફ੎ͧ૟ފ55 Ґ୤ફ੎ͧ૟ ѽؓТT(nT(n))⩲ͺ⩲ͺױࠕͧՀੴױѽؓТݹ࢐ױࠕͧՀੴױ55 ݹ࢐ ࣩੱ ......װࣩੱѷ૜ࠩװѷ૜ࠩ C(n,3)C(n,3)मঐ՟मঐ՟ޥے߇Пੌͧٮૹޥے߇Пੌͧٮૹ ࣩЅЖ䎯سޣ஄ऴڤЅ੔ۉےࣩЅЖ䎯Ж䎯ЗͧۨӰسޣ஄ऴڤЅ੔ۉے஄ІࣩnnЖ䎯ЗͧۨӰٴ஄Іࣩ֧ٴ֧ ͵ױ҅͵ױ҅ 55 ؗઔٚӯޥЈؿࣼբͧ࣎২ޯۉؗઔٚӯբ▁ୋஙॗ੐ࠆࣩЈբؗ҅ͧਗ਼ॠޥЈؿࣼբͧ࣎২ޯۉ55 բ▁ୋஙॗ੐ࠆࣩЈբؗ҅ͧਗ਼ॠ ୋங ......ޥૠ▁ؓТѶЦѐͧފୋஙѷޥૠ▁ؓТѶЦѐͧފ55 ѷ ࣩୋஙͤݿͧफ़੢ҀT(nT(n))ڀتࣩୋஙͤݿͧफ़੢ҀͣՂҾڀت੝੥ࢤؓॠࡄ੝੥ࢤؓॠࡄAAͣՂҾ ޯۉ୻ࣩਗ਼ॠےࣩؗ҅ͧޯۉ୻ࣩਗ਼ॠےїїTTAA(n(n)=)=࣐ॠࡄ࣐ॠࡄAA࠭੕ߏ▁ୋங੐ࠆН࠭੕ߏ▁ୋங੐ࠆНnnࣩؗ҅ͧ 55 ޯۉਗ਼ॠޯۉୋங੐ࠆୋங੐ࠆ vs.vs.ਗ਼ॠ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 32 FrancoisArago aseaglessustainthemselvesintheair. Hecalculatedjustasmenbreathe, (c)(c)ॠࡄӢ߅ॠࡄӢ߅ 1.1.ঠ੥ঠ੥ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 31 55 Google:smallGoogle:smallonotationonotation ЈѹиkkSubsetSubsetڅЈѹиࣩ୰څ੩ժ͵(k+1)(k+1)SubsetSubsetࣩ୰ۋރ੩ժ͵੩ۋރ੩ 55 kkSubsetSubset͵џচݦݤ୳͵џচݦݤ୳SSͧӭؓͧӭؓSSՔժӢНՔժӢНkkЖЈр׾୳ͧҾո֭НЖЈр׾୳ͧҾո֭Н((ɪɪS)/kS)/k n!n! ==OO((nn20122012)) 22nn ==OO(n!)(n!) (n(n22 +1)/(2n+3)=+1)/(2n+3)=OO(n)(n) nn20122012 ==OO(n!)(n!) loglogddnn ==OO(n(ncc),),c>0,d>1c>0,d>1 loglog1.0011.001n=n=OO(log(n(log(n10011001)))) 12n+5=12n+5=OO(nlogn)(nlogn) loglog22(n(n10241024 2*n2*n66 +101)=+101)=OO(?)(?) ਗ਼ॠ͵ FibonacciFibonacciݤݤ Fib(nFib(n)=)=OO(2(2nn))ۋ澝੩ժރਗ਼ॠ͵੩ۋ澝੩ժރ55 ੩ ૜ਸ਼ݿ୎ͺا૜ਸ਼ݿ୎ͺࠆֹЗ਺ଞॠࡄݝࢴͧНьСՐ୻িا55 ֧֧RAMRAMࠆֹЗ਺ଞॠࡄݝࢴͧНьСՐ୻ি 55 ֧փоݱ஄ͧࢻіࣔ׾ਗ਼ॠ޲ѐޭ૖Ӱ֧փоݱ஄ͧࢻіࣔ׾ਗ਼ॠ޲ѐޭ૖ӰRAMRAMࠆֹࣩੌ࠭ͺࠆֹࣩੌ࠭ͺ բ▁ॠࡄࣩ࠭੕ݿ୎Քৗࡇԇ࣎২Їୠͧלբ▁ॠࡄࣩ࠭੕ݿ୎Քৗࡇԇ࣎২ЇୠП҅͵୩ୋஙؗ҅੐ࠆ׌ͧל55 П҅͵୩ୋஙؗ҅੐ࠆ׌ ੿դ੿դ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 16 30 ॠࡄ֧؁Јٶݤୋஙͧילॠࡄজ֧؁Јٶݤୋஙͧיל...... জ әॠࡄͧӆؿՔৗѨԖٴୋஙࣩיڭәॠࡄͧӆؿՔৗѨԖٴୋஙࣩיڭ څ޳וݤܗڅ޳וݤܗ ((OO(2(2nn ॠࡄࣩୋஙړ஍י֧؁ॠࡄࣩୋஙړ஍י֧؁== PPୋஙୋங څ޳וړ஍יڅ޳וړ஍י ݤٮݤٮOO(n(ncc))ͧͧcc ੎ ऎୗЪࡄऎୗЪࡄٮ੎ЈٮЈ څ޳וݱैڅ޳וOO(n(n33)) ैݱ ઊГГঐ՟ DijkstraDijkstraॠࡄॠࡄت૎ҴޥےઊГГঐ՟ت૎Ҵޥے څ޳וݱٴڅ޳וݱٴ ((OO(n(n22 澝EUEU澝澝HuffmanHuffman঩ऑ঩ऑٽܭ澝ٽܭ ѨޣڰӞࢻͧѷЈ੎ٮޣѨޣڰӞࢻͧѷЈ੎ٮޣ (OO(nlogn)(nlogn ૝঎㓬 ߏоЅ੔ӺӢॠࡄߏоЅ੔ӺӢॠࡄܯ૝঎㓬ӘЦܯOO(nloglogn)(nloglogn) ӘЦ ૝঎㓬 ߏоߏоMSTMSTॠࡄॠࡄܯ૝঎㓬ӘЦӘЦӘЦܯOO(nlog(nlog**n)n) ӘЦӘЦӘЦ ଀Ӱ ߚ澝֢ࣩଁԶߚ澝֢ࣩଁԶٮ଀ӰԉԂࣹߗͧকٮԉԂࣹߗͧক څ޳ו঎㓬څ޳וOO(n)(n) ঎㓬 澝੮Ӏࣩߓੳ澝ܷҴЉӬୣ׃澝੮Ӏࣩߓੳ澝ܷҴЉӬୣ׃ զଞࣩзӢߓۨٽޥզଞࣩзӢߓۨٽޥ ૝ͧЋЈ୰଀Ӱܯݤݸୡٮ૝ͧЋЈ୰଀ӰЉܯݤݸୡٮЉ څ޳וݤتڅ޳וݤت (OO(logn)(logn ݤٮފݤ֧ૠЖ؊ؒЗͧӘЦػٮފOO(log(log**n)n) ֧ૠЖ؊ؒЗͧӘЦػ গ߄ׁࣩޯݒҀܨݤتগ߄ׁࣩޯݒҀܨݤت ૜ٷࠕױڰЈ૚ͧѷ୰װ૜ӆٷࠕױڰЈ૚ͧѷ୰װӆ څ޳וݤٮڅ޳וݤٮ (OO(1)(1 ࠊقڅ޳וࠊقڅ޳ו DataStructures&Algorithms(Fall2012),TsinghuaUniversity 15 29 logn sqrt(n) n nlogn n^2 n^3 n^2 60 55 50 45 40 35 30 25 20 15 10 5 0 2000 1500 1000 500 0 څ׌େૻڅ׌େૻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 14 28 logn sqrt(n) n nlogn n^2 n^3 n^2 60 55 50 45 40 35 30 25 20 15 10 5 0 30 25 20 15 10 5 0 څ׌େૻڅ׌େૻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 13 27 ѨࣩгࣩгޣފТু੗ͧࣻ੓ॠࡄٜকڿѨޣފТু੗ͧࣻ੓ॠࡄٜকڿ////ػࠕػࠕ ݿ୎Ӄ֚ग़ࠕୋஙࣩॠࡄࣩॠࡄړ஍יݿ୎Ӄ֚ग़ࠕୋஙՔ֧ړ஍יՔ֧֧؁֧؁ЈЈ Ԯ͵Ԯ͵ ػػࣹӸࣩਗ਼ॠࣹӸࣩਗ਼ॠࠆֹু੗ࠆֹু੗ڿڿ 55 㓢ڿ㓢ьСڿNPNPcompletecompleteࣩࣩ ////ьСފފࣂ͵ؓࣂ͵ 22SubSetSubSetؓ 55 ەࣩПەࣩПڿբڿࣩԃࡄիͺիͺ ////բװޟޥࣩԃࡄװޟޥੴڀ͵ࣻ੓ފੴ૟ڀ͵ࣻ੓ފ55 ૟ ੹ͧࣻ੓ॠࡄॠࡄ୻ੌ૨і୻ੌ૨і22nnૅૅ ////ѶЦѶЦЈ࣎ࣂ㖸Ј࣎ࣂ㖸ފ੹ͧࣻ੓Ьػފ55 Ьػ 55 ؓࣂ͵ؓࣂ͵|2|2ss|=2|=2|S||S| =2=2nn 55 ࣻ੓ॠࡄ͵ࣻ੓ॠࡄ͵૵▁߳ߓ૵▁߳ߓSSࣩࠠ▁׾୳ࣩࠠ▁׾୳ 22SubsetSubset DataStructures&Algorithms(Fall2012),TsinghuaUniversity 12 26 ࡄؾн 2323 ঍ং঍ং 3333 ԡࣷ஑ࢤԙԡࣷ஑ࢤԙ 33חءࡄؾнחء 1111 ޅڈ׹ݯޅڈѻ࠿нѻ࠿н 1313 ѿਞࢤѿਞࢤ 33 ׹ݯ ࠢ 77 ԅӮधؾнԅӮधؾн 5454 ݰࡌੋݰࡌੋ 1515ੋੋؤࠢੋੋؤ ࠐߠ 1818 ԡࣷ஑ԡࣷ஑ 1111 ࢚૖਍࢚૖਍ 44ؤࠐߠؤ ׅਘݯׅਘݯ 66 ԗ૖यєԗ૖यє 33 ԗԩ঱޼ঈԗԩ঱޼ঈ 1414 Ұਘݯ 3232ڰҰਘݯڰ Ԧԩ঱޼ঈԦԩ঱޼ঈ 88 ҏсҏҏсҏ 2121 ؾਁ૖ 1010 ৻׬࣋৻׬࣋ 77 Ԧ૖यєԦ૖यє 33ރؾਁ૖ރ ଛ 1111ਁؤଛਁؤ 44 صݰর١ьصݯԅ 33 ݰর١ьۿݯԅ୚ۿ୚ பਘ੼׈பਘ੼׈ 1212 Ӄԡ૖Ӄԡ૖ 44 44 ٍڰ঱ٍڰ঱ ѽப 88ۿѽபҏҰۿ৑ׇׁ৑ׇׁ 88 нӮߧଊнӮߧଊ 88 ҏҰ ݯ،ଊ 99 ҏԎӄҏԎӄ 77 பଛҹபଛҹ 1010ބݯ،ଊષބષ ٝப 99ۿٝபнۿ࣑ঈੋ࣑ঈੋ 1111 ୚৑৸୚৑৸ 66 н 33 ܔלਞܔלݯԅ 55 ਞۿݯԅӃ١ۿݰ׍ੋցݰ׍ੋց 55 Ӄ١ ԡ 33ۿԡࢤۿࢪєࢪє 55 ѿ঱ଛ૖ѿ঱ଛ૖ 2525 ࢤ ՠؾн 55ږՠؾнੋږੋ 88 יۿय঱יۿय঱ 44 ף׹זף׹ז ՠؾн 1313 দ֛দ֛ 44 Ԭॕ،ঈԬॕ،ঈ 1212ږՠؾнږ 䂥ࢬߡ 88ڈ䂥ࢬߡڈ 33 ރ㓅ҏރѣӮ੽ѣѣӮ੽ѣ 2222 㓅ҏ 269269तͺतͺڰ՞װժՔৗ㔵ފڰ՞װժՔৗ㔵ފ ГѸҗ૲ъޥГѸҗ૲ъਂҺޥҺਂ 55 ...... ފѷފ////ѷ ૲ڡ૲ПъतԮژ૲ڡ૲ПъतԮژ਑਑270270 5050ЖЖٕٕԅԅ11ЖࢤԙҺЖࢤԙҺ538538तत ૲Пу࣏૲Пу࣏ ܯ࣒૲ࠨࣻފ૲Пу࣏ͧুЈܯ࣒૲ࠨࣻފ૲Пу࣏ͧুЈ 㔀ঞ࣒՞㔀ঞ࣒՞ٕٕ੠Ѫ૲Ӟࣩ૲Пъ֜੠Ѫ૲Ӟࣩ૲Пъ֜ 55 澬澬૲Пъӱ૲Пъӱ澭澭 SSࣩ׾୳ࣩ׾୳TTࢁરɪࢁરɪT=mT=mͺͺޥժފޥժފ SSН▁ঐҺН▁ঐҺnnЖࠔݦݤͧɪЖࠔݦݤͧɪS=2mS=2m 55 澬澬ୋஙܵ૩ୋஙܵ૩澭澭 22SubsetSubset DataStructures&Algorithms(Fall2012),TsinghuaUniversity 11 25 ......יڰיњ㖸ઊࣩੌۊૠ६ୋஙੌࠢͧފ仫ॵࣩޟיڰיњ㖸ઊࣩੌۊૠ६ୋஙੌࠢͧފ仫ॵࣩޟ 55 ݿࡉؓՐৗ㑦ԌݸԄޥݿࡉؓՐৗ㑦ԌݸԄ࣎২ͧޥ࣎২ͧ ބॠࡄԯ߃ҾЈބ੎ͧѷ੦ਗ਼ӞOO(n(ncc))ॠࡄԯ߃ҾЈބুދکک੎ͧѷ੦ਗ਼ӞॠࡄބুދککୋஙࣩOO(2(2nn))ॠࡄיڭୋஙࣩיڭ 55 ݝॠࡄӰӰݸݝॠࡄݸݝॠࡄࣩӢࠫ㇣ࣩӢࠫ㇣ޥݝॠࡄޥёފёͧފͧ((55 ёёOO(n(ncc))ӰӰOO(2(2nn Ոڸ੃ੜНЈՔٮૹͧڼ׌େ߃ޯۉՈૠ६ॠࡄࣩਗ਼ॠڸ੃ੜНЈՔٮૹͧڼ׌େ߃ޯۉ55 ૠ६ॠࡄࣩਗ਼ॠ 1.00000011.0000001nn ==((nn10001000)) nn10001000 ==OO(1.0000001(1.0000001nn)=)=OO(2(2nn)) 55 c>1c>1ͧͧ nncc ==OO(2(2nn)) ////НьСͺНьСͺ ݤՇ22ͺͺځݤՇНѽځݤͣexponentialfunctionexponentialfunctionͤͤ ////Нѽܗݤͣܗ 55 OO(2(2nn)) DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 40 (a) (b) (c) (d) (e) (f) (g) 1 2 3 4 5 6 7 3 4 5 6 1 7 2 4 5 6 1 3 7 2 5 6 1 3 7 4 2 6 1 3 5 7 4 2 1 3 6 4 7 5 2 1 3 6 4 7 2 5 ࢐ওࠓͧЋৗচӞࠔग੕ग़ڶ࢐ওࠓͧЋৗচӞࠔग੕ग़年۟ܵդͧॠࡄڶnn年۟ܵդͧॠࡄיক২͵י55 ࠔग㓬ࠔग㓬͵ক২ դͧୋங੐ࠆবӖ২nnkkܦդͧୋங੐ࠆবӖ২ૅ۟ܵрܦԤઁ㓬Ԥઁ㓬͵ক͵কkkૅ۟ܵр ࢐ػѸڶ࢐ػѸЖҪॹڶࣩkkЖҪॹלޣդͧܦࣩૅ۟ܵрלޣդͧܦ55 ЈՉ㓬ЈՉ㓬͵ক͵কkkૅ۟ܵр 年ͺشי୻૨іי࢐Ѫগ޹ͺ২ڶ年ͺੴॠࡄشי୻૨іי࢐Ѫগ޹ͺ২ڶୋங͵ୋங͵ ੴॠࡄ 55 ٽܭફࡆٽܭફࡆ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 39 ࢝ޯ࢝ޯ22͵͵bubble2.cppbubble2.cpp 55 ܦӨрܦswap(A[i{swap(A[i1],1],A[iA[i]);sorted=false;]);sorted=false; }}////Өр} ٽ૱ਂٽ૱ਂ////([([if(A[iif(A[i1]>1]>A[iA[i for(inti=1;i)>ɒɒ1~n+11~n+11/x=ln(n+1)=1/x=ln(n+1)=(logn)(logn) //// h(nh(n)<1+)<1+ɒɒ1~n1~n1/x=1+lnn=O(logn)1/x=1+lnn=O(logn) 55 ઁոঃݤ͵ઁոঃݤ͵h(nh(n)=1+1/2+1/3+...+1/n=)=1+1/2+1/3+...+1/n=(logn)(logn) ӢݤͺފҤԤҪݤՔৗ؁੝੥ૠ६ঃݤէͺ୰ଂׁͧޯݒҀࠊݤ澝ੌڶޥӢݤͺފҤԤҪݤՔৗ؁੝੥ૠ६ঃݤէͺ୰ଂׁͧޯݒҀࠊݤ澝ੌڶޥ 55 22/6=/6=OO(1)(1) 1+1/21+1/222 +...+1/n+...+1/n22 <1+1/2<1+1/222 +...=+...= 55 ݗݠ͵ݗݠ͵ 1/1/2+1/2/3+1/3/4+...+1/(n1/1/2+1/2/3+1/3/4+...+1/(n1)/n=11)/n=1 1/n=1/n=OO(1)(1) ੎͵ 1+2+4+...+21+2+4+...+2nn =2=2n+1n+11=1=OO(2(2n+1n+1)=)=OO(2(2nn)) ////㔀ոЉޮ஍բ୘㔀ոЉޮ஍բ୘ٮޣ͵੎ٮޣ 55 ㍖ঃݤ͵㍖ঃݤ͵ TTaa(n(n)=a)=a00 +a+a11 +a+a22 +...+a+...+ann,a>0,a>0 ݱঃݤ͵T(nT(n)=1)=122 +2+222 +...+n+...+n22 =n(n+1)(2n+1)/6==n(n+1)(2n+1)/6=OO(n(n33))ٴ͵ݱঃݤٴ 55 55 ॠݤঃݤ͵ॠݤঃݤ͵T(nT(n)=1+2+...+n=n(n+1)/2=)=1+2+...+n=n(n+1)/2=OO(n(n22)) ঃݤոঃݤո DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 33 ࢲ࡚ࢲ࡚ ++ய੩ய੩ વ઼ ++૶ܱݱस૶ܱݱसڠ૶͵ڠવ઼૶ڠ૶͵ڠ૶ ૨і͵ঃݤ࠭ո૨і͵ঃݤ࠭ո Ӣ߅ࣩОੌݱࡄڅ޳וӢ߅ࣩОੌݱࡄڅ޳ו 55 Ј࣐гޯׁװٷ૨іͧފЈ࣐гޯઔІػޯׁװٷ૨іͧފӢݖૃզ͵Ӣݖૃզ͵gotogoto////ޯઔІػ ࣐ͤઁۊ১ͣڠ࣐ͤ૶ઁۊ১ͣڠઁ࣐ઁ࣐ &&૶ ࢺ͵for()for()澝澝while()while()澝澝......ڱࢺ͵૨іڱ૨і ਸ਼ࠊݤࣩ㔀ո㔀ո۝ਸ਼ࠊݤࣩтԮ՞ਸ਼іऑ۝тԮ՞ਸ਼іऑ ਸ਼ࠊݤ۝їࣩܗॗиׁޯڅ޳ו޼ऀͧݿ୎څਸ਼ࣩ੔۝ਸ਼ࠊݤёіऑ۝їࣩܗॗиׁޯڅ޳ו޼ऀͧݿ୎څਸ਼ࣩ੔۝55 ёіऑ ࠔग㓬ࠔग㓬 ==ЈՉ㓬ЈՉ㓬 ++Ԥઁ㓬Ԥઁ㓬 55 څ޳וڅ޳ו++ 55 ОੌџԆОੌџԆ ==ࠔग㓬ࠔग㓬 ॠࡄӢ߅ॠࡄӢ߅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 48 n2lolo hihi n 55 ૨іॳफ़࢝૨іॳफ़࢝ while(lo12)+1,n>1 T(0)=T(1)=1T(0)=T(1)=1 ͵څ޳ו͵څ޳ו 55 return(2>n)?n:fib(nreturn(2>n)?n:fib(n1)+fib(n1)+fib(n2);2); ͺۄͺНѽૠСۄ࢝ ////НѽૠСڠ࢝зӢ૶ڠ55 зӢ૶ fib(0)=0fib(0)=0ͧͧfib(1)=1,fib(1)=1,fib(nfib(n)=fib(n)=fib(n1)+fib(n1)+fib(n2)2) 55 FibonacciFibonacciݤӦ͵ݤӦ͵{0,1,1,2,3,5,8,13,...}{0,1,1,2,3,5,8,13,...} fib()fib() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 54 T(n)=(cT(n)=(c11+c+c22))nn cc11 ==OO((nn)) =2=2lognlogn(T(1)+c(T(1)+c11)=n*(c)=n*(c22 +c+c11)) =...=... T(n)+cT(n)+c11 =2*(T(n/2)+c=2*(T(n/2)+c11)=2)=222*(T(n/4)+c*(T(n/4)+c11)) 55 ࠭੕࠭੕ T(n)=2*T(n/2)+cT(n)=2*T(n/2)+c11 T(1)=T(1)=OO((11)) 55 ૶ܱһॸ૶ܱһॸ T(nT(n)=)=22**T(T(n/2n/2)+)+OO((11)) sum(Asum(A,lo,lo),lo,lo) ////OO(1)(1)͵ׁڠ૶͵ׁڠ૶ ׾ୋஙࣩ੕ॽԅ ////OO(1)(1)ذ׾ୋஙࣩ੕ॽԅૡুذૡু ࠭੕sum(A,lo,mi)sum(A,lo,mi)ոոsum(A,mi+1,hi)sum(A,mi+1,hi) //2*T(n/2)//2*T(n/2)ڠ࠭੕୻૶ڠ୻૶ ऀͧН࠭੕sum(Asum(A,lo,hi),lo,hi)څऀͧН࠭੕ё૶ܱࣩ੔څё૶ܱࣩ੔ 55 ڠݤঐ࠭ո͵зӢ૶ڠݤঐ࠭ո͵зӢ૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 53 (0,0) (1,1) (2,2) (3,3) (4,4) (5,5) (6,6) (7,7) (0,1) (2,3) (4,5) (6,7) sum(0,3) sum(4,7) sum(0,7) ==OO(1)(1) (2(2logn+1logn+1 1)1) ==OO(n)(n) == OO(1)(1) (2(200 +2+211 +2+222 +...+2+...+2lognlogn)) વ઼ڠવ઼૶ڠ୻ݿ୎Уոո ////૶ےؗ҅ڠ୻ݿ୎У૶ےؗ҅ڠ૶ققT(nT(n)) ==՞՞ 55 ڠݤঐ࠭ո͵зӢ૶ڠݤঐ࠭ո͵зӢ૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 52 lo mi mi+1 hi sum(lo,mi) sum(mi+1,hi) sum(lo,hi) Нsum(Asum(A,0,n,0,n1)1)ړڤНҴՌړڤ}}////ҴՌ returnreturnsum(Asum(A,lo,mi)+,lo,mi)+sum(Asum(A,mi+1,hi);,mi+1,hi); intmi=(lo+hi)>>1;intmi=(lo+hi)>>1; if(lo==hi)returnif(lo==hi)returnA[loA[lo];]; sumsum(int(int A[],intlo,inthi){A[],intlo,inthi){////ԙ୎ਇ֟ԙ୎ਇ֟A[loA[lo,hi],hi] 55 ڠݤঐ࠭ո͵зӢ૶ڠݤঐ࠭ո͵зӢ૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 51 ՟՟ ՟՟ ࠿࠿ ࠿࠿ ӢӢ ӢӢ ׾ୋங ׾ୋங Լୋங ӰԼୋஙࣩ੕ڰӰԼୋஙࣩ੕࣒׾ୋஙࣩ੕ͧڰ࣒׾ୋஙࣩ੕ͧ Ӣӯ࠭੕׾ୋஙӢӯ࠭੕׾ୋங ڡѼࣼל׾ୋஙͧ੐ࠆڡѼࣼל׾ୋஙͧ੐ࠆٳҾӥӢНਂذٳҾӥӢНਂذ ੐ࠆࣩୋஙͧՔјל੐ࠆࣩୋஙͧՔј࠭੕▁Жל55 澬澬DivideDivideandandconquerconquer澭澭НН࠭੕▁Ж Ӣু࠿УӢু࠿У DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 50 T(n)=T(n)=OO(1)+n=(1)+n=OO(n)(n) =T(0)=T(0) =...=... =T(n=T(n2)2) (n(n2)2) 55 ࠭੕࠭੕ T(n)T(n) n=T(nn=T(n1)1) (n(n1)1) T(0)=T(0)=OO(1)(1) //base//base 55 ૶ܱݱस૶ܱݱस T(nT(n)=)=T(nT(n1)+1)+OO(1)(1) ////recurrencerecurrence sum(Asum(A,0),0) ////OO(1)(1)͵ׁڠ૶͵ׁڠ૶ ӆॽԅІӆॽԅІA[nA[n1]1] ////OO(1)(1) ࠭੕੐ࠆНnn11ࣩୋஙࣩୋஙsum(A,nsum(A,n1)1) //T(n//T(n1)1)ڠ࠭੕੐ࠆН୻૶ڠ୻૶ ऀͧН࠭੕sum(Asum(A,n),n)څऀͧН࠭੕ё૶ܱࣩ੔څё૶ܱࣩ੔ 55 ڠݤঐ࠭ո͵঎㓬૶ڠݤঐ࠭ո͵঎㓬૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 49 recursivecalls main() sum(A,n) sum(A,n1) sum(A,n2) sum(A,2) sum(A,1) T(nT(n)=)= OO(1)*(n+1)=(1)*(n+1)=OO(n)(n) sum(A,0) ؗ҅১ાՐ୻O(1)O(1)ݿ୎ݿ୎ڠؗ҅১ાՐ୻ޯ҅ЗͧԤЖ૶ڠ55 ޯ҅ЗͧԤЖ૶ ਸ਼ݿ୎۝ਸ਼ݿ୎Ҿ㔀ոԮॠࡄ۝Ҿ㔀ոԮॠࡄ ࣩ׾ؗ҅ͤڀت୻ݿ୎ͣઁ࣐੶Վޯાͧਗ਼Ҵےࣩ׾ؗ҅ͤॽਗ਼ڀت୻ݿ୎ͣઁ࣐੶Վޯાͧਗ਼Ҵےॽਗ਼ ؗ҅ڠؗ҅૶ڠ߳ߓࠠЖ߳ߓࠠЖ૶ વ઼ͣrecursiontracerecursiontraceͤӢ߅ͤӢ߅ڠવ઼ͣ૶ڠ55 ૶ }} 0:0:sum(Asum(A,n,n1)+A[n1)+A[n1];1]; (1>n)?(1>n)? returnreturn sumsum(int(int A[],intn){A[],intn){ 55 ڠݤঐ࠭ո͵঎㓬૶ڠݤঐ࠭ո͵঎㓬૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 64 ১؅͵১؅͵IntroductiontoAlgorithms,IntroductiontoAlgorithms,hh15.1,15.1,hh15.3,15.3,hh15.415.4 ੦ਗ਼ׁकͣॕ33࢝ͤͧॕ࢝ͤͧॕ1111््ٽ੦ਗ਼ׁकͣॕࡴЭ͵सٽ55 ࡴЭ͵स ОੌՇӏиьС֛ॹͺͧڅ޳וॠࡄࣩी୎ڠОੌՇӏиьС֛ॹͺ૶ͧڅ޳וॠࡄࣩी୎ڠ55 ૶ ૚ளࣩԼ֛څ޳וવ઼ͧ੕ଚੴ࢝ޯڠ૚ளࣩԼ֛ૹ૚૶څ޳וવ઼ͧ੕ଚੴ࢝ޯڠૹ૚૶ څ޳וࣩ࢝ڠзӢ૶څ޳וࣩ࢝ڠવ઼ࡄͧӢ߅fib()fib()зӢ૶ڠવ઼ࡄͧӢ߅ੰ࣐૶ڠ55 ੰ࣐૶ ࣐੶ՎޯાՔЈঞਗ਼ͺઁڠવ઼Ӣ߅ݿͧНьС૶ڠ࣐੶ՎޯાՔЈঞਗ਼ͺҞ૶ઁڠવ઼Ӣ߅ݿͧНьС૶ڠ55 Ҟ૶ ੿դ੿դ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 19 63 ͺڐѽٚޥڅ޳וͺГम࢝ޯࣩी୎ڐѽٚޥڅ޳ו55 Гम࢝ޯࣩी୎ }} || }} ૞֚ڠ૶܈૞֚સૃͧࠆڠ૶܈|| gotonext;gotonext;////સૃͧࠆ elsereturnn*Fac(nelsereturnn*Fac(n1);1); || f*=nf*=n;; if(1>n)return1;if(1>n)return1; || if(1>n)returnf;if(1>n)returnf; ࣐ઁڠ૶܈ࠆͧڹҴૃզߗڕ࣐ઁڠ૶܈ࠆͧڹҴૃզߗڕ////:|| next:next ׾ୋஙࣩ੕ڢ׾ୋஙࣩ੕੢ڢ|| intf=1;intf=1;////੢ Fac(nFac(n){){ || Fac(nFac(n){){ 55 ړڤݘӉН૨іܯ࣐১ાͧӨՔࣻઁڠ૶ފդ▁ࠖਂޣӠݤࣩړڤݘӉН૨іܯ࣐১ાͧӨՔࣻઁڠ૶ފդ▁ࠖਂޣ55 Ӡݤࣩ ࡤୣڠՂ૶ڠ૶ـࡤୣڠՂ૶ڠ૶ـ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 18 62 䙈ᖈ䙈ᖈ ቴቴ 㓵ᙝ䙈ᖈ㓵ᙝ䙈ᖈ ӂ࠼䙈ᖈӂ࠼䙈ᖈ ཐ࠼᭥䙈ᖈཐ࠼᭥䙈ᖈ ڠ঎㓬૶ڠT(nT(n)=n+1=)=n+1=OO(n)(n)////঎㓬૶ ͵ڰ੕͵ڰ੕ T(0)=1T(0)=1 55 ૶ܱӢ߅૶ܱӢ߅ T(nT(n)=T(n)=T(n1)+11)+1 ࢝ fac(nfac(n)=n*fac(n)=n*fac(n1)1)ڠ࢝૶ڠ55 ૶ ୘ЪӠݤ҅͵୘ЪӠݤfac(nfac(n)=n!)=n!////૨і࢝૨і࢝͵͵OO(n)(n)͵҅ 55 ړݘӉН૨іࠆފࣩԃࡄͧػܯࣻޣړݘӉН૨іࠆފࣩԃࡄͧػܯࣻޣ 55 ڠ੣࠭ݝࢴݿͧੌؿՔৗࡤୣ૶֧ڠ55 ֧੣࠭ݝࢴݿͧੌؿՔৗࡤୣ૶ fib()fib()ױࠢױݿ୎ݝࢴѹݿ୎ݝࢴѹ ////ࠢ ी୎ݝࢴѹी୎ݝࢴѹ иࣂ੕ބиؗࢻͧބиࣂ੕ބиؗࢻͧބ ڠ૶ڠ55 ૶ ͺڠЈ૶ފ૟ͧڠͺ૶ڠЈ૶ފ૟ͧڠ૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 17 61 ٽܭٶڠٽܭٶڠ (T(nT(n)=2*T(n/2)+n)=2*T(n/2)+n OO(nlogn)(nlogn ࢝ڠ࢝զଞ࠭ոУзӢ૶ڠT(nT(n)=2*T(n/2)+1)=2*T(n/2)+1 OO(n)(n) զଞ࠭ոУзӢ૶ T(nT(n)=T(n/2)+n)=T(n/2)+n OO(n)(n) Ӧ਽ࣩзӢߓۨӦ਽ࣩзӢߓۨ T(nT(n)=T(n/2)+1)=T(n/2)+1 OO(logn)(logn) զଞࣩзӢߓۨզଞࣩзӢߓۨ T(nT(n)=2*T(n)=2*T(n1)+n1)+n OO(2(2nn)) T(nT(n)=2*T(n)=2*T(n1)+11)+1 OO(2(2nn)) ࠯੽ׇ澝࠯੽ׇ澝FibonacciFibonacciݤݤ ࢝ڠУ঎㓬૶ٽܭ࢝Ӧ਽ફࡆڠУ঎㓬૶ٽܭT(nT(n)=T(n)=T(n1)+n1)+n OO(n(n22)) Ӧ਽ફࡆ ࢝ڠ࢝զଞ࠭ոУ঎㓬૶ڠT(nT(n)=T(n)=T(n1)+11)+1 OO(n)(n) զଞ࠭ոУ঎㓬૶ ੕੕ ؗ҅ؗ҅ ړ૶ܱړ૶ܱ Ӏֹࣩ૶ܱݱसӀֹࣩ૶ܱݱस DataStructures&Algorithms(Fall2012),TsinghuaUniversity 16 60 T(64)=16T(64)=16 101033 sec=5hrsec=5hr ࠽1A1Aםފ࠽ԮҍםފԮҍ T(64)=16T(64)=16 101099 sec=500yrsec=500yr शԇ1G=101G=1099Жࣶ׾Жࣶ׾כशԇԮ҄ҁࣩࣔ৚ࠠरৗכ55 Ԯ҄ҁࣩࣔ৚ࠠरৗ 6464Жࣶ׾ͧЖࣶ׾ͧT(64)=2T(64)=26464 1=21=24444(2(21010))66 =16=16 10101818ޥҺਂޥ55 ਂҺ S(nS(n)=2)=2nn,,T(nT(n)=2)=2nn 1=1=OO(2(2nn))͵ڰ੕͵ڰ55 ੕ ӨӨ S(0)=1,S(0)=1,S(nS(n)=2*(T(n)=2*(T(n1)+1)=2*S(n1)+1)=2*S(n1)1) 55 її S(nS(n)=)=T(nT(n)+1)+1 T(nT(n)=2*T(n)=2*T(n1)+11)+1 T(0)=0T(0)=0 55 ੢੢ T(nT(n)=)=OO(1)(1) ##ࣶ׾शԇͧӨࣶ׾शԇͧӨ HanoiHanoiׇ͵૶ܱݱसׇ͵૶ܱݱस DataStructures&Algorithms(Fall2012),TsinghuaUniversity 15 59 XX YY ZZ move(Ymove(Y,Z),Z) hanoi(1,X,Y,Z)hanoi(1,X,Y,Z) hanoi(1,Y,Z,X)hanoi(1,Y,Z,X) move(Xmove(X,Z),Z) hanoi(0,?,?,?)hanoi(0,?,?,?) hanoi(0,?,?,?)hanoi(0,?,?,?) move(Ymove(Y,X),X) hanoi(0,?,?,?)hanoi(0,?,?,?) hanoi(0,?,?,?)hanoi(0,?,?,?) move(Zmove(Z,Y),Y) hanoi(0,?,?,?)hanoi(0,?,?,?) hanoi(0,?,?,?)hanoi(0,?,?,?) move(Xmove(X,Z),Z) hanoi(0,?,?,?)hanoi(0,?,?,?) hanoi(0,?,?,?)hanoi(0,?,?,?) move(Xmove(X,Y),Y) move(Xmove(X,Z),Z) hanoi(2,Y,X,Z)hanoi(2,Y,X,Z) hanoi(1,Z,X,Y)hanoi(1,Z,X,Y) hanoi(3,X,Y,Z)hanoi(3,X,Y,Z) hanoi(2,X,Z,Y)hanoi(2,X,Z,Y) hanoi(1,X,Y,Z)hanoi(1,X,Y,Z) વ઼ڠવ઼ׇ͵૶ڠHanoiHanoiׇ͵૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 14 58 XX YY ZZ XX YY ZZ XX YY ZZ XX YY ZZ ZZکЖࣶ׾शکYY஌ॏࣩ஌ॏࣩnn11Жࣶ׾शذͧذhanoi(nhanoi(n1,1,YY,,XX,,ZZ););////ҘԈҘԈXXͧ ZZکशکդ▁Жࣶ׾ёXXशޣذդ▁Жࣶ׾ёޣذ////;(move(Xmove(X,Z);,Z YYکЖࣶ׾शکXX஌ॏࣩ஌ॏࣩnn11Жࣶ׾शذͧذhanoi(nhanoi(n1,1,XX,,ZZ,,YY););////ҘԈҘԈZZͧ if(n>0)if(n>0) ZZکЖࣶ׾शکXXІࣩІࣩnnЖࣶ׾शذͧذ55 hanoihanoi(n(n,X,Y,Z),X,Y,Z)////ҘԈҘԈYYͧ HanoiHanoiׇׇ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 13 57 XX YY ZZ XX YY ZZ XX YY ZZ XX YY ZZ 55 Ӣু࠿У͵Ӣু࠿У͵hanoi(nhanoi(n),hanoi(n),hanoi(n1),...,hanoi(1),hanoi(0)1),...,hanoi(1),hanoi(0) Ӹnn11Жࣶ׾ёЖࣶ׾ёYYͣҘԈͣҘԈXXͤश২ͤश২ZZ //hanoi(n//hanoi(n1,Y,X,Z)1,Y,X,Z)ذӸذ (श২ZZ ////move(Xmove(X,Z),ZͤܯࣩଊՐࣶ׾ͣࣻלޣश২ІͤܯࣩଊՐࣶ׾ͣࣻלޣXXІذذ (Ӹnn11Жࣶ׾ёЖࣶ׾ёXXͣҘԈͣҘԈZZͤश২ͤश২YY //hanoi(n//hanoi(n1,X,Z,Y)1,X,Z,YذӸذ 55 ਂٜऍਂٜऍHanoi(Hanoi(nn11))ࣩ੕ࡄͧӨՐ୻ࣩ੕ࡄͧӨՐ୻ HanoiHanoiׇׇ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 12 72 ѽͺױѽͺؗଢ଼ݝ߈ױؗଢ଼ݝ߈ 55 ёёOO(2^r)(2^r)ӰӰOO(r(r))ࣩݘૡࣩݘૡ ==OO(r(r)) =1+1+4=1+1+4rr +1+1 T(rT(r)) ==FFloglog22(n+1)(n+1)VV =r=r =n=nࣩࣩзૡӱѸݤзૡӱѸݤ ࢺࠊݤڱࢺࠊݤڱ څ޳וڅ޳ו 55 returnpow;returnpow;//O(1)//O(1) }} p*=p;p*=p;//O(1)//O(1) nn >>=1;>>=1;//O(1)//O(1) pow*=p;pow*=p;//O(1)//O(1) ifif(n(n &1)&1)//O(1)//O(1) while(00n>0ͧਗ਼ॠͧਗ਼ॠaannͣͣaaНتџѽচࣩؓݦݤت ͵55 ୋங͵ୋங Јؿࣼբڅ޳וЈؿࣼբբ▁ୋஙࣩЈբॠࡄͧڅ޳וբ▁ୋஙࣩЈբॠࡄͧ ͵ا੏͵ا55 ੏ ㍖Ӡݤ͵㍖Ӡݤ͵O(2^r)O(2^r) DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 68 L' 00 nnkkR' L00 nnkk R L00 nnkk R }} reverse(Areverse(A,n);,n);////OO(3n/2)(3n/2) reverse(A+kreverse(A+k,n,nk);k);////OO(3(n(3(nk)/2)k)/2) reverse(Areverse(A,k);,k);////OO(3k/2)(3k/2) voidvoidshift2shift2(int*A,intn,intk){(int*A,intn,intk){ ࢺٗशkkѸͧѸͧOO(3n)(3n)ڱݤঐذࢺٗशҘԈҕ঴ॠࡄͧڱݤঐذ////ҘԈҕ঴ॠࡄͧ ࢺशѸ͵ҕ঴࢝ڱࢺशѸ͵ҕ঴࢝ڱ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 67 gg 2k2k L00 nnkk R 00 nnkk }} movmov +=+=shift(A,n,s,k);shift(A,n,s,k); ////OO(GCD(n,(GCD(n, k))=k))= OO(n(n ** kk // LCM(n,LCM(n, k))k)) for(ints=0,for(ints=0,movmov =0;=0; movmov ,n>ࣩ▁Жࣩ▁Ж 55 ٽҵٽҵ//ٽҝٽҝ᧨᧨ืื͵ٽТࣩࠊٹ͵ٽТࣩࠊٹ//// ӦܭٽߏमࠊܘӦ୻ܭٽߏमࠊܘ୻ һିऑ{K{K11,...,K,...,Knn}}ڀتһିऑͧڀتͧ{{џচџচnnЖҪॹЖҪॹ{R{R11,...,R,...,Rnn 55 ٽܭٽܭ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 76 ॻࣩ......ފॻࣩͧুЋފୋஙЇࣙН(nlogn)(nlogn)ͧুЋٽܭͧױୋஙЇࣙН҅ٽܭͧױ҅ 55 ѨޣТু੗ͧॠࡄٜকڿѨ੢ՙࣩޣТু੗ͧॠࡄٜকڿOO੢ՙࣩלػלػ ރЇࣙͧӨ੹څ૖Ӱ୰څ޳וݻॠࡄࣩ▁ރЇࣙͧӨ੹څ૖Ӱ୰څ޳ו55 ▁ݻॠࡄࣩ Їࣙڅளࣩୋங୰ޟރЇࣙ੩څளࣩୋங୰ޟރѹࣩॠࡄ ++੩ޟڅ޳וѹࣩॠࡄ੦ਗ਼ޟڅ޳ו੦ਗ਼ ͵ەГЖݱ஄अ͵ە55 ГЖݱ஄अ ͺڅѽगؓҾ୰ױͺͧڅѽगؓҾ୰ױͺচؓୋஙPPͧڅͺচؓୋஙНьСੌगؓୋஙࣩ୰څНьСੌगؓୋஙࣩ୰ 55 څࣩ୰څवНPPࣩ୰څ޳וवНࣩڅ޳וѹࣩޣѹޣॠࡄЗޥےॠࡄͧӨ֧؁ॠࡄЗਂޥےॠࡄͧӨ֧؁55 ୋஙୋஙPPਂ ͺڅѽࠢ૊Јբୋஙࣩ୰ױͺڅͺ֧Ք੕ࣩӸܶЇͧՔժં੥ୋஙࣩ୰څѽࠢ૊Јբୋஙࣩ୰ױͺڅ55 ֧Ք੕ࣩӸܶЇͧՔժં੥ୋஙࣩ୰ Քৗࣼٚ㕱ࠛڅ޳וՔৗࣼٚ㕱࣒ࠛІՔ੎ͧբ▁ୋஙࣩЈբॠࡄͧڅ޳ו55 ࣒ІՔ੎ͧբ▁ୋஙࣩЈբॠࡄͧ ЉЇࣙڅЉЇࣙ୰څ୰ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 75 ІͧЈऍёଊ޻ષԽͺȑ з௉ً澝࡯஝ୂۯъњ㺨سҘୋଊଛъଂ͵Ȑ 㾲؍ЖЅ㇋ષՌ澞ފІͧ֩գևҞࣆ௉ୂͧԯ ٠ୂ▁ГЖ՝儳஡ͧՀ઩г֙лԜଛͧԯ޼Ӱ ЉЇࣙٽܭЉЇࣙٽܭ((x2)(x2 1.1.ঠ੥ঠ੥ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 74 swap 00 nnrr <<<< NN ӦͺܭӦͺमܭn!n!मޥےۉࣩगՔјॗࠃࢴ࣏֩ޥےۉࣩगՔјॗࠃࢴ࣏֩ 55 ܦ՞ҪॹЉ୩޲૲Շࣩߏ▁Ӹ஫ͣլ১ાͤрذज़ࣛ͵১դզӸͧ҈ࠊܦ՞ҪॹЉ୩޲૲Շࣩߏ▁Ӹ஫ͣլ১ાͤрذ55 ज़ࣛ͵১դզӸͧ҈ࠊ {while(1::voidVector::copyFromcopyFrom(T(T*constA,Ranklo,Rankhi){*constA,Ranklo,Rankhi){ ٜଜૉટҚݒҀ॔'=''='ۋٜଜૉટҚݒҀ॔Нׁޯ६ֹͧۋ55 templateT> ////TTНׁޯ६ֹͧ ӱࣩ߄ૼוӱࣩ߄ૼׁиוׁи DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 93 55 ~Vector(){delete[]_elem;}~Vector(){delete[]_elem;}////ଚݚӃ଑ी୎ଚݚӃ଑ी୎ ӱוӱզଞݦѼו{{copyFromcopyFrom(V._elem(V._elem,0,,0,V._sizeV._size);});} ////զଞݦѼ Vector(VectorVector(Vectorconst&V)const&V) ӱוӱզଞԙ୎ו{{copyFromcopyFrom(V._elem(V._elem,lo,hi);},lo,hi);} ////զଞԙ୎ 55 Vector(VectorVector(Vectorconst&V,Ranklo,Rankhi)const&V,Ranklo,Rankhi) ӱוݦѼۋӱݤঐԙ୎וݦѼۋ////ݤঐԙ୎ Vector(TVector(T*A,Rankn){*A,Rankn){copyFromcopyFrom(A(A,0,n);},0,n);} 55 Vector(TVector(T*A,Ranklo,Rankhi){*A,Ranklo,Rankhi){copyFromcopyFrom(A(A,lo,hi);},lo,hi);} {_elem=new{_elem=newT[_capacityT[_capacity =c];_size=0;}=c];_size=0;}////ூੜூੜ 55 Vector(intVector(int c=DEFAULT_CAPACITY)c=DEFAULT_CAPACITY) ߄ૼЉ߅߄߄ૼЉ߅߄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 92 applications vector ~vector insert remove traverse... interface _size _elem_elem _capacity_capacity Vector }};; Ռ */*/ܯՌଁԶܯ/*.../*...ଁԶ Ռ */*/ܯՌՔӉܯ/*.../*...ՔӉ Ռ */*/ܯՌՐ੾ܯ/*.../*...Ր੾ /*.../*...߅߄Ӡݤ߅߄Ӡݤ */*/ /*.../*...߄ૼӠݤ߄ૼӠݤ */*/ public:public: /*.../*...Ӄ଑ӠݤӃ଑Ӡݤ **// protected:protected: ԙܨଞ澝ݤ؟ԙ੐ࠆ澝ܨଞ澝ݤ؟private:Rank_size;int_capacity;T*_elem;private:Rank_size;int_capacity;T*_elem;////੐ࠆ澝 templateT> classclassVectorVector {{////զଞࠆ߂६զଞࠆ߂६ ͤלޟ࣐ЗՔ੦঴Нڀଞͣؗଢ଼؟ூੜӫ׶ͤלޟ࣐ЗՔ੦঴Нڀଞͣؗଢ଼؟#defineDEFAULT_CAPACITY3#defineDEFAULT_CAPACITY3////ூੜӫ׶ typedefintRank;typedefintRank;////䰹䰹 VectorVectorࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 91 ADTADTݒҀؗ҅ݒҀؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 90 զଞզଞ ؓܗઊتࣂݱࡄ࣒ӠݤדҪॹͧޥےࣂד▁ঞٶԶզଞଁؓܗઊتࣂݱࡄ࣒ӠݤדҪॹͧޥےࣂד▁ঞٶtraverse()traverse() ଁԶզଞ զଞٽޥզଞٽޥ ҪॹוҪॹ⣞ୣଜוuniquify()uniquify() ⣞ୣଜ Ҫॹ զଞզଞוҪॹ⣞ୣଜוdeduplicate()deduplicate() ⣞ୣଜ զଞٽޥզଞٽޥ ࣩҪॹלޣࣩҪॹЋ䰹לޣиeeЋ䰹לиͧ૞֚Јלsearch(esearch(e)) ߓࣹۨߗҪॹߓࣹۨߗҪॹeeͧ૞֚Ј find(efind(e)) ߓࣹۨߗҪॹߓࣹۨߗҪॹee զଞզଞ Ӧ զଞզଞܭٽୠஂܘӦઁݦ՞ҪॹࣩѸ঴ͧ҄Уܭٽୠஂܘsort()sort() ઁݦ՞ҪॹࣩѸ঴ͧ҄У Ӧ զଞզଞܭٽୠஂܘժٜފҪॹޥےӦӭݮܭٽୠஂܘժٜފҪॹޥےdisordered()disordered() ӭݮ ઊ զଞզଞتݚࣩ؁ઊࣩҪॹͧ૞֚ੴҪॹЗԼتݚࣩ؁remove(rremove(r)) Ӭୣ䰹НӬୣ䰹НrrࣩҪॹͧ૞֚ੴҪॹЗԼ insert(rinsert(r,e),e) eeҀН䰹НҀН䰹НrrҪॹܷҴͧԼդটҪॹ҈ࠊդशҪॹܷҴͧԼդটҪॹ҈ࠊդश զଞզଞ 䰹НrrҪॹࣩݤҚҪॹࣩݤҚ զଞզଞܦޢ䰹Нܦޢput(rput(r,e),e) ࣐࣐ee get(rget(r)) ਑Շ䰹Н਑Շ䰹НrrࣩҪॹࣩҪॹ զଞզଞ Ӹࣩ੐ࠆͣҪॹ㔀ݤͤ զଞզଞڡӸࣩ੐ࠆͣҪॹ㔀ݤͤ۶ղզଞڡsize()size() ۶ղզଞ ઊتઊ૯࣐تݒҀݒҀ ԄৗԄৗ ૯࣐ ՌܯՌܯզଞզଞADTADT DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 89 AA 0 n1 2 n1...... গ߄ࣩؓӱЉؗࢻܨ޳ݤוԿЉ֩ܩНҍޟগ߄ࣩؓӱЉؗࢻՔܨ޳ݤוԿЉ֩ܩНҍޟՔ ԅफ़Ԗ澝ঞ▁Љ،ҵޟԅफ़Ԗ澝ঞ▁Љ،ҵݒҀ澝ॡࣂঢ۵ޟݒҀ澝ॡࣂঢ۵ Ҫॹࣩ६ֹЈୡиׁޯ६ֹͧ࣎২ՔјкЈࣼբҪॹࣩ६ֹЈୡиׁޯ६ֹͧ࣎২ՔјкЈࣼբ ڀت▁▁ͤڀت▁▁՞ҪॹЉ՞ҪॹЉ[0,n)[0,n)Ӄࣩ䰹ͣӃࣩ䰹ͣrankrankͤ ۉুੇخٽ঎㓬ࠊܘઊЉࡅԖ࣒ͧ▁ঐҪॹۻݤঐࣩފզଞۉুੇخٽ঎㓬ࠊܘઊЉࡅԖ࣒ͧ▁ঐҪॹۻݤঐࣩފ55 զଞ ݜтवҀ঎㓬ݤঐͣݜтवҀ঎㓬ݤঐͣlineararraylineararrayͤͤ A[iA[i]]ࣩࢢࣂࣩ֩֬ࢢࣂ֩֬ =A+i=A+is,ss,sНԤЖҪॹԨ࣐ࣩी୎ଞНԤЖҪॹԨ࣐ࣩी୎ଞ ੧ୋ੧ୋܯࣻܯіͧՔࣻܗ▁іͧՔՅУͧࠠЖҪॹ࣒঩ՙ։ܗ▁55 ՅУͧࠠЖҪॹ࣒঩ՙ։ A[0],A[1],A[2],...,A[nA[0],A[1],A[2],...,A[n1]1] ڀت▁▁Ӄࣩ঩ՙڀت▁▁55 C/C++C/C++੶੗Зͧݤঐ੶੗ЗͧݤঐA[]A[]ЗࣩҪॹЉЗࣩҪॹЉ[0,n)[0,n)Ӄࣩ঩ՙ ёݤঐӰզଞёݤঐӰզଞ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 104 e rightshift (d) (c) (b) expandedifnecessary }} (a) maybefull returnr;returnr;////૞֚䰹૞֚䰹 ଞ؟ݰޟଞ঴ҴݰҪॹͧ؟ݰޟ__elem[relem[r]=e;_size++;]=e;_size++;////঴ҴݰҪॹͧ __elem[ielem[i]=_elem[i]=_elem[i1];1];////դটҪॹஎࠊդश▁ЖԤҪդটҪॹஎࠊդश▁ЖԤҪ for(inti=_size;i>r;ifor(inti=_size;i>r;i))////১դզӸ১դզӸ ؟۞ͧੌڶޥਂ؟۞ͧੌڶޥਂ////;()expand();expand RankVector::RankVector::insertinsert(Rank(Rank r,Tconstr,Tconst&& e){e){//O(//O(nnrr)) 55 templateT>//e//eҀН䰹НҀН䰹НrrҪॹܷҴͧҪॹܷҴͧ0<=r<=size0<=r<=size ܷҴܷҴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 103 ړНДߡࣩݱޟଙ࣐ڀ࣐ؗЗړНДߡࣩݱޟଙ࣐ڀ࣐ؗЗ טڿࣂҴՌԿݤભࣙॗדބ޲ӱͧफ़טڿࣂҴՌԿݤભࣙॗדބЋӮ࣐assertionassertion޲ӱͧफ़ޗЋӮ࣐Нҍи੣੕ͧޗ55 Нҍи੣੕ͧ ٗҚ͵ٗҚ͵V[rV[r]=(T)(2*x+3);]=(T)(2*x+3); ՗Қ͵՗Қ͵Tx=Tx=V[rV[r]+]+U[sU[s]*]*W[tW[t];]; иӃ଑ࣩ V._elem[rV._elem[r]]ڀتиӃ଑ࣩԮڀتࣩ V[rV[r]]Ԯטتࣩࠕդͧטت55 ࠕդͧ {return_{return_elem[relem[r];}];} TT&& Vector::Vector::operator[]operator[](Rank(Rank r)constr)const//0<=r<_size//0<=r<_size 55 templateT> ੧ୋզଞҪॹړ੧ୋզଞҪॹͧҍՔࡀ࣐ݤঐࣩЇߗݱړ55 ૹ૚ଜૉЇߗݒҀ॔ૹ૚ଜૉЇߗݒҀ॔““[][]””ͧҍՔࡀ࣐ݤঐࣩЇߗݱ A[rA[r]]ړ੧ୋݱړݤঐҪॹࣩЇߗױ㓬ૢЈܩѷҍړ੧ୋݱړݤঐҪॹࣩЇߗױ㓬ૢЈܩѷҍ Ռͧ֠࢐Քј੾澝ӉզଞҪॹܯՌͧ֠࢐Քј੾澝ӉզଞҪॹܯ(55 ૹ૚ૹ૚V.get(rV.get(r))ոոV.put(rV.put(r,e),e Ҫॹ੧ୋҪॹ੧ୋ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 102 զଞٽզଞݸٽ(c)(c)ݸ 2.2.զଞզଞ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 101 ޳ࣩ҅׾וޟ澝יޟऀӰذ޳ࣩ҅׾դ஄וޟ澝יޟऀӰذ55 դ஄ গ߄ոॠࡄࣩँؗ㓬ৗܨНॳӔ֩੪ӭݤޟগ߄ոॠࡄࣩँؗ㓬ৗՔјܨНॳӔ֩੪ӭݤޟՔј Ӧٽԅ㒨ؗ֩ӵࣗгՔৗӞࢻࣩݒҀޟӦٽԅ㒨ؗ֩ӵࣗгՔৗӞࢻࣩݒҀޟ ॸӦݒҀҞݦѼࣩিଞ▁تͧڅॸӦݒҀҞݦѼࣩিଞёؗଢ଼Քਸ਼ࣩ੔▁تͧڅёؗଢ଼Քਸ਼ࣩ੔ Ӣ㢉২ԤࠊݒҀޯۉ୻㔀ѼےӢ㢉২ԤࠊݒҀࠊݒҀͧޯۉ୻㔀ѼےࠊݒҀͧיכરיכগ߄૤ড૤ড֩ؗݲ֩ؗݲરܨݤتগ߄ܨݤت amortizedcomplexityamortizedcomplexityͤͤͣڅ޳וӢ㢉ͣڅ޳ו55 Ӣ㢉 গ߄ոॠࡄࣩँؗ㓬ৗܨЈৗӔग֩੪ӭݤککগ߄ոॠࡄࣩँؗ㓬ৗܨЈৗӔग֩੪ӭݤکک Ԁ੆гݒҀУ୎ࣩࣼһ㓬ո૤ગ㓬Ԁ੆гݒҀУ୎ࣩࣼһ㓬ո૤ગ㓬 ՞मՔৗࣩݒҀͧҀН՞मՔৗࣩݒҀͧҀНࢮैࢮैжѝӢӯিߓжѝӢӯিߓ ֭ٴԅ޴ޯۉࣩڀتذগ߄՞मݒҀӞࢻࠃࢴࣩӢ١ͧܨݤܨߠ֭ٴԅ޴ޯۉࣩڀتذগ߄՞मݒҀӞࢻࠃࢴࣩӢ١ͧܨݤܨߠ average/expectedcomplexityaverage/expectedcomplexityͤͤͣڅ޳וީޫۋڅ޳ו֭ٴͣڅ޳וީޫۋڅ޳ו֭ٴ 55 Ӣ߅ vs.vs.Ӣ㢉Ӣ߅Ӣ㢉Ӣ߅֭ٴӢ߅֭ٴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 100 2^m=O(n)2^(m2^(m1)1) ......... 844 422 211 100 НO(1)O(1)ޯۉࣩӢ㢉؟Нͧࠠࠊ۞ޯۉࣩӢ㢉؟㔀Ѽৃݿ㔀Ѽৃݿ =O(n)=O(n)ͧࠠࠊ۞ 1,2,4,8,...,2^m=n1,2,4,8,...,2^m=n ////ӘѽঃݤӘѽঃݤ ࠊН҈ޯۉӱԼզଞࣩݿ୎ו૚सЗ؟ࠊН՞ࠊ۞҈ޯۉӱԼզଞࣩݿ୎ו૚सЗ؟՞ࠊ۞ 55 ؟ࠊܷҴݿ଒୻۞؟ॕ11澝澝22澝澝44澝澝88澝澝1616澝澝......ࠊܷҴݿ଒୻۞֧ͧފॕи֧ͧފ55 и ଞ11ࣩࣩࢁࢁզଞЗͧ૤ডܷҴզଞЗͧ૤ডܷҴn=2^mn=2^m฾฾ 22ЖҪॹЖҪॹ......؟㖊Ӑ͵֧ӫ׶֯ޣଞ؟㖊Ӑ͵֧ӫ׶֯ޣ 55 ଞԅҔԅҔ؟ଞ؟////;[;[T*T*oldElemoldElem =_elem;_elem=new=_elem;_elem=newT[T[_capacity_capacity <<=1<<=1 ଞԅҔ؟ІࡽУӸͧ҄ذଞԅҔ֧Ԯ؟ІࡽУӸͧ҄ذ55 ֧Ԯ ଞԅҔज़ࣛ؟ଞԅҔज़ࣛ؟ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 99 m*I=O(n)(m(m1)I+11)I+1 ......... ......... 3*I2I+12I+1 2*II+1I+1 INCREMENT11 НO(n)O(n)ޯۉࣩӢ㢉؟Нͧࠠࠊ۞ޯۉࣩӢ㢉؟㔀Ѽৃݿ㔀Ѽৃݿ =I*(m=I*(m1)*m/2=O(n1)*m/2=O(n22))ͧࠠࠊ۞ ঃݤޱঃݤॠޱ0,I,2I,...,(m0,I,2I,...,(m1)I1)I ////ॠ ࠊН҈ޯۉӱԼզଞࣩݿ୎ו૚सЗ؟ࠊНԮҍЈਗ਼࣓੻ी୎ݒҀͧ՞ࠊ۞҈ޯۉӱԼզଞࣩݿ୎ו૚सЗ؟ԮҍЈਗ਼࣓੻ी୎ݒҀͧ՞ࠊ۞ 55 ؟ࠊܷҴݿ଒୻۞؟ॕ11澝澝I+1I+1澝澝2I+12I+1澝澝3I+13I+1澝澝......ࠊܷҴݿ଒୻۞֧ͧފॕи֧ͧފ55 и ଞ00ࣩࣩीीզଞЗͧ૤ডܷҴզଞЗͧ૤ডܷҴn=m*In=m*I฾฾ 22ЖҪॹЖҪॹ......؟㖊Ӑ͵֧ӫ׶֯ޣଞ؟㖊Ӑ͵֧ӫ׶֯ޣ 55 T*T*oldElemoldElem =_elem;_elem=new=_elem;_elem=newT[T[_capacity_capacity ++==INCREMENTINCREMENT];]; ଞ؟ࣩسלІࡽУӸͧ૬ԅ֠ؓذଞ֧Ԯ؟ࣩسלІࡽУӸͧ૬ԅ֠ؓذ55 ֧Ԯ ଞ૶׌ज़ࣛ؟ଞ૶׌ज़ࣛ؟ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 98 ժՔਸ਼ͺފժՔਸ਼ͺҾ؈ज़ࣛފҾ؈ज़ࣛ ଞԅҔ””ࣩज़ࣛ⩲ͺࣩज़ࣛ⩲ͺ؟ଞԅҔ؟““ஏଙ࣐ڶஏଙ࣐Нѽڶ55 Нѽ ଢܗУդࢢࣂ֩֬ݘՉͧԯЈ৩Ӟࢻଝ؟ؿॡ۞ͧੇخ࣮иզଞࣩڰଢܗУդࢢࣂ֩֬ݘՉͧԯЈ৩Ӟࢻଝ؟ؿॡ۞ͧੇخ࣮иզଞࣩڰ 55 }} delete[]delete[]oldElemoldElem;;////ଚݚԼी୎ଚݚԼी୎ ٜଜૉટҚݒҀ॔'=''='ۋٜଜૉટҚݒҀ॔Нׁޯ६ֹͧۋelem[ielem[i]=]=oldElem[ioldElem[i];];////TTНׁޯ६ֹͧ__ ؟ӱԼզଞӃו؟ӱԼզଞӃו//// (++for(inti=0;i<_size;i++)for(inti=0;i<_size;i ଞԅҔԅҔ؟ଞ؟////;[;[T*T*oldElemoldElem =_elem;_elem=new=_elem;_elem=newT[T[_capacity_capacity <<=1<<=1 ଞ؟سޣଞЈѹи؟سޣcapacity_capacity==max(_capacitymax(_capacity,, DEFAULT_CAPACITY)DEFAULT_CAPACITY);;////Јѹи_ ؟۞ڶࢁճݿͧЈޭظ؟۞ڶࢁճݿͧЈޭظ////;if(_size<_capacity)return;if(_size<_capacity)return ؟զଞी୎Јરݿ۞؟voidVector::voidVector::expandexpand(){(){////զଞी୎Јરݿ۞ 55 templateT> ॠࡄؗࢻ؟ॠࡄؗࢻ۞؟۞ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 97 e B[] A[] (c) allocated full B[] free A[] copied (e) released B[] free A[] A[] A[] copied (d) full full(b) (a) ଞ؟Ӄ଑ݤঐࣩלଞ۞؟Ӄ଑ݤঐࣩל۞֩ڡ૯֩ڡ૯ Ն࣏ІࡽІࡽݿݿذՆ࣏֧Ԯذ55 ֧Ԯ іУј......ͧגטіУјԮ塩ԽԼҮࣩͧגטԮ塩ԽԼҮࣩ ঈ؟גטঈાѼࠠক૚▁ࠝݿ୎ࣩ࣏େͧј৩ݸࡄН؟גטાѼࠠক૚▁ࠝݿ୎ࣩ࣏େͧј৩ݸࡄН 55 墜ࣩ⫱؅͵墜ࣩ⫱؅͵ ԇ㓆ी୎ॡࣂԇ㓆ी୎ॡࣂ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 112 Հ੃оۙࢱࣩ۴г▁ԠԽЬ澞ȑ ͧٵњૠ▁Ԡͧ㟌ਃࣩѺً֧ЗͧૠГ ۊՀԽг▁Ԡ澞דࡕͧӯ؁⠽帿ͧ୰ј շ澞Ղ২䍡䍣䏕ࡤͧӞ޼ݿͧՀ࠻৿߈ г㓬ڰଛͧବ֧䃃Ӄͧਦиଭ߂ߪЇͧ њ弸֧мۊԠ澞ל啚ਘ䎯І䍡ͧ䏝㬑г স䘄ଂ͵Ȑ১ё䔟䔟Խդͧૠً੃зଏ զଞٽޥզଞٽޥ((d)(d 2.2.զଞզଞ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 111 }} V.V.traversetraverse((IncreaseIncrease());());////ԮՔјУНׁޯݒҀଁԶզଞԮՔјУНׁޯݒҀଁԶզଞ templateT> voidvoidincreaseincrease(Vector(Vector&& V){V){ 55 ࠕդͧࠕդͧ...... };}; virtualvoidvirtualvoidoperator()(Toperator()(T&& e){e++;}e){e++;}////ԅ▁ԅ▁ ઊ͵ૹ૚ଜૉݒҀ॔““()()””ؗࢻؗࢻتઊ͵ૹ૚ଜૉݒҀ॔ӠݤتstructstructIncreaseIncrease {{////Ӡݤ ٜଜૉݒҀ॔““++++””ۋ૶׌ܯٜଜૉݒҀ॔Քࣻۋ૶׌ܯtemplateT>////Ҝ੦Ҝ੦TTՔࣻ 55 நҮͧؗࢻ▁ЖՔ҄ԤЖநҮͧؗࢻ▁ЖՔ҄ԤЖTT६ֹҪॹԅ▁ࣩ६६ֹҪॹԅ▁ࣩ६ ҪॹӢӯԅ▁ͧՐ୻ ......ޥےզଞЗذ▁НঞͧױҪॹӢӯԅ▁ͧՐ୻ࠢޥےզଞЗذ▁Нঞͧױ55 ࠢ ଁԶ͵ؗ҅ଁԶ͵ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 110 {for(inti=0;i<_size;i++){for(inti=0;i<_size;i++)visitvisit(_elem[i(_elem[i]);}]);} ઊتઊӠݤتvoidVector::voidVector::traversetraverse(VST(VST&&visitvisit))////Ӡݤ templateT> templateVST> 㓬Ғݘفઊ޲ӱͧՔҵت㓬Ғݘفઊ޲ӱͧՔҵت55 Ӯ࣐ӠݤӮ࣐Ӡݤ {for(inti=0;i<_size;i++){for(inti=0;i<_size;i++)visitvisit(_elem[i(_elem[i]);}]);} ଢܗଢӠݤܗvoidVector::voidVector::traversetraverse(void(void (*(*visitvisit)(T)(T&))&)) ////Ӡݤ templateT> ଑㓬Ғݘفۋଢ޲ӱͧՐ੾ܗ଑㓬ҒݘӮ࣐Ӡݤفۋଢ޲ӱͧՐ੾ܗ55 Ӯ࣐Ӡݤ Ҿѭ૶ӰզଞӃ଑ͺذѽױҾѭ૶ӰզଞӃ଑ͺݒҀͺذѽױvisitvisitݒҀͺؓܗѽױؓܗѽױ ՞ҪॹӢӯؗݲvisitvisitݒҀݒҀت▁՞ҪॹӢӯؗݲঞت▁55 ଁԶզଞଁԶզଞͧͧঞ ଁԶଁԶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 109 i leftshift (c) [0,i) Uf i+1 (b) [0,i+1) Uf (a) i [0,i) Ue 55 ࠠૅ૨іЗࠠૅ૨іЗfind()find()ոոremove()remove()ॽਗ਼ৃચ঎㓬ݿ୎ͧݜ㔀ѼНॽਗ਼ৃચ঎㓬ݿ୎ͧݜ㔀ѼНO(nO(n22)) ૨іO(n)O(n)ૅૅי࢐ওࠓͧЋ২ڶ૨іݜॠࡄי࢐ওࠓͧЋ২ڶݜॠࡄ ܸ۬ބޟܸ۬ބޟ(ԤઁЇୠͧЋ૥ݾӖ২00//2)//2څԤઁЇୠͧЋ૥ݾӖ২ࣩେڅӸҪॹդথդথࣩେڡӸҪॹͤڡ22ͤ ڀتڀت(ԤઁஂୠͧЋ૥ݾ׌২_size_size//1)//1)ոո2)2څԤઁஂୠͧЋ૥ݾ׌২ࣩେڅӸҪॹӸথӸথࣩେڡӸҪॹͤڡ11ͤ ࣩwhilewhile૨і૨іוࣩ୩अՅו୩अՅ 55 ैۉݿ১࢐ैۉӫ׶ӫ׶i=1i=1ݿ১࢐ ڐЗࣩҪॹ֭кڐӸҪॹV[iV[i]]ӸথӸথV[0,i)V[0,i)ЗࣩҪॹ֭кڡ͵ӸҪॹЈՉ㓬ڡ͵ЈՉ㓬 55 څ޳ו։▁Ԗ͵ࠔग㓬Ղڅ޳ו։▁Ԗ͵ࠔग㓬Ղ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 108 }} returnreturnoldSizeoldSize _size;_size;////զଞ੐ࠆՉԖଞզଞ੐ࠆՉԖଞͧͧԮӬୣҪॹ㔀ݤԮӬୣҪॹ㔀ݤ i++:i++:remove(remove(ii);); ////ਂݸ୹բӨটডিߓҾդটͧժӨӬୣ୹բীਂݸ୹բӨটডিߓҾդটͧժӨӬୣ୹բী ((find(_elem[ifind(_elem[i],0,i)],0,i) <0)?<0)? Жͤ▁יЉУ୹բীͣ২ۨثЖͤӸথЗ▁יЉУ୹բীͣ২ۨث////֧Ҿ֧ҾӸথЗ while(i<_size)while(i<_size)////১Ӹզդ૵▁িߓ՞Ҫॹ১Ӹզդ૵▁িߓ՞Ҫॹ_elem[i_elem[i]] ׶ڏ׶ڏ[Ranki=1;Ranki=1;////ёё_elem[1]_elem[1 Լ੐ࠆڢԼ੐ࠆ੢ڢintintoldSizeoldSize =_size;=_size;////੢ intVector::intVector::deduplicatededuplicate(){(){ ////ॾ䛑࢝ॾ䛑࢝ ++ହ੷࢝ହ੷࢝ Ҫॹͧͧ૞֚੃ӬୣҪॹݤࣹ૞֚੃ӬୣҪॹݤࣹוҪॹӬୣଜו55 templateT>////Ӭୣଜ ։▁Ԗ͵ॠࡄ։▁Ԗ͵ॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 107 scan lolo hihicompared&failedee }} ઑͶժӨhihiԮշЗҪॹࣩ䰹ԮշЗҪॹࣩ䰹סյअڿઑͶժӨסյअڿreturnhi;returnhi;//hi::RankVector::findfind(T(T constconst&& e,Ranklo,Rankhi)conste,Ranklo,Rankhi)const 55 templateT>//0<=lo"">"ٜଜૉݒҀ॔ۋٜଜૉݒҀׁࣩ॔ޯ६ֹͧۋզଞ͵ҜؓTTНՔНՔࠢ૊ࠢ૊ׁࣩޯ६ֹͧٽޥզଞ͵Ҝؓٽޥ "=!""=!"ۋۋ"=""="ٜଜૉݒҀ॔ۋٜଜૉݒҀׁࣩ॔ޯ६ֹͧۋզଞ͵ҜؓTTНՔНՔӭॗӭॗׁࣩޯ६ֹͧٽզଞ͵Ҝؓݸٽݸ 55 ӭॗ֗Љࠢ૊֗ӭॗ֗Љࠢ૊֗ ߓۨߓۨ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 106 څ޳וࣩڅ޳ו৩㔀ѼO(nO(n22))ࣩجذࠕͧױ৩㔀Ѽجذࠕͧױ (hi=hi lo=O(n)lo=O(n= څࢺࠊݤॗиԙ୎ؠڱڅࢺࠊݤॗиԙ୎ؠڱ (n=n hi=O(n)hi=O(n= څࢺৃݿࠔࠢиӬୣԙ୎ࣩդথେڱࠠࠊڅࢺৃݿࠔࠢиӬୣԙ୎ࣩդথେڱࠠࠊ ѽͺױՌؗࢻԙ୎Ӭୣͧݝ߈ܯ߈Յ૚޼ׁͧиԤҪॹӬୣױѽͺױՌؗࢻԙ୎Ӭୣͧݝ߈ܯ߈Յ૚޼ׁͧиԤҪॹӬୣױ 55 }} returne;returne;////૞֚੃ӬୣҪॹ૞֚੃ӬୣҪॹ remove(rremove(r,r+1);,r+1);////ઁ࣐ԙ୎Ӭୣॠࡄઁ࣐ԙ୎Ӭୣॠࡄ Ѡ੃ӬୣҪॹהѠ੃ӬୣҪॹה////;[;[Te=_Te=_elem[relem[r TVector::TVector::removeremove(Rank(Rank r){r){//O(//O(nnrr)) 55 templateT>////ӬୣզଞЗ䰹НӬୣզଞЗ䰹НrrࣩҪॹࣩͧҪॹͧ0<=r::intVector::removeremove(Rank(Rank lo,Rankhi){lo,Rankhi){//O(//O(nnhihi)) 55 templateT>////Ӭୣԙ୎Ӭୣԙ୎[[lo,hilo,hi))ͧͧ0<=lo<=hi<=size0<=lo<=hi<=size ԙ୎Ӭୣԙ୎Ӭୣ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 120 0 hi x 1xcomparison comparisonx2 n mi+1 x<= lo hi (b) <=x lo (a) x<=<=x }} mi ઑסઑߓۨסreturnreturn1;1;////ߓۨ }} շЗדշЗדelsereturnmi;elsereturnmi;////֧֧mimi elseelseifif ((A[miA[mi]>1;Rankmi=(lo+hi)>>1;////јЗ䎯Нે䎯јЗ䎯Нે䎯 while(loT> зӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯAAͤ͵ؗࢻͤ͵ؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 119 0 hi x 1xcomparison comparisonx2 n mi+1 x<= lo hi (b) <=x lo (a) x<=<=x mi ୋங੐ࠆবӖذীۋշЗͧכীৗۋГࠊࠢ૊ͧיୋங੐ࠆবӖӨࠠক૚২ذীۋշЗͧכীৗۋГࠊࠢ૊ͧיӨࠠক૚২ ۲Ԡज़ࣛۋ۲Ԡज़ࣛзӢۋՇҀЗ䎯 ////зӢފՇҀЗ䎯㔀ފ55 ਂે䎯ਂે䎯mimi㔀 շЗͧՔ୩Ԯ૞֚דշЗͧՔ୩Ԯ૞֚͵ٜ֧ࠕד33ͤͤ ee== xx͵ٜ֧ࠕ ࡬Ҵڠ࡬ҴͧтՔ૶ڠи՗Ҋ׾ԙ୎S[S[mi+1mi+1,,hihi))ͧтՔ૶يڶ֧؁и՗Ҋ׾ԙ୎ਂيڶ֧؁22ͤͤ xx<< ee͵Ө͵Өeeਂ ࡬Ҵڠ࡬ҴͧݜՔ૶ڠиٗҊ׾ԙ୎S[S[lolo,,mimi))ͧݜՔ૶يڶ֧؁иٗҊ׾ԙ୎ਂيڶ֧؁11ͤͤ ee<< xx͵Ө͵Өeeਂ ࣂ͵דࣂ͵Ҟ▁ࠢ૊ͧԮՔӢЅम㖊Ӑૡ▁ࠖדࣹߗҪॹeeЉЉxxҞ▁ࠢ૊ͧԮՔӢЅम㖊Ӑૡ▁ࠖذࣹߗҪॹՐ୻ذ55 Ր୻ S[loS[lo,mi)<=,mi)<=S[miS[mi]<=S[mi+1,hi)]<=S[mi+1,hi) ////S[miS[mi]]वҀે䎯वҀે䎯 ߓۨԙ୎ӢНЅ଑Ӣڬذߓۨԙ୎ӢНЅ଑ӢНࣙͧ଒Քڬذ55 Ӗু࠿У͵јџ▁ҪॹӖু࠿У͵јџ▁Ҫॹx=x=S[miS[mi]]Нࣙͧ଒Ք зӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯAAͤ͵Լࣂͤ͵Լࣂ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 118 lo hilo1 hi1 Ӹ▁ЖҪॹ””⩲ͺ⩲ͺޣӸ▁ЖҪॹࣩޣиeeࣩسиЈس}}////ংؓ૞֚ংؓ૞֚““Ј ::fibSearch(_elemfibSearch(_elem,e,lo,hi);,e,lo,hi);//Fibonacci//Fibonacciߓۨॠࡄߓۨॠࡄ ীۋীзӢߓۨॠࡄͧۋbinSearch(_elembinSearch(_elem,e,lo,hi),e,lo,hi)////зӢߓۨॠࡄͧ ՞50%50%ࣩࠃࢴ୩޲૲࣐ࣩࠃࢴ୩޲૲࣐ܘ՞ܘ////?(return(rand()%2)?return(rand()%2 {{//0<=lo::RankVector::searchsearch(T(T const&e,Ranklo,Rankhi)constconst&e,Ranklo,Rankhi)const Ռܯ▁Ռߓۨॠࡄঞܯ▁55 templateT>////ߓۨॠࡄঞ զଞࣩঢ۵͵V.insertAfter(V.search(eV.insertAfter(V.search(e),e)),e)ٽޥࠕͧՔҍиױ͵զଞࣩঢ۵ٽޥࠕͧՔҍиױ V[hiV[hi1]::intVector::uniquifyuniquify 55 templateT> ݘૡذڶӬୣ୹բҪॹͧ㓬ৗۦۉԙ୎НԤѸͧוݘૡਂৗјଜذڶӬୣ୹բҪॹͧ㓬ৗۦۉԙ୎НԤѸͧוਂৗјଜ ࠊӸशיࠊӸशѹݝࣩߠࡼ֧иͧբ▁ҪॹՔҀН੃ӬୣҪॹࣩդটי55 Յ㓢͵Յ㓢͵ ѹݝࣩߠࡼ֧иͧբ▁ҪॹՔҀН੃ӬୣҪॹࣩդট ։▁Ԗͣளݝ࢝ͤ͵ॠࡄ։▁Ԗͣளݝ࢝ͤ͵ॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 115 e(e) e e(d) 1 e e e(c) n3duplicatestoberemoved (b) (a) n2 duplicatestoberemoved e e e e e e e... duplicates e e e e e e... զଞࣼբࣼբٽզଞݸٽॽਗ਼ॽਗ਼OO(n(n22)) ———— ؿॡࣾԽؿॡࣾԽfind()find()ͧ㔀ѼौЉͧ㔀ѼौЉݸ 㖊Ӑ͵ ࠠࠊ଒୻ઁ࣐▁ࠊࠠࠊ଒୻ઁ࣐▁ࠊremove()remove()ͧৃݿͧৃݿOO(n(n1)~1)~OO(1)(1)֯ޣ͵㖊Ӑ֯ޣ 55 ࢺͧࠊݤҺਗ਼ _size_size 1=n1=n 11ڱࢺͧࠊݤҺਗ਼ڱ૜ਸ਼ݿ୎ОੌՇӏи૜ਸ਼ݿ୎ОੌՇӏиwhilewhile 55 څ޳ו͵։▁Ԗͣѹݝ࢝ͤڅ޳ו͵։▁Ԗͣѹݝ࢝ͤ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 114 }} returnreturnoldSizeoldSize _size;_size;////զଞ੐ࠆՉԖଞͧԮӬୣҪॹ㔀ݤզଞ੐ࠆՉԖଞͧԮӬୣҪॹ㔀ݤ (_(_elem[ielem[i]==_]==_elem[ielem[i +1])?+1])?remove(iremove(i +1):i++;+1):i++; ////ਂ୹բͧӨӬୣդীͶժӨͧૃ২դ▁Ҫॹਂ୹բͧӨӬୣդীͶժӨͧૃ২դ▁Ҫॹ ࣼ଍Ҫॹت՞تࣼ଍ҪॹёӸզդͧ૵▁ࠢت՞تwhile(i<_sizewhile(i<_size 1)1)////ёӸզդͧ૵▁ࠢ ׶ڏ׶ёநҪॹڏintintoldSizeoldSize =_size;inti=0;=_size;inti=0;////ёநҪॹ ҪॹͧݝࢴѹוҪॹͧݝࢴѹ૵Ж⣞ୣଜוintVector::intVector::uniquifyuniquify(){(){////૵Ж⣞ୣଜ 55 templateT> ֛ࠕͧࠠ▁ԙ୎Ր୻ҐࣚԤЖҪॹԮՔ֛ࠕͧࠠ▁ԙ୎Ր୻ҐࣚԤЖҪॹԮՔ Жԙ୎▁ۉ࢐ࣼкॻ଍߄ڶࣩҪॹוզଞЗͧଜٽޥ֧͵اЖԙ୎੏▁ۉ࢐ࣼкॻ଍߄ڶࣩҪॹוզଞЗͧଜٽޥ֧͵ا55 ੏ ։▁Ԗͣѹݝ࢝ͤ͵ॠࡄ։▁Ԗͣѹݝ࢝ͤ͵ॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 113 ՔѨԖיզଞУդͧࣼһॠࡄٽޥНܦࣂૃדզଞকஓٽՔѨԖݸיզଞУդͧࣼһॠࡄٽޥНܦࣂૃדզଞকஓٽ55 ݸ }} n=0n=0ڡЋэڡٽޥզଞڡЋэڡٽޥreturnn;returnn;////զଞ Өਗ਼ݤٽ૱Өਗ਼ݤٽ૱////;([n+=(_elem[in+=(_elem[i1]>_elem[i]);1]>_elem[i ࣼ଍Ҫॹتࣼ଍Ҫॹتfor(inti=1;i<_size;i++)for(inti=1;i<_size;i++) ////૵▁߳ߓ૵▁߳ߓ՞՞ intn=0;intn=0;////ਗ਼ݤ֗ਗ਼ݤ֗ intVector::intVector::disordereddisordered()const{()const{ ࣩ㔀ݤتࣼ଍Ҫॹٽ૱ࣩ㔀ݤ૞֚تࣼ଍Ҫॹٽ૱templateT>////૞֚ 55 څसڅसٽ૱ٽ૱ଞզଞࣩڅࣼ଍ҪॹࣩݤࣹͧՔ࣐јٽ૱ଞզଞࣩ֛ࠕͧڅࣼ଍ҪॹࣩݤࣹͧՔ࣐јٽ૱ࠕ֛ͧ 55 ٽ૱ٽ૱//ٽஎٽࣼ଍Ҫॹஎت▁ࣼ଍Ҫॹت▁ޥ㔀ޥ㔀//ڿџڿӦЗͧџٽӦЗͧٽٽݸٽݸ//ٽޥٽޥ ॠࡄࣩࣂ੕ࣼբٽܭॠࡄࣩࣂ੕ࣼբЉફࡆٽܭ55 Љફࡆ 㓬ՂҾ䞻ӯٽޥ㓬ՂҾ䞻ӯٽޥ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 128 x 1xcomparison comparisonx1 n mi x<= lo hi (b) >1;Rankmi=(lo+hi)>>1;////јЗ䎯Нે䎯јЗ䎯Нે䎯ͧͧকࠢ૊դगؓ࡬Ҵকࠢ૊դगؓ࡬Ҵ while(1T> зӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯBBͤ͵ؗࢻͤ͵ؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 16 127 x 1xcomparison comparisonx1 n mi x<= lo hi (b) T> FibonacciFibonacciߓۨ͵ؗࢻߓۨ͵ؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 13 124 1xcomparison comparisonx2 fibk1 fibk1 fib(k2) 1 0 fibk1 (b) fib(k1) 1 0 (a) fib(k2) 1fib(k1) 1 fibk11 x ӢӯНfib(kfib(k1)1) 11澝澝fib(kfib(k2)2) 11څӸ澝դ׾զଞࣩେͧފӢӯНиڅӸ澝դ׾զଞࣩେͧފи Ҝ੦n=n=fib(kfib(k)) 11ͧӨՔՇͧӨՔՇmi=fib(kmi=fib(k1)1) 11ਂͧױҜ੦ࠢਂͧױ55 ࠢ বएכੴৗڀڅߓۨେ֭ٴࠢ૊ࠊݤҞ਼ңͧتڅ࡬ڠবएਂৗӮ࣐૶כੴৗڀڅߓۨେ֭ٴࠢ૊ࠊݤҞ਼ңͧتڅ࡬ڠ55 ਂৗӮ࣐૶ ԯࣼբԯࣼբڅ࡬ڠ૶څ࡬ڠૃզٗ澝՗ӢݖӸࣩһିऑૃզٗ澝՗ӢݖӸࣩһିऑࠢ૊ࠊݤࠢ૊ࠊݤЈॗͧুЈॗͧু૶ ݘૡѾ֛֩ͧНЈ୰ՆࢻޥݘૡѾ֛֩ͧНЈ୰ՆࢻјІзӢߓࣩۨݝࢴѐޥ55 јІзӢߓࣩۨݝࢴѐ FibonacciFibonacciߓۨ͵ݘૡ㓢ષՂԼࣂߓۨ͵ݘૡ㓢ષՂԼࣂ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 12 123 +2 6 +1 5 +2 5 +1 4 +2 5 +1 4 +2 4 +1 3 +2 6 +1 5 +2 5 +1 4 +2 4 +1 3 2 (d) (c) (b) a)) 4.50=8/36=4.50=8/36= څߓۨେڅઑߓۨେסઑס֭ٴॗࠃࢴ㖊ӐЇ֧֭ͧٴ֧ॗࠃࢴ㖊ӐЇͧ ӢӯН͵{3,4,4,5,4,5,5,6}{3,4,4,5,4,5,5,6}څઑ㖊ӐͧߓۨେסӢӯН͵मڅઑ㖊ӐͧߓۨେסҺҺ88म 55 4.14=7/29=4.14=7/29= څߓۨେڅԄߓۨେۉԄۉ֭ٴॗࠃࢴ㖊ӐЇ֧֭ͧٴ֧ॗࠃࢴ㖊ӐЇͧ Н͵{4,3,5,2,5,4,6}{4,3,5,2,5,4,6}څԄߓۨେۉࣩڀتН͵ݿͧ՞ҪॹڅԄߓۨେۉࣩڀتn=7n=7ݿͧ՞Ҫॹ 55 څߓۨେ͵ͤڅзӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯAAͤ͵ߓۨେ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 11 122 +2 6 +1 5 +2 5 +1 4 +2 5 +1 4 +2 4 +1 3 +2 6 +1 5 +2 5 +1 4 +2 4 +1 3 2 (d) (c) (b) (a) НOO(1.50(1.5044logn)logn) ////ੵ੎ݟ޶ੵ੎ݟ޶څԄߓۨେۉ֭ٴͧױНࠢڅԄߓۨେۉ֭ٴͧױ55 ࠢ ੪Ѳڅॗ੔֭ٴ澝֯ޣ澝װޣઑߓۨͧёסԄЉۉت୻Ӣӯଢͧٮ੪Ѳૹڅॗ੔֭ٴ澝֯ޣ澝װޣઑߓۨͧёסԄЉۉت୻Ӣӯଢͧٮ55 ૹ searchlengthsearchlengthͤͤͣڅিߓһିऑࣩࠢ૊ࠊݤͧԮߓۨେͣڅিߓһିऑࣩࠢ૊ࠊݤͧԮߓۨେ Нॳ঑֩੪Ѳߓۨॠࡄࣩ㓬ৗͺޟѽױНॳ঑֩੪Ѳߓۨॠࡄࣩ㓬ৗͺޟѽױ 55 څߓۨେ͵ͤڅзӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯAAͤ͵ߓۨେ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 121 1 (4) 1 22 0 44 31 55 2 22 0 7 1212 6 99 5 88 4 55 2 44 1 22 0 77 3 5 88 4 99 75 1212 6 88 4 7 1212 6 99 5 88 4 55 2 44 1 22 0 77 3 (3) (2) (1) (3) (2) (1) ֭ؗ҅ৃݿOO(1)(1)ڠ֭ؗ҅ৃݿͶ՞૶ڠOO(logn)(logn)Ͷ՞૶څ࡬ڠવ઼͵ે䎯㔀ՇЗ䎯ͧ૶ڠ૶څ࡬ڠવ઼͵ે䎯㔀ՇЗ䎯ͧ૶ڠ૶ ߓۨٽѨиஎללߓۨͧٽѨиஎללͧ(T(nT(n)=T(n/2)+)=T(n/2)+OO(1)=(1)=OO(logn)(logn͵ڠ঎㓬૶͵ڠ55 ঎㓬૶ ઑסઑסדד[[55 S.search(S.search(33,0,7),0,7)͵Һক͵Һক 1+1+2=41+1+2=4ࠊࠢ૊֧ͧࠊࠢ૊֧ͧS[S[11 շЗշЗדד[[S.search(S.search(88,0,7),0,7)͵Һক͵Һক 2+1+2=52+1+2=5ࠊࠢ૊֧ͧࠊࠢ૊֧ͧS[S[44 55 څ޳וؗ҅Љ͵ͤڅ޳וзӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯAAͤ͵ؗ҅Љ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 136 ՌܯՌܯ()চࣩؗࢻͧ҄FibonacciFibonacciߓۨДߡ॔՟ߓۨДߡ॔՟search()searchےচࣩؗࢻͧ҄ݘૡޯৼے55 ݘૡޯৼ ૶Ӗڅݱߠૻٴܘ૶Ӗͤһିऑ֭ԓࢮैӢ١ݿܷͧҚߓۨЗ׾ୋங੐ࠆڅݱߠૻٴܘ33ͤһିऑ֭ԓࢮैӢ١ݿܷͧҚߓۨЗ׾ୋங੐ࠆ 㖊ӐЇͧFibonacciFibonacciߓۨЬՐ୻ߓۨЬՐ୻OO(logn)(logn)ݿ୎ݿ୎֯ޣ㖊ӐЇͧͤԮҍ֧֯ޣ22ͤԮҍ֧ НOO(1.50*log(n+1))(1.50*log(n+1))څઑߓۨେס֭ٴНࣩڅઑߓۨେס֭ٴ11ͤͤbinSearchbinSearch()()࢝ޯ࢝ޯAAࣩ ރ੩ੰރ55 ੰ੩ ਸ਼Әࠊͺ۝૤ডכৗיࢺͧ২ڱਸ਼Әࠊͺ۝૤ডכৗיࢺͧ২ڱwhilewhileقࣩӃق55 fibSearch()fibSearch()ࣩӃ ੿դ੿դ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 24 135 ࢐դӆૡਸ਼зӢߓۨ࢐դӆૡਸ਼зӢߓۨ Ӱ▁ࣩؓਇ֟سߓۨਇ֟বذӰ▁ࣩؓਇ֟நҮૹ૚ܷҚߓۨͧسߓۨਇ֟বذநҮૹ૚ܷҚߓۨͧ 55 ؗଢ଼Քਸ਼ࣩݱࡄؗଢ଼Քਸ਼ࣩݱࡄ ҴЪࡄ澝ୣࡄ૜ॠڕҴЪࡄ澝ୣࡄ૜ॠஏڕ55 ஏ ո““ਞரਞர””ۤٳԇࣩۤسՈބոۤٳԇࣩۤسՈބ 55 loglog22(n)=32(n)=32ͧͧloglog22(log(log22(n))=5(n))=5 n=2^(2^5)=2^32=4Gn=2^(2^5)=2^32=4Gݿݿͧױࠢͧױࠢ ߃ளޯۉীࠢ૊ݒҀۋͧל߃څ߃ளୣஂߓۨԙ୎ؠޯۉীࠢ૊ݒҀۋͧל߃څୣஂߓۨԙ୎ؠ ———— ދރѨԍЈٮૹދރѨԍЈٮ55 ૹ ͺڰժҚފͺͧڰժҚފͧ((55 ёёOO(logn)(logn)ӰӰOO(loglogn(loglogn ܷҚߓۨ͵㓬ৗܷҚߓۨ͵㓬ৗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 23 134 ɺɺ OO(loglogn(loglogn)) k>loglognk>loglogn k+loglogn<0k+loglogn<0 k*log(1/2)+loglogn<0k*log(1/2)+loglogn<0 (1/2)(1/2)kk  logn<1logn<1 n^(1/2)n^(1/2)kk <2<2ͺͺޥࠊࠢ૊Уդͧشיকޥࠊࠢ૊Уդͧشי55 ক n,n^(1/2),n^(1/2)n,n^(1/2),n^(1/2)22,...,n^(1/2),...,n^(1/2)kk,...,2,...,2 n,n,sqrt(nsqrt(n),),sqrt(sqrt(nsqrt(sqrt(n)),...,2)),...,2 јЇમԍবӖ͵ܘذڅߓۨԙ୎ؠڬͧފјЇમԍবӖ͵иܘذڅߓۨԙ୎ؠڬͧފ55 и 㖊Ӑ͵ࠠক▁ࠊࠢ૊ͧnnব২ব২sqrt(nsqrt(n)) //[Yao76]//[Yao76]֭ٴ㖊Ӑ͵ࠠক▁ࠊࠢ૊֭ͧٴ 55 㖊Ӑ͵OO(hi(hi lo)=lo)=OO(n)(n) ////ҿѼؗ҅ͺҿѼؗ҅ͺ֯ޣ͵㖊Ӑ֯ޣ 55 ܷҚߓۨ͵㓬ৗܷҚߓۨ͵㓬ৗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 22 133 0 1 2 3 4 5 6 7 8 9 101112131415161718 55 1010 1212 1414 2626 3131 3838 3939 4242 4646 4949 5151 5454 5959 7272 7979 8282 8686 9292 NOT_FOUNDNOT_FOUNDͤͤͣۉ؎ߓۨͣۉ؎ߓۨ Շ͵Շ͵ mi=10>1;Rankmi=(lo+hi)>>1;////јЗ䎯Нે䎯јЗ䎯Нે䎯ͧͧকࠢ૊դगؓ࡬Ҵকࠢ૊դगؓ࡬Ҵ while(loT> зӢߓۨͣ࢝ޯзӢߓۨͣ࢝ޯCCͤ͵ؗࢻͤ͵ؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 18 129 x 1xcomparison comparisonx1 n mi x<= lo hi (b) >1;intmi=(lo+hi)>>1;////јЗ䎯НࣙјЗ䎯Нࣙ ժӨ......ͧٽޥժӨԤҪॹԙ୎১࢐ͧٽޥif(hiif(hi lo<2)return;lo<2)return;////ԤҪॹԙ୎১࢐ voidVector::voidVector::mergeSortmergeSort(Rank(Rank lo,Rankhi){lo,Rankhi){////[lo,hi)[lo,hi) 55 templateT> ؗࢻ͵ٽܭٶڠؗࢻ͵ٽܭٶڠ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 142 Ӣ ੕ڠզଞ ࣩ૶ٽݸ ٶڠقզଞࣩ૵ٽޥ 6 3 2 7 1 5 8 4 6 3 2 7 1 5 8 4 6 3 2 7 1 5 8 4 6 3 2 7 1 5 8 4 63 72 51 84 7632 8541 87654321 O(nlogn)O(nlogn)ފੴڀࣩ૜ਸ਼ݿ୎ٽܭٶڠࠕͧױৗँਂފੴڀࣩ૜ਸ਼ݿ୎ٽܭٶڠࠕͧױ55 ਂँৗ ҅҅22͵͵xSortLabxSortLab 55 ҅҅11͵͵mergesortmergesort Ӧ //O(//O(nn))ٽ׾ٽޥٶӦ՟ٽ׾ٽޥٶ՟ 2T(n/2)T(n/2)//2// ٽܭڠ૶ٽܭڠӦ૶ٽӦ׾ٽ׾ Ӧ▁ӢНз //O(1)//O(1)ٽӦ▁ӢНзٽ //J.vonNeumann,1945//J.vonNeumann,1945 ////զଞЉӦ਽ૹ࣐զଞЉӦ਽ૹ࣐ 55 ////Ӣ࠿ज़ࣛӢ࠿ज़ࣛ Լࣂ͵ٽܭٶڠԼࣂ͵ٽܭٶڠ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 141 T.J.Watson,1943 foraboutfivecomputers. Ithinkthereisaworldmarket Ӣڶ՟ͧ՟РڶԍͧӢРלЇם զଞզଞ.2.2 ٽܭٶڠٽܭٶڠ((f)(f JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 140 ѽՉԖͺޥذѽՉԖͺͧޥذͧ””=<=<““ۉܦۉܦ””<<““55 ֧֧ifif▁Վࣩӭݮ޻ѝЗͧਂ۬▁Վࣩӭݮ޻ѝЗͧਂ۬ Ѹ঴ܦѸ঴ুрܦрুٽ૱ٽ૱Зͧзী֛ܦЇ޼▁ૅ۟ܵрܯЗͧзী֛֧ܦЇ޼▁ૅ۟ܵрܯ֧ ૝ࣻ২ࣻ২ࣼ଍ࣼ଍ܯ૝ܯзীࣼкͧܦзীࣼкকӢӯЉҾ؈ҪॹࣩрͧܦকӢӯЉҾ؈Ҫॹࣩр मՔৗ͵▁ޥѸ঴Ն࣏ՉԖͧՐتमՔৗ͵ࣩࣼ▁ޥѸ঴Ն࣏ՉԖͧՐتЗͧҪॹaaոոbbࣩࣼٽܭЗͧҪॹ֧ફࡆٽܭ55 ֧ફࡆ ࣩ͜НьСͺފऻࣩؓէͺފॠࡄٽܭࣩ͜НьСͺјІફࡆފऻࣩؓէͺފॠࡄٽܭјІફࡆ 1,2,3,4,5,6,1,2,3,4,5,6,77aa,,77cc,,77bb,8,8//unstable//unstable ૎Ӟ͵૎Ӟ͵ 1,2,3,4,5,6,1,2,3,4,5,6,77aa,,77bb,,77cc,8,8//stable//stable ૎Ҵ͵૎Ҵ͵ 6,6,77aa,3,2,,3,2,77bb,1,5,8,,1,5,8,77cc,4,4 ЈՉͺܕժҐފͧٽࠊتӦЗࣩࣼٽҪॹ֧૎Ҵ澝૎ӞוЈՉͺଜܕժҐފͧٽࠊتӦЗࣩࣼٽҪॹ֧૎Ҵ澝૎Ӟוଜ Н঑৩ࣩੌ࠭ޟފН঑৩ࣩੌ࠭ͤޟފҪॹݿͧॠࡄࣩऻؓ㓬ऻؓ㓬ͣͣstablilitystablilityͤוҪॹݿͧॠࡄࣩ૎Ҵլଜו55 ૎Ҵլଜ O(nO(n22))֯ޣͧ֯ޣͧ(O(n)O(nװޣݦݤݤঐࣩ࢝ޯࣼբͧتݝࢴЉॕ▁्ଢװޣݦݤݤঐࣩ࢝ޯࣼբͧت55 ݝࢴЉॕ▁्ଢ ণ՟੪ўণ՟੪ў DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 139 {{ ڹߗٽޥ૞֚ڹߗٽޥreturnsorted;returnsorted;////૞֚ {{ ٽޥ଑ف҄ܦૹ૚рٽޥ଑ف҄ܦswap(_elem[loswap(_elem[lo1],_1],_elem[loelem[lo]);]);////ૹ૚р ୻ੌٶͧٽޥݦѼޭظյअڿ୻ੌٶͧٽޥݦѼޭظյअڿ////;sorted=false;sorted=false Өͧٽ૱Өਂͧٽ૱ਂ////}([}([if(_elem[loif(_elem[lo1]1]>> __elem[loelem[lo ࣼ଍Ҫॹتࣼ଍Ҫॹ১ٗզ՗ͧ૵▁߳ߓ՞تwhile(++lo::boolVector::bubble(Rankbubble(Rank lo,Rankhi){lo,Rankhi){////▁年۟ܵр templateT> 55 ٽࣻ২ҵͧܦрܵ۟ٽࣻ২ҵͧܦ{while(!{while(!bubble(lobubble(lo,hi,hi));}));}////૵年Ҟ૵年Ҟ۟ܵр voidVector::voidVector::bubbleSort(RankbubbleSort(Rank lo,Rankhi)lo,Rankhi) templateT>//0<=lo::voidVector::sortsort(Rank(Rank lo,Rankhi){lo,Rankhi){////ԙ୎ԙ୎[lo,hi)[lo,hi) 55 templateT> ঞ▁ҴՌ͵֗ٽܭঞ▁ҴՌ͵֗ٽܭ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 137 զଞզଞ.2.2 ٽܭફࡆٽܭ(e)(e)ફࡆ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 152 00 1122 3344 55 6677 88 00 11 22 33 44 55 66 77 88 ՃD...D...ࣩբ؅ࣩբ؅ZZیՃࣩیࣩަՃAAࣩщ㛑ࣩщ㛑BBࣩբжࣩբжCCࣩۊࣩަՃࠢ֕͵ۨӰۊ55 ࠢ֕͵ۨӰ ࣐ͧۨӰࢤࣩؓৼ䎯ڕӮ࣐ৼ䎯У୎ࣩࣼкুૃڀ੹ͧފ࣐ͧۨӰࢤࣩؓৼ䎯ЬػڕӮ࣐ৼ䎯У୎ࣩࣼкুૃڀ੹ͧފЬػ ੧ୋӦ਽Ҫॹړ੧ୋӦ਽ҪॹࣩͤݱړѸ঴੧ୋͣcallcallbybypositionpositionࣩͤݱڱݘ࣐ڀѸ঴੧ୋ֛ͣࠕͧڱݘ࣐ڀ55 ֛ࠕͧ Н(n+1)/2=(n+1)/2=OO(n)(n)څ޳וӢ١Н҅ͧԤࠊ੧ୋࣩޫީ֭ٴНјڅ޳וӢ١Н҅ͧԤࠊ੧ୋࣩޫީ֭ٴј 䰹੧ୋٜЈ՟ݿؕ ////Їৼੵ੕Їৼੵ੕ڱ૚ளͧࠕݿࣩޯۉ䰹੧ୋٜЈ՟ݿؕ࢐ু֛Нڱ૚ளͧࠕݿࣩޯۉ55 ࢐ু֛Н ࣐Ӹૡͣդ૭ͤiiࠖࠖڕॏӞՆͧࡀͤـͣע࣐Ӹૡͣդ૭ࣩͤҪॹͧஏёڕॏӞՆͧࡀͤـͣעНۨӰ䰹ННۨӰ䰹НiiࣩҪॹͧஏё ࣩҪॹڀتӦͧӦ਽֠࢐ЬՔૹ૚䰹ۨӰٽ঎㓬يࣩҪॹݹ࢐բڀتӦͧӦ਽֠࢐ЬՔૹ૚䰹ۨӰٽ঎㓬ي55 ݹ࢐բ ࣒䰹ӰѸ঴࣒䰹ӰѸ঴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 151 00 1122 3344 55 6677 88 00 11 22 33 44 55 66 77 88 Քժ੃Ӧ਽ࡀ࣐ͺͧړՔժ੃Ӧ਽ࡀ࣐ͺૠमளݝࣩݱͧړ55 ૠमளݝࣩݱ ࣩ֩ࣂѸ঴ =V+i=V+issېࣩ֩ࣂѸ঴ࣩѺېӨୈ࢞ՙНӨୈ࢞ՙНiiࣩѺ ࣩ֩ࣂ୎઴֭Нssېࣩ֩ࣂ୎઴֭Нͧ՞Ѻې55 ࠢ֕͵ࠢ֕͵ Ҝ੦ࡀԗц٠ࡢࡨԙࣩਹଂҜ੦ࡀԗц٠ࡢࡨԙࣩਹଂVVͧ՞Ѻ V[iV[i]]ࣩࢢࣂࣩ֩֬ࢢࣂ֩֬ =V+i=V+issͧͧssНԤЖԤҪԨ࣐ࣩी୎ଞНԤЖԤҪԨ࣐ࣩी୎ଞ गؓҾࢢࣂ֩֬ܯगؓҾࢢࣂ֩֬ݿ୎ӃࣻܯҪॹࣩ䰹ͧՔ֧OO(1)(1)ݿ୎ӃࣻܨݤܨҪॹࣩ䰹ͧՔ֧ߠܨݤܨߠ ړࣩݱͤړ䰹੧ୋͣcallcallbybyrankrankࣩͤݱڱܕ䰹੧ୋͣզଞݖڱܕ55 զଞݖ ё䰹ӰѸ঴ё䰹ӰѸ঴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 150 00 1122 3344 55 6677 88 00 11 22 33 44 55 66 77 88 Ӹ஫//դটࣩৼ䎯वҀդটࣩৼ䎯वҀநநͣͣfirst/frontfirst/frontͤͤ//ޮޮͣͣlast/rearlast/rearͤৼ䎯ͤৼ䎯ޥӸ஫࠻ޥ55 ࠻ ࢐։▁ڶӨ֧ͧ؁դটਂۋ࢐։▁Ӹ஫ڶӨ֧ͧ؁դটਂۋӸ஫ դটͣsuccessorsuccessorͤͤۋդটͣͤۋࠕкवӸ஫ͣpredecessorpredecessorͤڨࠕкवӸ஫ͣࣼ଍ৼ䎯ڨ55 ࣼ଍ৼ䎯 L={aL={a00,a,a11,...,a,...,ann11 }} ӦٽӦІࣩ঎㓬ٽЖ૽્૽્Іࣩ঎㓬▁ۉ߄ͧܯࠕ৊ڨ࣐ڕۋଢܗЖ՞ৼ䎯ૹ૚▁ۉ߄ͧܯࠕ৊ڨ࣐ڕۋଢܗ՞ৼ䎯ૹ૚ ҾЗࣩҪॹवҀৼ䎯ͣҾЗࣩҪॹवҀৼ䎯ͣnodenodeͤͤ ज़ࣩࣛӀֹগ߄؁ଙ࣐ԇ㓆Ҥފज़ࣩࣛӀֹগ߄ͤ؁ଙ࣐ԇ㓆Ҥފ55 Ӧ਽ͣӦ਽ͣlistlistͤ ёզଞӰӦ਽ёզଞӰӦ਽ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 149 ளݝࣩԇ㓆ԇ㓆ݒҀݒҀܕளݝࣩՔݖܕՔݖ ЖݦѼ▁ۉڤࠕࣩࢢࣂ֩֬ͧڨڢЖݦѼ૽્Іࣼ଍ࣩҪॹ੢▁ۉڤࠕࣩࢢࣂ֩֬ͧڨڢ૽્Іࣼ଍ࣩҪॹ੢ Ҫॹԇ㓆֩Ӣକո֚ݗࣩࢢࣂी୎ܨҪॹԇ㓆֩Ӣକո֚ݗࣩࢢࣂी୎Н՞ݤܨԇ㓆͵ԇ㓆͵ Н՞ݤ 22ͤͤ ڀت঎㓬ٽզଞͧҪॹࣩࢢࣂ֩֬ЉҾ૽્ࠊױࠢڀت঎㓬ٽզଞͧҪॹࣩࢢࣂ֩֬ЉҾ૽્ࠊױࠢ ளݝࣩ஁㓆஁㓆ݒҀݒҀܕளݝࣩՔݖܕՔݖ Дߡ▁৩ٽЉҾ૽્ࠊٽҤࠊ؁ҪॹࣩࢢࣂܨДߡ▁৩ݤٽЉҾ૽્ࠊٽҤࠊ؁Ҫॹࣩࢢࣂܨݤ ଵࠞۋڎी୎ݦѼӪܨଵࠞݤۋڎी୎ݦѼӪܨ11ͤͤ ஁㓆͵஁㓆͵ ݤ ЬӢНГमړҤиঐ঒ݱ؁Ҫॹࣩܨݤͧ֩ڀتࣼړЬӢНГमЉݒҀݱړҤиঐ঒ݱ؁Ҫॹࣩܨݤͧ֩ڀتࣼړ55 ЉݒҀݱ ݘՉ͵insertinsert澝澝removeremoveذݦѼۋ଑فগ߄ࣩܨݘՉ͵ԇ㓆͵୻ӉҴͧݤذݦѼۋ଑فগ߄ࣩܨ22ͤͤ ԇ㓆͵୻ӉҴͧݤ ЈՉ͵getget澝澝searchsearchذۉՂঐ؟গ߄ࣩӃܨЈՉ͵஁㓆͵э੾ՇͧݤذۉՂঐ؟গ߄ࣩӃܨ஁㓆͵э੾Շͧݤ 11ͤͤ ړ৩ӢНГ६ݱלݒҀޥےগ߄ͧܨժҒݘݤފܨߠړ৩ӢНГ६ݱלݒҀޥےগ߄ͧܨժҒݘݤފܨ55 ߠ ё஁㓆Ӱԇ㓆ё஁㓆Ӱԇ㓆 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 148 RobinMilner(1934~2010) Don'tlosethelink. ՌЉؗࢻܯՌЉؗࢻܯ((a)(a 3.3.Ӧ਽Ӧ਽ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 147 ѐ୻(nlogn)(nlogn)ݿ୎ݿ୎ͧͤٽޥ૝ܯۋٽޥ㖊Ӑٜͣকװޣѐ୻Ԯ֧҄ͧͤٽޥ૝ܯۋٽޥ㖊ӐٜͣকװޣԮ֧҄ ॗ੐ࠆࣩોԈी୎تॗ੐ࠆࣩોԈी୎ஂػ֩ͧ୻ੌتஂػ֩ͧ୻ੌ 55 ভ䎯ভ䎯 ਸ਼Ԗٶиބਸ਼Ԗٶиބ PageRankPageRank ———— ٽܭ଑ט㓬߃҃ͧԜӢ૯ؕиىՔ۞ٽܭ଑ט㓬߃҃ͧԜӢ૯ؕиىՔ۞ ՔҐ੩ऻؓ ———— Ӟࢻ୹բҪॹݿͧٗҊ׾զଞѨҮӞࢻ୹բҪॹݿͧٗҊ׾զଞѨҮͧڡՔҐ੩ऻؓՐੌؗࢻ㔵ͧڡՐੌؗࢻ㔵 הझ٪У६ࣩ੦הझ٪У६ࣩ੦ ӦٽӦӦ਽У६ࣩٽӦ਽У६ࣩ Ҿ૯࣐иغҾ૯࣐иغ ———— ੧ୋٽ੧ୋЈ୻୩޲੾Ӊͧ؎ҵஎٽЈ୻୩޲੾Ӊͧ؎ҵஎ ॠࡄٽܭॠࡄ㓬ৗࣩॕ▁ЖٽܭѨO(nlogn)O(nlogn)㓬ৗࣩॕ▁Жޣ㖊ӐЇ֯ޣѨؗࢻޣ㖊ӐЇ֯ޣؗࢻ 55 Ѩ䎯Ѩ䎯 ণ՟੪ўণ՟੪ў DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 146 lo mi hi BB lblb CC lclc _elem_elem AA Ӧ ———— Ӧ਽ͣЇ▁्ͤӦ਽ͣЇ▁्ͤٽӦؗଢ଼Іͧૠ▁ॠࡄՂগ੥Ь૯࣐иՏ▁६ٽ55 ؗଢ଼Іͧૠ▁ॠࡄՂগ੥Ь૯࣐иՏ▁६ ॗେͧԮҩ੤lblb lclcͧͧmimi (lo+hi)/2(lo+hi)/2ڶӦЈٽ׾ٶڠڬ͵ڿॗେͧԮҩ੤ࡉڶӦЈٽ׾ٶڠڬ͵ڿࡉ 55 ٽޥٜ֭ٽޥЈऋࣽ ———— ࠣौࠣौBBոոCCٜ֭ٶЈऋࣽЇࣙٶॠࡄࣩ(nlogn)(nlogn)Їࣙٽܭॠࡄࣩૠ▁গ੥Љٽܭ55 ૠ▁গ੥Љ ݜऍ͵㔀Ѽ૨іЈ૚ݜऍ͵㔀Ѽ૨іЈ૚O(n)O(n)ࠊͧࠊͧ঎㓬঎㓬ݿ୎͜ݿ୎͜ 55 ݹ࢐͵ݹ࢐͵j+kT> voidVector::voidVector::mergemerge(Rank(Rank lo,Rankmi,Rankhi){lo,Rankmi,Rankhi){ ؗࢻ͵ٶڠؗࢻзષ͵ٶڠзષ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 160 ݝࢴѹЇͧЈ࣐ؕ҄ݝࢴѹЇͧЈ࣐ؕ҄ (1+2+3+...+n)/n=(n+1)/2=O(n)(1+2+3+...+n)/n=(n+1)/2=O(n) Нڅ޳וӢ١Н҅ͧԤࠊ੧ୋࣩޫީ֭ٴНјڅ޳וӢ١Н҅ͧԤࠊ੧ୋࣩޫީ֭ٴј ੧ୋৼ䎯ࣩ䰹ڬ੧ୋৼ䎯ࣩ䰹ͧ঎㓬ࠔࠢиڬНO(rO(r))ͧ঎㓬ࠔࠢиڅ޳וНݿ୎څ޳ו55 ݿ୎ }} returnp;returnp;////ࣹߗৼ䎯ࣹߗৼ䎯 ފЖৼ䎯Ԯފwhile(0succ;>succ;////எݤॕஎݤॕrrЖৼ䎯Ԯ Posi(TPosi(T)p=first();)p=first();////ёநৼ䎯ӞՆёநৼ䎯ӞՆ Posi(TPosi(T)List::)List::operator[]operator[](int(int r)const{r)const{////O(rO(r)) 55 templateT>//assert:0<=r::~List(){::~List(){////Ӧ਽߅߄֗Ӧ਽߅߄֗ 55 templateT> }} returnreturnoldSizeoldSize;;////ࣻ২Ӧ਽Չीࣻ২Ӧ਽Չी Ӭୣநৼ䎯וӬୣநৼ䎯Յוwhile(0<_size)while(0<_size)removeremove(header(header>succ);>succ);////Յ intintoldSizeoldSize =_size;=_size; intintListList::::clearclear(){(){////࡯ीӦ਽࡯ीӦ਽ 55 templateT> ߅߄߅߄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 157 pred succ ^trailer pred last pred succ ^ header succ first visiblelist pred trailer succ header list ^empty ^ ৼ䎯ࣩ䰹ՔјӢӯࣂ੕Н11澝澝00澝澝nn11澝澝nnـ澝ந澝ޮ澝עৼ䎯ࣩ䰹ՔјӢӯࣂ੕Нॗݝ֩ͧـ澝ந澝ޮ澝ע55 ॗݝ֩ͧ }} ੐ࠆڢ੐ࠆ੢ڢ_size=0;_size=0;////੢ trailertrailer>pred=header;>pred=header; trailertrailer>succ=NULL;>succ=NULL;////к৊к৊ headerheader>succ=trailer;>succ=trailer; headerheader>pred=NULL;>pred=NULL;////к৊к৊ ҽৼ䎯⫩ـڎҽৼ䎯Ӫ⫩ـڎtrailer=newtrailer=newListNodeListNode;;////Ӫ ҽৼ䎯⫩עڎҽৼ䎯Ӫ⫩עڎheaderheader =new=newListNodeListNode;;////Ӫ ઊݿঞ▁ઁ࣐تӦ਽ڎઊݿঞ▁ઁ࣐Ӧ਽ӫ׶ԖͧӪتӦ਽ڎvoidvoidListList::::initinit(){(){////Ӧ਽ӫ׶ԖͧӪ 55 templateT> ߄ૼ߄ૼ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 156 pred succ ^trailer pred last pred succ ^ header succ first visiblelist }};; Ռ */*/ܯՌ澝ଁԶܯՌ澝ՔӉܯՌ߄ૼӠݤ澝߅߄Ӡݤ澝Ր੾ܯՌ澝ଁԶܯՌ澝ՔӉܯ/*.../*...߄ૼӠݤ澝߅߄Ӡݤ澝Ր੾ public:public: /*.../*...Ӄ଑ӠݤӃ଑Ӡݤ **// protected:protected: ҽ⫩ҽ⫩ــҽ澝⫩ҽ澝⫩עעint_size;int_size;Posi(TPosi(T)header,)header, trailer;trailer;////੐ࠆ澝੐ࠆ澝 private:private: templateclassT>classListList {{////Ӧ਽ࠆ߂६Ӧ਽ࠆ߂६ ҴӦ਽ৼ䎯६ڕҴӦ਽ৼ䎯६ڕ////""55 #include"#include"listNode.hlistNode.h Ӧ਽͵Ӧ਽͵ListListࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 155 Ӧ਽Ӧ਽ ؓܗઊتࣂݱࡄ࣒ӠݤדԶӦ਽ͧଁؓܗઊتࣂݱࡄ࣒Ӡݤדtraverse()traverse() ଁԶӦ਽ͧ Ӧ਽ٽޥӦ਽ٽޥ ৼ䎯וৼ䎯⣞ୣଜוuniquify()uniquify() ⣞ୣଜ ৼ䎯 Ӧ਽Ӧ਽וৼ䎯⣞ୣଜוdeduplicate()deduplicate() ⣞ୣଜ Ӧ਽ٽޥӦ਽ٽޥ ࣩৼ䎯לޣࣩৼ䎯Ћ䰹לޣиeeЋ䰹לиͧ૞֚Јלsearch(esearch(e)) ߓࣹۨߗҪॹߓࣹۨߗҪॹeeͧ૞֚Ј ઑݿ૞֚NULLNULL Ӧ਽Ӧ਽סઑݿ૞֚ͧסfind(efind(e)) ߓࣹۨߗҪॹߓࣹۨߗҪॹeeͧ Ӧ Ӧ਽Ӧ਽ܭٽୠஂܘӦઁݦ՞ৼ䎯ࣩѸ঴ͧ҄Уܭٽୠஂܘsort()sort() ઁݦ՞ৼ䎯ࣩѸ঴ͧ҄У Ӧ Ӧ਽Ӧ਽ܭٽୠஂܘժٜފৼ䎯ޥےӦӭݮܭٽୠஂܘժٜފৼ䎯ޥےdisordered()disordered() ӭݮ ࣐ Ӧ਽Ӧ਽ڕࣩৼ䎯ͧ૞֚Ҿד࣐ڕࣩৼ䎯ͧ૞֚Ҿדremove(premove(p)) ӬୣѸ঴ӬୣѸ঴pp Ӹ஫澝դটܷҴ Ӧ਽Ӧ਽ܯӸ஫澝դটܷҴࣩࣻܯҀৼ䎯ppࣩࣻڡҀৼ䎯ڡeeذذ (insertAfter(pinsertAfter(p,e),e insertinsertBeforeBefore(p(p,,e)e) Ҁந澝ޮৼ䎯ܷҴ Ӧ਽Ӧ਽ڡҀந澝ޮৼ䎯ܷҴڡeeذذ ((insertAsLast(einsertAsLast(e insertAsFirst(einsertAsFirst(e)) last()last() ૞֚ந澝ޮৼ䎯ࣩѸ঴૞֚ந澝ޮৼ䎯ࣩѸ঴ Ӧ਽Ӧ਽ first()first() Ӹࣩ੐ࠆͣৼ䎯㔀ݤͤ Ӧ਽Ӧ਽ڡӸࣩ੐ࠆͣৼ䎯㔀ݤͤ۶ղӦ਽ڡsize()size() ۶ղӦ਽ ઊتઊ૯࣐تՌ ԄৗԄৗ ૯࣐ܯՌݒҀܯݒҀ ՌܯՌܯӦ਽͵Ӧ਽͵ADTADT DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 154 data succpred };}; Posi(TPosi(T))insertAsSuccinsertAsSucc(T(T const&e);const&e);////դܷҴդܷҴ Posi(TPosi(T))insertAsPredinsertAsPred(T(T const&e);const&e);////ӸܷҴӸܷҴ ::data(edata(e),),pred(ppred(p),),succ(ssucc(s){}){}////ூੜ߄ૼ֗ூੜ߄ૼ֗ ListNode(Te,Posi(T)p=NULL,Posi(T)s=NULL)ListNode(Te,Posi(T)p=NULL,Posi(T)s=NULL) headerheaderոոtrailertrailerࣩ߄ૼࣩ߄ૼتଢتListNode(){}ListNode(){}////ଢ Posi(TPosi(T)succ;)succ;////դটդট Posi(TPosi(T)pred;)pred; ////Ӹ஫Ӹ஫ Tdata;Tdata;////ݤҚݤҚ ؗࢻͤړڤؗࢻͤӦ਽ৼ䎯ࠆ߂६ͣјՄզ଴਽ړڤstructstructListNodeListNode {{////Ӧ਽ৼ䎯ࠆ߂६ͣјՄզ଴਽ ੇخڅݚুЈӆ૚ڏफ़ࡍફ੎ͧ؎ҵੇخڅݚুЈӆ૚ڏ55 templateT> ////फ़ࡍફ੎ͧ؎ҵ //ISOC++.0x,templatealias//ISOC++.0x,templatealias 55 #define#definePosi(TPosi(T))ListNodeListNode**////Ӧ਽ৼ䎯Ѹ঴Ӧ਽ৼ䎯Ѹ঴ Ӧ਽ৼ䎯͵Ӧ਽ৼ䎯͵ListNodeListNodeࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 153 00 1122 3344 55 6677 88 00 11 22 33 44 55 66 77 88 ઊeeͧ૞֚ݰৼ䎯Ѹ঴ͧ૞֚ݰৼ䎯Ѹ঴ت࣐ڕҴ੃؁ઊܷҴդটৼ䎯ͧت࣐ڕҴ੃؁insertinsertAsSuccAsSucc(e(e)) ܷҴդটৼ䎯ͧ ઊeeͧ૞֚ݰৼ䎯Ѹ঴ͧ૞֚ݰৼ䎯Ѹ঴ت࣐ڕҴ੃؁ઊܷҴӸ஫ৼ䎯ͧت࣐ڕҴ੃؁insertinsertAsPredAsPred(e(e)) ܷҴӸ஫ৼ䎯ͧ ઊتܨݤ؁ےӸৼ䎯ڡઊتܨݤ؁ےӸৼ䎯ڡ ()data()data Ӹৼ䎯դটৼ䎯ࣩѸ঴ڡӸৼ䎯դটৼ䎯ࣩѸ঴ڡ ()succ()succ Ӹৼ䎯Ӹ஫ৼ䎯ࣩѸ঴ڡӸৼ䎯Ӹ஫ৼ䎯ࣩѸ঴ڡ ()pred()pred ݒҀݒҀ ԄৗԄৗ ՌܯࣩݒҀޯׁٳংؓਂٶՌНࠕͧՔ੦঴ܯࣩݒҀޯׁٳংؓਂٶНࠕͧՔ੦঴ ؗࢻؗࢻ””ੇخੇخ““55 ҀНӦ਽ׁࣩޯҪॹͧӦ਽ৼ䎯நҮ୻ੌࢮै֩ҀНӦ਽ׁࣩޯҪॹͧӦ਽ৼ䎯நҮ୻ੌࢮै֩ ՌܯՌܯӦ਽ৼ䎯͵Ӧ਽ৼ䎯͵ADTADT DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 168 Ӧ਽ٽޥӦ਽ٽޥ((c)(c 3.3.Ӧ਽Ӧ਽ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 167 }} while((p=pwhile((p=p>succ)!=trailer)>succ)!=trailer)visit(pvisit(p>data);>data); Posi(TPosi(T)p=header;)p=header; ઊتઊӠݤتvoidvoidListList::::traversetraverse(VST(VST&visit){&visit){ ////Ӡݤ templateVST> 55 templateT> }} while((p=pwhile((p=p>succ)!=trailer)>succ)!=trailer)visit(pvisit(p>data);>data); Posi(TPosi(T)p=header;)p=header; ଢܗଢӠݤܗvoidvoidListList::::traversetraverse(void(void (*(*visit)(Tvisit)(T&)){&)){ ////Ӡݤ 55 templateT> ଁԶଁԶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 166 55 ࠔग㓬ՂݝࢴӢ߅ࣩݱࡄЉগ੥ͧЉࠔग㓬ՂݝࢴӢ߅ࣩݱࡄЉগ੥ͧЉVector::Vector::deduplicatededuplicate()()ࣼբࣼբ }} returnreturnoldSizeoldSize _size;_size;////Ӧ਽੐ࠆՉԖଞͧԮ੃ӬୣҪॹ㔀ݤӦ਽੐ࠆՉԖଞͧԮ੃ӬୣҪॹ㔀ݤ Ӹ஫кЈࣼբޥےӸ஫кЈࣼբࣩޥےݿӵͧppࣩڿࢺ૚सЗࣩџڱݿӵͧڿࢺ૚सЗࣩџڱ:}}//assert://assert ӨӬୣУͶժӨ䰹ԅ▁֧ͧ؁ӨӬୣУͶժӨ䰹ԅ▁ਂࣩग֧ͧ؁q?q?removeremove(q(q):r++;):r++;////ਂࣩग Posi(TPosi(T)q=)q=findfind(p(p>data,r,p);>data,r,p);////֧Ӹ஫Зߓۨ୹բী֧Ӹ஫Зߓۨ୹բী while(trailer!=(p=pwhile(trailer!=(p=p>succ)){>succ)){////҈ࠊࣻӰޮৼ䎯҈ࠊࣻӰޮৼ䎯 Posi(TPosi(T)p=first();Rankr=1;)p=first();Rankr=1;//p//pёநৼ䎯ફёநৼ䎯ફ Լ੐ࠆڢԼ੐ࠆ੢ڢintintoldSizeoldSize =_size;=_size;////੢ וәӦ਽১࢐ݸଜٴוәӦ਽১࢐ݸଜٴ////;if(_size<2)return0;if(_size<2)return0 ৼ䎯וӦ਽Зࣩଜٽৼ䎯⣞ୣݸוӦ਽ЗࣩଜٽintintListList::::deduplicatededuplicate(){(){////⣞ୣݸ 55 templateT> ։▁Ԗ։▁Ԗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 165 55 ਂਂremoveremove(header(header>succ)>succ)ݘҀݘҀremoveremove(trailer(trailer>pred)>pred)⩲ͺ⩲ͺ }} //O(n)//O(n)ͧ঎㓬ࠔࠢиӦ਽੐ࠆͧ঎㓬ࠔࠢиӦ਽੐ࠆ returnreturnoldSizeoldSize;; while(0<_size)while(0<_size)removeremove(header(header>succ);>succ); intintoldSizeoldSize =_size;=_size; Ӭୣநৼ䎯ͧࣻ২਽ीוӬୣநৼ䎯ͧࣻ২਽ी࡯ीӦ਽͵ՅוintintListList::::clearclear(){(){////࡯ीӦ਽͵Յ 55 templateT> {{clearclear();deleteheader;deletetrailer;}();deleteheader;deletetrailer;} ҽ⫩ـ澝עҽ࡯ीӦ਽ͧଚݚ⫩ـ澝עListList::::~List~List()()////Ӧ਽߅߄֗Ӧ਽߅߄֗͵͵࡯ीӦ਽ͧଚݚ 55 templateT> ߅߄߅߄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 164 (d) succ pred this pred succ pred (c) succ pred succ pred (b) pred succ p succ pred succ (a) pred succ p succ pred pred succ }} ѠݤҚהѠݤҚהdeletep;_sizedeletep;_size;; returne;returne;////૞֚૞֚ pp>pred>pred>succ=p>succ=p>succ;>succ; pp>succ>succ>pred=p>pred=p>pred;>pred; ટҚͤͤܯટҚՔࣻܯӬୣৼ䎯ݤҚͣ੦ͣ੦६ֹ६ֹTTՔࣻڬѠהӬୣৼ䎯ݤҚڬѠה////;Te=pTe=p>data;>data TTListList::::removeremove(Posi(T(Posi(T)p){)p){ //O(1)//O(1) ৼ䎯ͧ૞֚ҾݤҚדৼ䎯ͧ૞֚ҾݤҚד55 templateT>////Ӭୣ՟ࡄѸ঴Ӭୣ՟ࡄѸ঴pp ӬୣӬୣ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 163 {{copyNodescopyNodes(L[r],n);}(L[r],n);} ListList::::ListList((ListListconst&L,intr,intn)const&L,intr,intn) ////O(O(rr+n+n)) {{copyNodescopyNodes(L.first(L.first(),(),L._sizeL._size);});} ListList::::ListList((ListListconst&L)const&L) ////O(_sizeO(_size)) ՌܯՌଜૉࣩܯ55 ଜૉࣩ }} {{insertAsLastinsertAsLast(p(p>data);p=p>data);p=p>succ;}>succ;} ફ১ppࣩࣩnn஍҈ࠊҀНޮৼ䎯ܷҴ஍҈ࠊҀНޮৼ䎯ܷҴذફ১ذ////((whilewhile (n(n Ҟӫ׶Ԗٶҽৼ䎯⫩ـ澝עڎҞӫ׶ԖӪٶҽৼ䎯⫩ـ澝עڎinitinit();();////Ӫ voidvoidListList::::copyNodescopyNodes(Posi(T(Posi(T)p,intn){)p,intn){ //O(n)//O(n) ՌܯՌׁޯܯޯׁ//// <55 templateT ӱࣩ߄ૼוӱࣩ߄ૼׁиוׁи DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 162 (a) succ pred this pred succ (b) succ succ pred this pred succ pred new (c) succ pred succ succ this pred pred new (d) pred succ new succ pred this pred succ }} ૞֚ݰৼ䎯ࣩѸ঴ͧܯ଴ैڎ૞֚ݰৼ䎯ࣩѸ঴ͧܯ଴ैڎ////;predpred>succ=x;pred=x;>succ=x;pred=x; returnx;returnx ݿ100100ҔͤҔͤৃͣڎݿӪৃͣڎPosi(TPosi(T)x=new)x=newListNodeListNode(e(e,pred,this);,pred,this);////Ӫ Posi(TPosi(T))ListNodeListNode::::insertAsPredinsertAsPred(T(T const&e){const&e){//O(1)//O(1) वͤتवͤӸܷҴॠࡄͣդܷҴॠࡄ؎ҵت55 templateT> ////ӸܷҴॠࡄͣդܷҴॠࡄ؎ҵ ҀppࣩӸ஫ܷҴࣩӸ஫ܷҴڡҀڡ{_size++;returnp{_size++;returnp>>insertAsPred(einsertAsPred(e);});}//e//e ListNodePosi(T)ListNodePosi(T)ListList::::insertBeforeinsertBefore(ListNodePosi(T)p,Tconst&e)(ListNodePosi(T)p,Tconst&e) 55 templatetemplate ܷҴܷҴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 161 {return{returnfindfind(e(e,_size,,_size,trailertrailer);});} ՌܯߓۨفՌଜૉҵܯߓۨف55 Posi(TPosi(T))findfind(T(T const&e)constconst&e)const////ଜૉҵ L.L.findfind(e(e,n,p)?cout<data:cout<<"notfound";>data:cout<<"notfound"; ૹ૚૞֚Қӭؓ͵ړૹ૚૞֚ҚӭؓӀֹࣩઁ࣐ࠆ͵ړ55 Ӏֹࣩઁ࣐ࠆ Нफ़ࡍޟࣂדڰ֧҄؁Нफ़ࡍࣩޟࣂדڰ֧҄؁}}////headerheaderࣩ ઑסઑߓۨסյअߓۨڿյअਂભӞٗ૕ࣙͧڿreturnNULL;returnNULL;////ਂભӞٗ૕ࣙͧ ԄۉԄߓۨۉif(e==(p=pif(e==(p=p>pred)>pred)>data)returnp;>data)returnp;////ߓۨ تЖӸ஫ͧё՗զٗ૵Жࠢت૝ࣩnnЖӸ஫ͧё՗զٗ૵Жࠢޣ૝ࣩࣩޣиppࣩتиت////((while(0:::: ////֧֧ppࣩࣩ O(n)O(n)څ޳וߓۨͧٽஎڅ޳וߓۨͧٽ55 templateT>////எ ߓۨߓۨ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 176 ҼયܕҼયҐܕॠࡄ ////ҐٽܭॠࡄࣩٽܭӰO(nlogn)O(nlogn)ࣩڰࠕैԮՔјױ࢐ͧڡӰڰࠕैԮՔјױ࢐ͧڡ 55 গ߄ͧselectMax()selectMax()Քݘૡ২Քݘૡ২O(logn)O(logn) ////हդӢ੕हդӢ੕ܨগ߄ͧӮ࣐ளঃݤܨӮ࣐ளঃݤ 55 Քј͜Քј͜ ҪॹͺלޣӸࣩڡҪॹͺࠊࠢ૊ͧԮۨӞלޣӸࣩڡ55 ՔժՔժ ......ࠠૅՐҞࠠૅՐҞoo(n(n))ࠊࠢ૊ͧԮۨӞ ੹ͧ(n(n22))Оੌ޼১иҪॹࣩݤҚОੌ޼১иҪॹࣩݤҚࠢ૊ࠢ૊ݒҀݒҀފ੹ͧЬػފЬػ ٽܭиફࡆشݒҀૢૢٽܭиફࡆشચݿࣩͤҪॹशԇशԇݒҀૢૢޟࠕͧͣؗଢ଼ױચݿࣩͤҪॹؿॡޟࠕͧͣؗଢ଼ױ55 ؿॡ Н(n(n22))ڀڅ޳וНݜ㔀Ѽڀڅ޳וݜ㔀Ѽ remove()remove()ոոinsertBefore()insertBefore()֭Н֭НO(1)O(1) selectMax()selectMax()НН(n(nk)k) 55 Һ૨іҺ૨іnnࠊͧॕࠊͧॕkkࠊ૨іЗࠊ૨іЗ 㓬ৗ㓬ৗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 175 LL pp maxmax curcur p+np+n 226a6a 446b6b 301530156c6c 789789 226a6a 446b6b 30306c6c 1515789789 ॠࡄࣩऻؓ㓬ٽܭ܌૲قࠕͧՔҐ੩Іױॠࡄࣩऻؓ㓬ٽܭ܌૲قࠕͧՔҐ੩Іױ ””gege()()””ॗݝиॗݝи““դীѨҮդীѨҮ““ۋۋ””()Ҫॹݿͧଙ࣐ࠢ૊֗““!lt()!ltוଜޥҪॹݿͧଙ࣐ࠢ૊֧֗וଜޥ֧ 55 }} ৼ䎯Ѹ঴לޣৼ䎯Ѹ঴૞֚לޣreturnmax;returnmax;////૞֚ ڢҪॹѸ঴੢לޣݰޟӨڢҪॹѸ঴੢לޣݰޟmaxmax =cur;=cur;////Ө if(if(!!ltlt((cur((cur =cur=cur>>succsucc))>data,max>data,max>data))>data))////ਂਂ >=>= maxmax for(for(Posi(TPosi(T)cur=p;1::::selectMaxselectMax(Posi(T(Posi(T)p,intn){)p,intn){ ////(n)(n) ীלޣীЖҪॹЗ૲Ӟלޣ55 templateT> ////ёફ׶иѸ঴ёફ׶иѸ঴ppࣩࣩnnЖҪॹЗ૲Ӟ ؗࢻ͵ؗࢻ͵selectMax()selectMax() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 174 tail=Mtail=M ......MM LL pp p+np+n...... LL pp tailtail p+np+n }} }} tail=tailtail=tail>pred;n>pred;n;; ݰޟݰޟբࠖڅբࣩࠖਇ֟澝ؠڅԙ୎ࣩਇ֟澝ؠٽܭٽޥԙ୎澝ٽܭڬԙ୎ٽܭٽޥԙ୎澝ٽܭڬ//// ;(((insertBeforeinsertBefore(tail(tail,,removeremove((selectMaxselectMax(head(head>succ,n)));>succ,n ԙ୎ӸॏٽޥҾश২ذԙ୎ӸॏٽޥҾश২ذীͧלޣীͧלޣ////ۨӞۨӞ ԙ୎Ӄٽܭڬ૟ӽГЖৼ䎯УӸ֧ͧشԙ୎Ӄ֧২ٽܭڬ૟ӽГЖৼ䎯УӸ֧ͧشwhile(1succ;>succ; Posi(T)head=pPosi(T)head=p>pred;Posi(T)tail=p;>pred;Posi(T)tail=p; ঞ▁֩਽ठুܩҽͧՔफ़⫩ـ澝עޥঞ▁֩਽ठ֛੦ুܩҽͧՔफ़⫩ـ澝עޥԙ୎Н(head,tail)(head,tail)————֛੦ٽܭڬԙ୎Нٽܭڬ//// }(voidvoidListList::::selectionSortselectionSort(Posi(T)p,intn){(Posi(T)p,intn templatetemplate //valid(p)&&rank(p)+n<=size//valid(p)&&rank(p)+n<=size ٽܭٽܭ܌ЖҪॹҞ૲܌Ӧ਽Зફ׶иѸ঴ppࣩ૤ডࣩ૤ডnnЖҪॹҞ૲ت͵Ӧ਽Зફ׶иѸ঴ૹ࣐࢝ޯت͵////ૹ࣐࢝ޯ ؗࢻ͵ؗࢻ͵selectionSortselectionSort()() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 173 77 ^^ 12345671234567 66 ((11)) 234567234567 55 ((22)) 11 3456734567 44 22((33)) 11 45674567 33 22((44)) 3131 567567 22 ((55)) 24312431 6767 11 524524((66)) 3131 77 00 5252((77)) 46314631 ^^ Ӧٽ׾ٽޥӦդথٽ׾ٽޥӦ դথٽ׾ٽӦӸথݸٽ׾ٽ૨іૅࠊ૨іૅࠊ Ӹথݸ ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 172 swapswap M M S[r1,n) S[r,n) S[0,r1) S[0,r) ર䩚ܦર䩚рܦࠕդͧࠕդͧ▁ࠊ▁ࠊр ককO(n)O(n)ࠊࠢ૊ԮՔगؓࠊࠢ૊ԮՔगؓMM 55 ੌڶޥ࠻ੌڶޥ࠻ܦрܦO(n)O(n)ࠊࠊр O(n)O(n)ࠊࠊࠢ૊ࠢ૊ݸՔԻஂͧѷݸՔԻஂͧѷ 55 ܦࠊрܦO(n)O(n)ࠊࠢ૊ոࠊࠢ૊ոO(n)O(n)ࠊр ҪॹMMͧҞгͧҞгלޣ૲ࠠЖܙҪॹНלޣ૲ࠠЖܙН Н֛ފНݿ୎֛ͧފј୻ੌO(nO(n22))ݿ୎ͧےУٽܭј୻ੌફࡆےУٽܭ55 ફࡆ ͜Ј૚......ٽܭ܌͜Ј૚૲ٽܭ܌УػѸ ////૲҄ٶҪॹͧלޣУػѸӞ҄ٶҪॹͧלޣ૲Ӟܙ૲ܙҪॹЗٽܭҪॹЗёޭٽܭёޭ ࢐ػѸ ////Ҿݝ߈ЬॗݝиҾݝ߈Ьॗݝи......ڶҪॹלޣӸࣩڡͧܦ࢐ػѸࠠক▁年۟ܵрڶҪॹלޣӸࣩڡͧܦࠠক▁年۟ܵр 55 ...... ٽܭફࡆڷ֚ٽܭફࡆڷ֚ 55 ߄㓢߄㓢 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 171 ⽝ҨЬ૮гӘߞ޼澞 ЇՀ૲гӘߞ߈਒Љ⢎⽝૮Խͧ⢎ڡ ٽܭ܌૲ٽܭ܌(d)(d)૲ 3.3.Ӧ਽Ӧ਽ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 170 Нफ़Ԗלࣂדդড֧ͧ؁࣮и⫩ҽࣩڰНफ़Ԗלࣂדդড֧ͧ؁࣮и⫩ҽࣩڰ ॗդডݒҀॗդডݒҀٽܭҴܷٽܭࠕՔҍиܷҴױҪॹͧלޣࠕՔҍиࣩױҪॹͧלޣиeeࣩלи׶ও૞֚Јל55 ׶ও૞֚Ј ੧ୋ੧ୋٽஎٽ࣐எڕҪॹ୎ࣩܨ࣐੧ୋݸࡄளݝؗࢻͧুՐৗ҈ڕҪॹ୎ࣩܨ䰹ࣩ୩޲୩޲੧ୋݸࡄளݝؗࢻͧুՐৗ҈ܨ䰹ࣩ҈ܨ҈ ݸһٽݸһࠊٽҤ֩֬ЉҾ૽્૽્ࠊ؁Ҥ֩֬ЉҾ؁ࢢࣂࢢࣂͧړѸ঴੧ୋࣩݱڱ࢑ܘͧړѸ঴੧ୋࣩݱڱ࢑ܘ 55 ߠޯЈՔৗͺފ૟ͧڡ㓬ܶளߓۨݝࢴͺؗࢻЈٽޥߠޯЈՔৗͺНѽޭৗҘԈފ૟ͧڡ㓬ܶளߓۨݝࢴͺؗࢻЈٽޥНѽޭৗҘԈ 55 څࠔࠢиԙ୎ؠͧڅO(n)O(n)ͧࠔࠢиԙ୎ؠ֭ٴͶॗࠃࢴݿ֭ٴO(n)O(n)Ͷॗࠃࢴݿ֯ޣͧ֯ޣͧ(O(1)O(1װޣװޣ////{{ ਇ֟ભࣙդͧ૞֚ߓۨওࠓࣩѸ঴դͧ૞֚ߓۨওࠓࣩѸ঴ۋਇ֟ભࣙݤҚભࣙۋreturnp;returnp;////ࣻ২շЗ澝ࣻ২շЗ澝ݤҚભࣙ if(((p=pif(((p=p>pred)>pred)>data)<=e)break;>data)<=e)break;////૵Жࠢ૊૵Жࠢ૊ ૝ࣩnnЖӸ஫ͧё՗զٗЖӸ஫ͧё՗զٗޣ૝ࣩࣩޣиppࣩتиت////((while(0<=nwhile(0<=n Posi(TPosi(T))ListList::::searchsearch(T(T const&e,intn,const&e,intn,Posi(TPosi(T)p)const{)p)const{ 55 templateT> ߓۨߓۨ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 169 }} returnreturnoldSizeoldSize _size;_size; ////੐ࠆՉԖଞͧԮ੃ӬୣҪॹ㔀ݤ੐ࠆՉԖଞͧԮ੃ӬୣҪॹ㔀ݤ }} {{removeremove(q(q);q=p;});q=p;}////ӨӬୣդীӨӬୣդী if(pif(p>data==q>data==q>data)>data)////ਂ୹բਂ୹բ ॻ଍ৼ䎯تॻ଍ৼ䎯ࠠتPosi(TPosi(T)p=q;q=p)p=q;q=p>succ;>succ;////ࠠ while(trailer!=qwhile(trailer!=q>succ){>succ){////ՐੌՐੌqq՟ࡄ՟ࡄ Posi(TPosi(T)q=first();)q=first();////ёநৼ䎯ӞՆёநৼ䎯ӞՆ Լ੐ࠆڢԼ੐ࠆ੢ڢintintoldSizeoldSize =_size;=_size; ////੢ וәӦ਽১࢐ݸଜٴוәӦ਽১࢐ݸଜٴ////;if(_size<2)return0;if(_size<2)return0 ளޟҪॹͧݝࢴוୣଜ⣞ۦۉளޟҪॹͧݝࢴוୣଜ⣞ۦۉ////}()}()intintListList::::uniquifyuniquify 55 templateT> ։▁Ԗ։▁Ԗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 184 lo+n = hi lo+n/2 = lo mi ListList pp qq 11ͧݜ㔀Ѽ૜ਸ਼ݿ୎Нͧݜ㔀Ѽ૜ਸ਼ݿ୎НO(n+mO(n+m))ͧ঎㓬ࠔࠢиৼ䎯㔀ݤͧ঎㓬ࠔࠢиৼ䎯㔀ݤشӖڶشӖڶ}}////ࠠক૚▁ࠊ૨іࠠক૚▁ࠊ૨іn+mn+m {{insertBeforeinsertBefore(p(p,,L.L.removeremove((q((q =q=q>succ)>succ)>pred));m>pred));m;};} qqܷ২ܷ২ppУӸУӸذӨͧذv(qv(q)<)succ))break;n>succ))break;n;};}////Ө if((0data<=q>data<=q>data))>data))////ਂਂppѐ֧ԙ୎ӃЋѐ֧ԙ୎ӃЋv(pv(p)<=)<=v(qv(q)) शӞԙ୎УӸޭظशӞԙ୎УӸޭظwhile(0::::mergemerge(Posi(T(Posi(T)&p,intn,List&L,)&p,intn,List&L,Posi(TPosi(T)q,intm){)q,intm){ templateT>////O(n+mO(n+m)) ݿНբ▁Ӧ਽ͤٽܭٶڠͣٶڠݿНբ▁Ӧ਽ͤЖҪॹٽܭٶڠͣٶڠӸӦ਽З১ppફࣩફࣩnnЖҪॹЉӦ਽ЖҪॹЉӦ਽LLЗ১З১qqફࣩફࣩmmЖҪॹڡӸӦ਽З১ڡ//// ٶڠзષٶڠзષ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 183 lo+n = hi lo+n/2 = lo mi ListList pp qq Ө㔀Ѽ૜ਸ਼ݿ୎тНO(nlogn)O(nlogn)ͧۉ؎Ք֧঎㓬ݿ୎ӃٶڠӨ㔀Ѽ૜ਸ਼ݿ୎тНਂͧۉ؎Ք֧঎㓬ݿ୎Ӄٶڠਂ////{{ ٶڠٶڠ////;(mergemerge(p(p,m,*this,q,n,m,*this,q,n m);m ٽܭӦӢӯٽ׾ٽܭӦӢӯٽmergeSortmergeSort(p(p,m);,m);mergeSortmergeSort(q(q,n,nm);m);////׾ for(inti=0;isucc;>succ;////֭ӢӦ਽֭ӢӦ਽ Posi(TPosi(T)q=p;intm=n>>1;)q=p;intm=n>>1;////јЗ䎯НࣙјЗ䎯Нࣙ ૞֚ͧժӨ......ܯݿࣻسכਇ֟રٽܭڬ૞֚ͧժӨܯݿࣻسכਇ֟રٽܭڬ////;if(n<2)return;if(n<2)return voidvoidListList::::mergeSortmergeSort(Posi(T(Posi(T)&p,intn){)&p,intn){ templateT>////valid(pvalid(p)&&)&&rank(prank(p)+n<=size)+n<=size ٽܭЖҪॹٽܭફ׶иѸ঴ppࣩࣩnnЖҪॹتફ׶иѸ঴ت//// ٽܭٶڠٽܭٶڠ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 182 ௉ࣽУ՟ͧ挪ј嬩徠 橪ފЗͧ樁機ފ橤橰 ە֙䕄׿晍ͧҸ恉֧ ׭ͺޥҾފㆹ㖤ͧ⠹ל㓢ЫٍٕУԧ Уͺۃ՟⠹ͧロґҒুڶ㫗ГষҾ ٽܭٶڠٽܭٶڠ((f)(f 3.3.Ӧ਽Ӧ਽ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 181 S\Sr+1 S\Sr SSr+1r+1 SSrr ளͺܶ֩ڀளͺͧѷ㔀Ѽ㓬ৗѪժࣼܶ֩ڀ55 ݘ࣐զଞͧߓۨݝࢴՔѨԖ২ݘ࣐զଞͧߓۨݝࢴՔѨԖ২O(logkO(logk))ͧѷ㔀Ѽ㓬ৗѪժࣼ [0+1+...+(n[0+1+...+(n1)]/2+1=O(n1)]/2+1=O(n22)) 㔀Ѽݿ୎ࣩޫީҚНͧފ㔀Ѽݿ୎ࣩޫީҚНиͧފ55 и == r/2+1r/2+1 [r+(r[r+(r1)+...+3+2+1+0]/(r+1)+11)+...+3+2+1+0]/(r+1)+1 ৿ચݿ୎ࣩޫީҚଞНے৿ચݿ୎ࣩޫީҚଞНے[[ҴS[rS[rڕࣩૠࠊ૨іЗͧНۉ؎Ҵ֛ࠕ֧ͧөڕࣩૠࠊ૨іЗͧНۉ؎55 ֛ࠕ֧ͧө ՔৗͧЋࠃࢴ֭ॗи1/(r+1)1/(r+1)ޥՔৗͧЋࠃࢴ֭ॗиЖҪॹ଒ޥҾЗࣩr+1r+1ЖҪॹ଒͵اҾЗࣩ੏͵ا55 ੏ ԼҮࣩS[rS[r]]ͺͺފԼҮࣩЗͧփЖҪॹފࣩS[0,r]S[0,r]ЗͧփЖҪॹٽޥࣩੰୋ͵֧ࠕݿٜٽޥੰୋ͵֧ࠕݿٜ 55 িߓ͵িߓ͵S[rS[r]]өܷҴӰөܷҴӰS[0,r)S[0,r)դࣩଊ▁ݿӵդࣩଊ▁ݿӵ ࠊҪॹࠢ૊ͺشיҞੌ֭ٴࠊҪॹࠢ૊ͺਂ՞ҪॹࣩݤҚ֭ԓ澝ࢮैӢ١ͧشיҞੌ֭ٴ55 ਂ՞ҪॹࣩݤҚ֭ԓ澝ࢮैӢ١ͧ 㓬ৗ֭ٴ㓬ৗ֭ٴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 180 иppלи澝سиЗࣩҪॹ֭Дߡלи澝سSSrrЗࣩҪॹ֭Дߡ 澝ЈլЉppࣼॗࣩҪॹࣼॗࣩҪॹޥ澝ЈլЉЗլޥSSrrЗլ ய੩՞म㖊ӐЇࣩࠔग㓬ͧѼѪ⫩ҽৼ䎯ࣩҀ࣐͵ய੩՞म㖊ӐЇࣩࠔग㓬ͧѼѪ⫩ҽৼ䎯ࣩҀ࣐͵ 55 ٽޥܕҐފӸৼ䎯ͧ㔀ڡՌ૞ࣩ֚Ѹ঴УդܷҴܯٽޥܕҐފӸৼ䎯ͧ㔀ڡՌ૞ࣩ֚Ѹ঴УդܷҴܯ()55 ॻ଍иॻ଍иsearch()search иػ֩ॠࡄيиػ֩ॠࡄોԈी୎ͧݜي}} ////ୣ૎ҴӦ਽ͧэ࣐҄ୣ૎ҴӦ਽ͧэ࣐҄O(1)O(1)ોԈी୎ͧݜ }} //n//nࠊ૨іͧࠠࠊࠊ૨іͧࠠࠊO(rO(r +1)+1) p=pp=p>succ;>succ;removeremove(p(p>pred);>pred);////ૃզЇ▁ৼ䎯ૃզЇ▁ৼ䎯 insertAfterinsertAfter((searchsearch(p(p>data,>data,rr,p),p,p),p>data);>data); ////ߓۨߓۨ++ܷҴܷҴ ӰSSr+1r+1ڰӰڰҴ՞ৼ䎯࣒ͧSSrrڕҴ՞ৼ䎯࣒ͧڕ▁for(intfor(intrr =0;=0;rr ::::insertionSortinsertionSort(ListNodePosi(T(ListNodePosi(T)p,intn){)p,intn){ templateT> ////valid(pvalid(p)&&)&&rank(prank(p)+n<=size)+n<=size ٽܭЖҪॹҞܷҴٽܭӦ਽Зફ׶иѸ঴ppࣩ૤ডࣩ૤ডnnЖҪॹҞܷҴت͵Ӧ਽Зફ׶иѸ঴ૹ࣐࢝ޯت͵////ૹ࣐࢝ޯ ؗࢻؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 179 77 ((11)) 22 33 44 55 66 77 ^^ ^^ 66 22 ((33)) 44 55 66 77 11 ^^ 55 22 44 55 ((66)) 77 33 11 44 22 ((44)) 55 77 66 33 11 33 22 55 ((77)) 44 66 33 11 22 ((22)) 55 77 44 66 33 11 11 ((55)) 22 77 44 66 33 11 00 ^^ 55 22 77 44 66 33 11 11 ^^ ^^ 55 22 77 44 66 33 11 Ӧٽ׾ٽӦդথݸٽ׾ٽҪॹҪॹ դথݸ ӸڡӸڡ Ӧٽ׾ٽޥӦӸথٽ׾ٽޥૅࠊૅࠊ Ӹথ ૨і૨і ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 178 e e S[r+1,n) S[r,n) S[0,r+1) S[0,r) ӦٽٽޥࣩۉӦЖҪॹঐٽٽޥࣩۉSSrr ==ӸӸrrЖҪॹঐ  00<=<=rr<=<=nnͧͧ 55 ࠔग㓬ׁијЇЈՉ㓬͵ࠔग㓬ׁијЇЈՉ㓬͵ ӦٽٽܭӦԮٽٽܭওͧSSnn =S[0,n)=S[0,n)Ԯޣওͧޣ SS00,S,S11,,……,S,Snn Ӱ͵ڰࠕͧՔ૵ࠖױ͵ӰڰࠕͧՔ૵ࠖױ 55 ӦࣩܷҴٽٽޥӦࣩܷҴٽٽޥ//// [ࣩSSr+1r+1 =S[0,r]=S[0,rٽޥӰڰࣩͧٽޥӰڰͧ[[ܷҴܷҴS[rS[r ӦࣩߓۨٽٽޥӦࣩߓۨٽٽޥ//// Ѹ঴ڡѸ঴Зगؓ૯ڡࣩSSrr =S[0,r)=S[0,r)Зगؓ૯ٽޥࣩ֧ٽޥ֧ 澬澬૨і૨і澭澭 55 ٽޥӦ১࢐ٽीٽޥӦ১࢐ٽӦ ////ीٽН୸ࣩͤीڅӦНͣେٽН୸ࣩͤीڅ55 澬澬ӫ׶Ԗӫ׶Ԗ澭澭঴঴SS00Нͣେ S=Srr +S+S\\SSrr //|S//|Srr|=r|=r= ٽܭޭٽܭޭ++ ٽܭГ଑Ӣ͵ٜۉ૎ҴऀذٽܭГ଑Ӣ͵ٜۉ૎Ҵऀذ 55 ߄㓢߄㓢 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 177 ஞஷੁӃ澞 ॏࠔͧӆܷݚڰ޼ݦ஑ॢڔ٪ѺͧՇ ъ澞ȑ۬ߋڟޥڶ৿哛ҍଂ͵ȐӸ஄ ٽܭҴܷٽܭ(e)(e)ܷҴ 3.3.Ӧ਽Ӧ਽ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 192 }} /*......*//*......*/ }} A[rA[r]=obj;n++;returnobj;]=obj;n++;returnobj; for(inti=n;i>r;ifor(inti=n;i>r;i))A[iA[i]=A[i]=A[i1];1]; if(n>=N)if(n>=N) thrownewthrownewExceptionBoundaryViolationExceptionBoundaryViolation("("ࡽӞࡽӞ");"); thrownewthrownewExceptionBoundaryViolationExceptionBoundaryViolation("("ભࣙભࣙ");"); if(0>r||r>n)if(0>r||r>n) throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation {{ publicObjectpublicObjectinsertAtRank(intinsertAtRank(int r,Objectobj)r,Objectobj) publicbooleanisEmpty(){return0==n;}publicbooleanisEmpty(){return0==n;} publicintpublicintgetSizegetSize(){returnn;}(){returnn;} publicpublicVector_ArrayVector_Array(){A=new(){A=newObject[NObject[N];n=0;}];n=0;} privateObject[]A;privateintn=0;privateObject[]A;privateintn=0; ଞ֠ؓ؟ଞ֠ؓݤঐ؟privatefinalintN=1024;privatefinalintN=1024;////ݤঐ publicclasspublicclassVector_ArrayVector_Array implementsVectorimplementsVector {{ զଞؗࢻզଞؗࢻ11͵͵Vector_Array.javaVector_Array.java DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 191 }} throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation;; publicObjectpublicObjectremoveAtRank(intremoveAtRank(int r)r) throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation;; publicObjectpublicObjectinsertAtRank(intinsertAtRank(int r,Objectobj)r,Objectobj) throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation;; publicObjectpublicObjectreplaceAtRank(intreplaceAtRank(int r,Objectobj)r,Objectobj) throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation;; publicObjectpublicObjectgetAtRank(intgetAtRank(int r)r) publicbooleanisEmpty();publicbooleanisEmpty(); publicintpublicintgetSizegetSize();(); publicpublicinterfaceinterface VectorVector {{ Ռ͵Vector.javaVector.javaܯՌ͵զଞܯզଞ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 190 }} }} returndx*returndx*dxdx +dy*+dy*dydy r;ifor(inti=n;i>r;i))A[iA[i]=A[i]=A[i1];1];////դডҪॹஎࠊդशդডҪॹஎࠊդश }} A[]A[]ܦޢܦޢ[]for(inti=0;ir||r>n)if(0>r||r>n) throwsthrowsExceptionBoundaryViolationExceptionBoundaryViolation {{ publicObjectpublicObjectinsertAtRank(intinsertAtRank(int r,Objectobj)r,Objectobj) /*......*//*......*/ ଞͧͧՔЈݮ׌ԅՔЈݮ׌ԅ؟ଞӫ׶؟privateintNprivateintN == 8;8; ////ݤঐࣩݤঐࣩӫ׶ publicclasspublicclassVector_ExtArrayVector_ExtArray implementsVectorimplementsVector {{ զଞؗࢻզଞؗࢻ22͵͵Vector_ExtArray.javaVector_ExtArray.java DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 208 hailstone(94)hailstone(94) ............ odd(31)odd(31) hailstone(31)hailstone(31) even(62)even(62) hailstone(62)hailstone(62) even(124)even(124) hailstone(124)hailstone(124) odd(41)odd(41) hailstone(41)hailstone(41) even(82)even(82) hailstone(82)hailstone(82) odd(27)odd(27) hailstone(27)hailstone(27) callstack main(2,27)main(2,27) hailstone(1)hailstone(1) even(2)even(2) hailstone(2)hailstone(2) even(4)even(4) hailstone(4)hailstone(4) even(8)even(8) hailstone(8)hailstone(8) even(16)even(16) hailstone(16)hailstone(16) odd(5)odd(5) hailstone(5)hailstone(5) even(10)even(10) hailstone(10)hailstone(10) callstack main(2,10)main(2,10) {hailstone(atoi(argv[1]));}{hailstone(atoi(argv[1]));} 55 main(intmain(int argcargc,char*,char*argvargv[])[]) {hailstone(3*n+1);}{hailstone(3*n+1);} odd(intodd(int n)n) {{hailstone(nhailstone(n /2);}/2);} 55 even(inteven(int n)n) }} n%2?n%2?odd(nodd(n):):even(neven(n);); if(2>n)return;if(2>n)return; 55 hailstone(inthailstone(int n){n){ ؗ҅͵ؗ҅͵hailstone()hailstone() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 207 xx main(2,3)main(2,3) 1+x1+x fib(3)fib(3) 22 main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) 11 fib(1)fib(1) 1+11+1 fib(3)fib(3) 1+x1+x fib(3)fib(3) x+xx+x fib(3)fib(3) x+xx+x fib(3)fib(3) 1+01+0 fib(2)fib(2) 00 fib(0)fib(0) 1+x1+x fib(2)fib(2) 1+x1+x fib(2)fib(2) x+xx+x fib(3)fib(3) x+xx+x fib(2)fib(2) x+xx+x fib(3)fib(3) 11 fib(1)fib(1) x+xx+x fib(2)fib(2) x+xx+x fib(3)fib(3) x+xx+x fib(3)fib(3) main(intmain(int argcargc,char*,char*argvargv[]){fib(atoi(argv[1]));}[]){fib(atoi(argv[1]));} 55 intfib(intn)intfib(intn) {return(2>n)?{return(2>n)? nn :fib(n:fib(n1)+fib(n1)+fib(n2);2); }} ؗ҅͵ؗ҅͵fib()fib() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 206 66 main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) xx main(2,3)main(2,3) 3*23*2 fac(3)fac(3) 2*12*1 fac(2)fac(2) 3*x3*x fac(3)fac(3) 11 fac(1)fac(1) 2*x2*x fac(2)fac(2) 3*x3*x fac(3)fac(3) 2*x2*x fac(2)fac(2) 3*x3*x fac(3)fac(3) 3*x3*x fac(3)fac(3) xx main(2,3)main(2,3) main(intmain(int argcargc,char*,char*argvargv[]){[]){ fac(fac(atoi(argv[1]))atoi(argv[1]));};} 55 intintffacac(int(int n)n) {return(2>n)?{return(2>n)? 11 ::n*n*ffacac(n(n1);1); }} ؗ҅͵ؗ҅͵fac()fac() DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 205 } /*...*/ /*...*/ inti=m/4; } /*...*/ /*...*/ intk=j/3; } /*...*/ /*...*/ inti=9; callstack binaryexecutable m=24,i=6,... previousframe=8630 returnaddress=1820 funcB()funcB() m=32,i=8,... previousframe=8850 returnaddress=1500 j=81,k=27,... i=9,... previousframe=9000 returnaddress=1000 main()main() xxxx1000:funcA(i *i); funcA()funcA() funcB()funcB() xxxx1500:funcB(k +5); xxxx1820:funcB(m i); xxxx1750:voidfuncB(int m){ xxxx1200:voidfuncA(int j){ xxxx0700:main(){ ****9000 ****8850 ****8480 ****8630 Ӡݤઁ࣐ߘӠݤઁ࣐ߘ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 204 ڠߘЉ૶ڠ(b)(b)ߘЉ૶ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 203 ѽͺױՌͧݝࢴܯؗࢻࣩߘےѽͺ੪࡚͵ҁױՌͧݝࢴܯؗࢻࣩߘے੪࡚͵ҁ 55 ੿դ͵ׁи੿դ͵ׁиӦ਽Ӧ਽ͧࡖ࣏ؓТߘࠆ߂६ͧࡖ࣏ؓТߘࠆ߂६ Ռ֭ͧՐ୻O(1)O(1)ݿ୎ݿ୎ܯࠕؗࢻࣩߘױ͵Ռ֭ͧՐ୻गੜܯࠕؗࢻࣩߘױ͵55 गੜ ஌஌ ———— ஜҕ૚޼⩲ͺஜҕ૚޼⩲ͺ//ځॏНߘޮځ}};;////јզଞநјզଞந//ޮॏНߘ T&top(){return(*this)[size()T&top(){return(*this)[size()1];}1];}////Շ஌Շ஌ Tpop(){returnTpop(){returnremoveremove(size(size()() 1);}1);}////ӞߘӞߘ voidvoidpush(Tpush(T const&e){const&e){insertinsert(size(size(),e);}(),e);}////ҴߘҴߘ ࡀ࣐ܯՌ֭Քࣻܯݚڏࡀ࣐јՂҾ؈ܯՌ֭Քࣻܯݚڏpublic:public: //size()//size()澝澝empty()empty()јՂҾ؈ classclassStackStack::publicpublic VectorVector{{////࣒զଞࡖ࣏ࣩߘࠆ߂६࣒զଞࡖ࣏ࣩߘࠆ߂६ 55 templateT> Ӧ਽ࡖ࣏ۋиզଞׁܯӦࣩࢤӦͧݜՔࣻٽиيӦ਽ࡖ࣏ߘݹ࢐ۋиզଞׁܯӦࣩࢤӦͧݜՔࣻٽиي55 ߘݹ࢐ ࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 202 empty()empty() falsefalse 33 77 55 size()size() 44 1111 33 77 55 top()top() 33 33 77 55 top()top() 1111 1111 33 77 55 push(3)push(3) 33 77 55 pop()pop() 66 1111 33 77 55 push(7)push(7) 77 55 pop()pop() 77 66 1111 33 77 55 pop()pop() 33 55 push(7)push(7) 77 66 1111 33 77 55 push(3)push(3) 33 55 empty()empty() falsefalse 66 1111 33 77 55 push(5)push(5) 55 push(6)push(6) 66 1111 33 77 55 empty()empty() truetrue size()size() 44 1111 33 77 55 Stack()Stack() push(11)push(11) 1111 33 77 55 ݒҀݒҀ ૎Ӟ૎Ӟ ߘͣٗҊНߘ஌ͤߘͣٗҊНߘ஌ͤ ݒҀݒҀ ૎Ӟ૎Ӟ ߘͣٗҊНߘ஌ͤߘͣٗҊНߘ஌ͤ ݒҀؗ҅ݒҀؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 201 push poptop Ռ͵getMax()...getMax()...ܯى۞͵Ռܯى۞ 55 ҮૡդӞͣҮૡդӞͣFILOFILOͤͤ 55 դૡҮӞͣդૡҮӞͣLIFOLIFOͤͤ top()top() ߓ஌ߓ஌ pop()pop() ӞߘӞߘ push()push() ҴߘҴߘ size()/empty()size()/empty() ՌܯՌׁޯܯޯׁ 55 bottombottomͤНࣺॏͤНࣺॏͣځߘͣځߘ Րৗ֧ߘ஌ͣՐৗ֧ߘ஌ͣtoptopܷͤҴոӬୣܷͤҴոӬୣ ӦٽӦࣩٽՈୡՈୡࣩފͤފ55 ߘߘͣͣstackstackͤ ՌܯՌݒҀЉܯݒҀЉ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 216 ՙԘକ܍͵࣐ڀՙԘକߘ܍͵࣐ڀ(c2)(c2)ߘ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 215 }} ૎Ӟٽ૱૎Ӟٽ૱////;((();((()while(!while(!S.S.emptyempty())())printf("%cprintf("%c",(S.",(S.poppop Ӱࣩ՞ݤѸڰܦૃڢӰࣩ՞ݤѸ࣐ߘ੢ڰܦૃڢStackStackS;S;convertconvert(S(S,n,base);,n,base);////࣐ߘ੢ 55 main(){main(){ }} }} basebaseࣩୣ֋ࣩୣ֋تݰНҾޟتݰНҾޟn/=base;n/=base;////nn ࣩݤѸͤҴߘڀتࣩݤѸͤҴߘڀتS.S.pushpush(digit[n(digit[n %base]);%base]);////ѾݤѾݤͣͣ while(n>0){while(n>0){////࣒ѹӰளͧ૵▁ਗ਼ॠӞݰૡӱЇࣩ՞ݤѸ࣒ѹӰளͧ૵▁ਗ਼ॠӞݰૡӱЇࣩ՞ݤѸ Ҭ۞ڡҬՇҚਇ֟૯۞ڡ};}; ////ՔੑՔੑbasebaseՇҚਇ֟૯ '8','9','A','B','C','D','E','F''8','9','A','B','C','D','E','F' '0','1','2','3','4','5','6','7','0','1','2','3','4','5','6','7', staticchardigit[]={staticchardigit[]={////ݰૡӱЇࣩݤѸ॔ՙݰૡӱЇࣩݤѸ॔ՙ 55 voidvoidconvertconvert((StackStack&S,__int64n,intbase){&S,__int64n,intbase){ ॠࡄؗࢻॠࡄؗࢻ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 214 // %% dd22 nn22 %% ddmm %% //nnmm=d=dmm ddmm11 //nnmm11 %% dd11 %% //nn11 dd00 nn00=n=n ޯۉݝܰӱਗ਼ॠޥ࣐ߘͧԮՔࢁરјІੌ࠭ͧтՔ҄ޯۉݝܰӱਗ਼ॠޥ55 ࣐҄ߘͧԮՔࢁરјІੌ࠭ͧтՔ Ռ֭੃୍঴ܯݤיՌ֭੃୍঴ਂ࣐҄Ӧ਽ͧӨܯݤי࣐Ӧ਽ͧӨ҄ਂ ڡڰஏڶ࣐զଞͧӨ۞Ҭज़ࣛ҄ਂڡڰஏڶ55 ਂ࣐҄զଞͧӨ۞Ҭज़ࣛ ѽ১ளӰѹ১ளӰѹ૎Ӟͺ૎ӞͺױӰࣩݤѸͧڰѽױӰࣩݤѸͧڰ১ѹӰள১ѹӰள ࣩmmͧբݿी୎ЬЈ࡟ચͺͧբݿी୎ЬЈ࡟ચͺלכરܕѽݖױࣩלכરܕѽݖױ গ߈ͺҿѼ֩ܦ૎Ӟૃٶڢѽࠔग੢ױЈगؓͧٶগ߈ͺҿѼ֩ܦ૎Ӟૃٶڢѽࠔग੢ױЈगؓͧٶ55 ѸݤѸݤmm ୰䎯Ղ੕ӏݱࡄ୰䎯Ղ੕ӏݱࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 213 // %% dd22 nn22 %% ddmm %% //nnmm=d=dmm ddmm11 //nnmm11 %% dd11 %% //nn11 dd00 nn00=n=n Ӟૡӱࣩ՞Ѹૡӱࣩ՞ѸڰӞڰՇࠆ澝ݦୣͧԮՔ১ѹӰள১ѹӰளוՇࠆ澝ݦୣͧԮՔՅוՅتت55 ߄㓢͵߄㓢͵nn nni+1i+1 =n=nii // ոո ddii =n=nii %% ////ӫ׶ӫ׶nn00 =n=n ͵ޥӨ͵ޥ55 Ө ї͵ї͵ nnii =(d=(dmm...d...di+1i+1ddii)) ==ddmmmm +...+d+...+d1111 +d+d0000 55 ੦͵੦͵ nn =(d=(dmm...d...d22dd11dd00)) 㓢ષ㓢ષ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 212 Ԯҍ૕େНԮҍ૕େН1km1kmͧ੷ٚтЈરͧ੷ٚтЈર1mm1mm ||11oo24'51''10'''24'51''10''' sqrt(2)|<0.0000006=0.6x10sqrt(2)|<0.0000006=0.6x1066 55 ੷ٚ੷ٚ == 1.414212961.41421296296296296296296296...... == 11 ++2424/60+/60+5151/60^2+/60^2+1010/60^3/60^3 11oo2424''5151''''1010'''''' ੔঎تࣩڤ੔঎େݱتࣩڤ55 େݱ BabylonicBabylonic cuneiformcuneiformͤЗࣩͤЗࣩ6060ૡӱૡӱ......ͣ؀ݧڤٝࠢѯ߿ͣ؀ݧڤ55 ٝࠢѯ߿ printf("%dprintf("%d |%I64d|%b|%o|%x",n);|%I64d|%b|%o|%x",n); (8)(8)30071=30071= (10)(10)1234512345 ړڤૡӱ਽ठړڤНૡӱ਽ठܦҾૃذНૡӱஂ઎ݦݤͧܦҾૃذ55 ܵ૩͵ܵ૩͵ চؓџ▁চؓџ▁1010ૡӱஂ઎ݦݤͧ ୋஙୋங DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 211 ړиߘগ߄ࣩࢤؓਗ਼ॠࠆׁړׁиߘগ߄ࣩࢤؓਗ਼ॠࠆ ॠࡄ RPNRPNړॠࡄߘړ55 ߘ ࣂࣩӸথדେУդͧݱৗगؓՔכЗ֧ͧஓ੾રړࣂࣩӸথ঎㓬۟ܵॠࡄࠆדେУդͧݱৗगؓՔכЗ֧ͧஓ੾રړ঎㓬۟ܵॠࡄࠆ ૥নӎ evaluationevaluationڌ૥নӎڌ 55 څ޳וݝܰӱࠕ६ॠࡄࣩޥ׫㓬ͧՔ࣐јٓڠ૶ޥ࢐ҿםߘݒҀڅ޳וݝܰӱࠕ६ॠࡄࣩޥ׫㓬ͧՔ࣐јٓڠ૶ޥ࢐ҿםߘݒҀ Ј֠ؓڅЈ֠ؓѷӢݖѸ঴ոٓ׫࡬څ૩ͧ ѷӢݖѸ঴ոٓ׫࡬ܵڠ১ࣼѶ㓬ࣩୋஙՔ૶ޥ૩ͧҿܵڠ১ࣼѶ㓬ࣩୋஙՔ૶ޥҿ ׫ permutation+parenthesispermutation+parenthesisٓڠ׫૶ٓڠ55 ૶ ஓऍބЈڅո૎Ӟେڅ࡬ڠஓऍͧ૶ބЈڅո૎Ӟେڅ࡬ڠࣂ૚सஜҕஜҕͧ૶דЉٽࣂ૚स૎ӞࠊדЉٽ૎Ӟࠊ ૎Ӟ conversionconversionٽ૱૎Ӟٽ૱ 55 ࣐֪՟ڀ࣐֪՟јЇݤৼͧӢӯগ՟ؗଢ଼ୋஙяঔߘࣩӘमӀֹڀ55 јЇݤৼͧӢӯগ՟ؗଢ଼ୋஙяঔߘࣩӘमӀֹ ࣐֪՟ڀ࣐֪՟ӀֹڀӀֹ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 210 NurseryRhymeMedley Themouseranuptheclock Hickory,Dickory,Dock ܦ࣐͵ૡӱૃڀߘܦ࣐͵ૡӱૃڀ(c1)(c1)ߘ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 209 ঢ۵▁Жߘঢ۵▁Жߘ ////ॕл्ॕл्֩ړދ֩ړދ୻ੌکک޳ࣩॠࡄͧҾ૨і࢝ޯוНޟ୻ੌکک޳ࣩॠࡄͧҾ૨і࢝ޯוНޟ 55 {while(11)f*=n{intf=1;while(n>1)f*=n;returnf;};returnf;} 55 intintfac(intfac(int n)n) ॠࡄݘӉН૨і࢝ޯڠ૶ذॠࡄݘӉН૨і࢝ޯڠ૶ذ ͵55 ݱࡄ͵ݱࡄ ࣂݿ୎דࣩטࣂݿ୎ঢ۵ઁ࣐ߘͧЬ୻৿ચ஛דࣩטঢ۵ઁ࣐ߘͧЬ୻৿ચ஛֩ړ୪֩ړНгНг୪ څ࡬ڠ૶לޣڅ࡬ڠ૶לޣОੌՇӏиͧڅ޳וӠݤࣩी୎ڠОੌՇӏи૶ͧڅ޳וӠݤࣩी୎ڠԇ޲͵ԇ޲͵ ૶ 55 ڠଈұ૶ڠଈұ૶ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 224 SSLL SR SSLL (( SR) ՙ܍ՙؗଢ଼ІͧߘЗՐ୻ͣՐৗͤҐࣚٗ܍55 ؗଢ଼ІͧߘЗՐ୻ͣՐৗͤҐࣚٗ টড۟ܵٶটড۟ܵͧٶͧ")"")"Ӟߘ஌ࣩڞӞߘ஌ࣩͧӨڞ55 ਂ۟ܵ২ਂ۟ܵ২")"")"ݿߘӃНݿߘӃНSSLL +"("+"("ͧӨ ٜࣩ۟ܵ଑Ӣڢ࣐ߘ੢ͧړ਽૖ܵ۟ٽٜࣩ۟ܵ଑Ӣஎڢ࣐ߘ੢ͧړ਽૖ܵ۟ٽ55 எ ডૡਸ਼ͺܕјڰѽ҄ୋஙࣩૠमୠ੕ױՙͺՀͧ܍تѽۨӰૠױডૡਸ਼ͺଊСͧܕјڰѽ҄ୋஙࣩૠमୠ੕ױՙͺՀͧ܍تѽۨӰૠױ55 ଊСͧ SSLL +S+SRRԘକԘକڡЋэڡڡЋэڡтԮ͵тԮ͵SSLL +"()"+S+"()"+SRRԘକͧԘକͧ ࣩԘକӭݮفտҵڦՙͧЈ܍ࣩԘକӭݮࣩٗ՗فտҵڦՙͧЈ܍ॻ଍ॻ଍ࣩٗ՗ت▁ஜҕјІ㓢ષՔऍ͵ࡤԽت▁55 ஜҕјІ㓢ષՔऍ͵ࡤԽ ՙ܍ՙ՗܍Нٗ//՗ܦޢНٗܦޢӦЗࣩpush/poppush/popٽ࡭ࡏݒҀذӦЗࣩНऀӞૠ▁䎯ͧՐ୻ٽ࡭ࡏݒҀذНऀӞૠ▁䎯ͧՐ୻ ՙࣩԘକ܍تՙࣩԘକ܍ت55 nnЖҪॹࣩߘ࡭ࡏͧॗўиЖҪॹࣩߘ࡭ࡏͧॗўиnn ՙԘକ͵߄㓢܍ՙԘକ͵߄㓢܍ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 223 Յ҅͵Յ҅͵"(()())()""(()())()" ="("+"()())("+")"="(()"="("+"()())("+")"="(()" +"())()"+"())()" ஏҘԈиҬӢ㓬ڶݝୠ੕ͧޥஏҘԈиҬӢ㓬ুН҄ୋஙڶݝୠ੕ͧޥুН҄ୋங 㓬ੌڶ㓬эНੌڶ55 1)1)ոո2)2)эН ؗࢻ ////ߠࡼ֧иߠࡼ֧и......ڠӰளݝࣩ૶ڰބјІܵ૩ͧЈܨؗࢻ࢐ুͧߠڠӰளݝࣩ૶ڰބјІܵ૩ͧЈܨ55 ࢐ুͧߠ E+FE+FԘକԘକڡԘକͧэ֭ڡ2)2) EEոոFF֭Ԙକͧэ E+")""("+E+")"ԘକԘକ+")"ڡԘକͧэڡ1)1) EEԘକͧэ Ԙକࣩފړՙࣩ਽૖܍Ԙକࣩݸފړՙࣩ਽૖܍૩͵ 0)0) ݸܵڠ૩͵૶ܵڠ55 ૶ ࣛھޗՙͧҾ؈॔ՙՔ܍ୣ՞म͵اࣛ੏ھޗՙͧҾ؈॔ՙՔ܍ୣ՞म͵ا55 ੏ (a[i(a[i1][j+1]+a[i+1][j1][j+1]+a[i+1][j1])*21])*2ԘକԘକ ([][][][])([][][][]) 55 (a[i(a[i1][j+1]1][j+1]))+a[i+1][j+a[i+1][j1])*21])*2ЈԘକЈԘକ ([][]([][]))[][])[][]) ՙԘକ܍ՙԘକ܍ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 222 44 33 22 11 44 33 22 33 22 3344 22 44 44 3344 33 44 33 22 44 33 22 11 1popped (8) 4popped (7) 4pushed (6) 2popped (5) 3popped (4) 3pushed (3) 2pushed (2) 1pushed (1) init (0) 11 44 11 11 22 11 33 22 11 22 11 11 иpoppopͶՅУͶՅУт࢐т࢐شиЈشլpushpushЈےӦЗͧџ▁Ӹথٽլߘ࡭ࡏࣩݒҀےӦЗͧџ▁Ӹথٽ55 ߘ࡭ࡏࣩݒҀ Ӧ͵{1,2,3,4}/{3,2,4,1}{1,2,3,4}/{3,2,4,1}ͶՅУͶՅУЈ࢐Ј࢐ٽӦ͵૎Ӟٽи૎Ҵ//૎ӞڀتӦٽи૎ҴڀتӦٽ55 push/poppush/pop pop(4)pop(1)pop(4)pop(1) pop(3)pop(2)pop(3)pop(2) push(4)push(4) push(1)push(2)push(3)push(1)push(2)push(3) ӦٽࣩۉӦݒҀ߄ٽࣩۉиߘSSࣩࣩnnࠊࠊpushpushЉЉnnࠊࠊpoppopݒҀ߄ڀتࠠ▁ߘ࡭ࡏͧ଒͵اиߘ੏ڀتࠠ▁ߘ࡭ࡏͧ଒͵ا55 ੏ ߘ࡭ࡏ͵䞻ӯߘ࡭ࡏ͵䞻ӯ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 221 ӞࣩҪॹ֧SSЗԯஂ஌ҪॹЗԯஂ஌Ҫॹڞ୻ۋӞࣩҪॹ֧ٜीͧڞ୻ۋժSSٜीͧފժУӸ࡚ͧ߳ފࠠࠊࠠࠊS.pop()S.pop()УӸ࡚ͧ߳ ࡭ࡏ૚स ////НѽՔਸ਼ͺНѽՔਸ਼ͺ܈࡭ࡏ૚सࠆ܈ҘԈߘAA澝澝BBոոSSࠆܯҘԈߘॠࡄ͵ࣻܯ55 O(O(nn))ॠࡄ͵ࣻ ЖO(n^O(n^22))ࣩ䞻ӯॠࡄࣩ䞻ӯॠࡄ ////ӆૡ▁ࠖ֩ӆૡ▁ࠖ֩......▁ڰࠕͧՔױЖ▁ڰࠕͧՔױ 55 j+1j+1,...,,...,ii,...,,...,jj,...},...},...},...}ړЈլࠆͧړi0123add4div5GS>0123add4div5mulmul 6767mulmul add8sub9add=add8sub9add= 55 PostScriptPostScript 55 \\>>set/aset/a 0+(1+23)/4*5*670+(1+23)/4*5*67 8+98+9 55 $$echo$((echo$(( 0+(1+23)/4*5*670+(1+23)/4*5*67 8+98+9)))) ࣩݤҚڀتࣩݤҚͧਗ਼ॠЉУڀتSSͧਗ਼ॠЉУړ਽૖ޱࣩॠړ਽૖ޱ55 চؓচؓ੶ࡄ੶ࡄࠔगࠔगࣩॠ ࠭Қړ࠭Қ਽૖ړ਽૖ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 228 ࠭Қړ࣐͵Зথ਽૖ڀ࠭Қߘړ࣐͵Зথ਽૖ڀ(c3)(c3)ߘ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 227 Тѽ֧ͺڿࣩ֧؁Тѽ֧ͺࣩԄৗէͺդীڿࣩ֧؁ؗࢻॠࡄWWࣩԄৗէͺդীܯՔјࣻފؗࢻॠࡄͧЈܯՔјࣻފ55 ࣒ॠࡄ࣒ॠࡄUUͧЈ D.B.Arnold&M.R.Sleep,1980D.B.Arnold&M.R.Sleep,1980 ACPACPv4v4f4f4p15,Algorithmp15,AlgorithmWW Зͧॗࠃࢴॗࠃࢴ֩୩޲џ૲Ҿ▁֩୩޲џ૲Ҿ▁ړ਽૖ޥےࣩۉՙঐ܍ԘକتЗͧړ਽૖ޥےࣩۉՙঐ܍Ԙକت55 ֧࣒֧࣒nn F.F.RuskeyRuskey,1978,1978 ACPACPv4v4f4f4p14,Algorithmp14,AlgorithmUU ՇӞॕॕNNЖЖٽӀ؀ܘЗͧړ਽૖ޥےࣩۉՙঐ܍ԘକتՇӞٽӀ؀ܘЗͧړ਽૖ޥےࣩۉՙঐ܍Ԙକت55 ֧࣒֧࣒nn I.I.SembaSemba,1981,1981 ACPACPv4v4f4f4p5,Algorithmp5,AlgorithmPP ړ਽૖ޥےࣩۉՙঐ܍Ԙକتړ਽૖ޥےࣩۉՙঐ܍Ԙକت߇П߇П࣒࣒nnͧٽӀ؀ܘͧٽӀ؀ܘ 55 ى܃͵ՙԘକ܍ى܃͵ՙԘକ܍ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 11 226 {{ }} [[ ]] (( )) {{ }} (( {{)) {{ (( (( [[ [[ (( (( {{ {{{{ [[ {{ (( [[ {{ (( [[ {{ [[ {{ {{ {{ {{
      +
    +
澝澝
  • +
  • +
    +/center>
    +/center>澝澝

    +

    +

    body>++澝澝

    +

    +> ͵ڹߗ͵ڹHTMLHTMLݧѝЗࣩݧѝЗࣩtagtagߗͧױࠢͧױࠢ ՙࣩ६ֹݤࣹ܍жҮ֠ؓڶЈুͧړՙࣩ६ֹݤࣹࣩૹ࣐ߡ܍жҮ֠ؓڶЈুͧړՙ””ࣩૹ࣐ߡ܍ՙ܍““55 ࣎২ͧՐ୻ংؓ࣎২ͧՐ୻ংؓ ࣩ㖊Ӑ؁ٶՙ܍मי২ٹܱ֩ܩࣩ㖊ӐјІ㓢ષՂॠࡄͧՔҍ؁ٶՙ܍मי২ٹܱ֩ܩјІ㓢ષՂॠࡄͧՔҍ 55 ى܃͵ՙԘକ܍ى܃͵ՙԘକ܍ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 225 (( (()) (( (( )) (( )) (( )) (( (()) (( (( (( (( (( (( (( (( (((( (( (( (( (( (( (( (( }} ԘକڡЋэڡդͧߘीޣԘକڡЋэڡդͧߘीޣ////;();()returnreturnS.S.emptyempty S.S.emptyempty()() ??returnfalsereturnfalse :S.:S.poppop()();; ՙͤ܍Ӟߘ஌ͣٗڞՙ͵ߘٜीͧӨЈԘକͶժӨ܍ՙͤ՗܍Ӟߘ஌ͣٗڞՙ͵ߘٜीͧӨЈԘକͶժӨ܍elseelse////՗ ՙ͵Өૡߘ܍ՙ͵Өૡߘٗ܍ٗ////;([;([if(if('('=='('==exp[iexp[i])])S.S.pushpush(exp[i(exp[i ॔؀Ӹڡ૵▁߳ߓ॔؀Ӹڡwhile(while(exp[exp[++++ii])])////૵▁߳ߓ inti=inti=11;; ՙ܍ԘକࣩٗޭظٜՆࢻѷڢՙ࣐҄ߘ੢܍ԘକࣩٗޭظٜՆࢻѷڢStackStackS;S;////࣐҄ߘ੢ 55 boolboolparenparen(const(const charexp[],intlo,inthi){charexp[],intlo,inthi){ ՙԘକ͵ؗࢻ܍ՙԘକ͵ؗࢻ܍ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 240 ړ࣐͵૱ࡇҹ਽૖ڀߘړ࣐͵૱ࡇҹ਽૖ڀ(c4)(c4)ߘ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 239 (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ ૞֚։▁ࣩҪॹ૞֚։▁ࣩҪॹ20122012 ફ׶ߗ੫Ӟߘړફ׶ߗ੫Ӟߘ਽૖ړ(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))(8+9))$$ 20122012 ਽૖ ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))(8+9))$$ $$ 20122012 ૜ॠ॔૜ॠ॔''''Ӟߘ ՙӞߘ܍ՙӞߘٗ܍ٗ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9)(8+9)))$$ $$ 204836204836 ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9)(8+9)))$$ $$ (( 204836204836 ૜ॠ॔૜ॠ॔''''Ӟߘ ՙӞߘ܍ՙӞߘٗ܍ٗ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9(8+9)))$)$ $$ (( 2048531720485317 ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9(8+9)))$)$ $$ (( (( 2048531720485317 ૜ॠ॔૜ॠ॔'+''+'Ӟߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+(8+99))$))$ $$ (( (+(+ 2048538920485389 ݒҀݤݒҀݤ99ҴߘҴߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8(8++9))$9))$ $$ (( (+(+ 20485382048538 ૜ॠ॔૜ॠ॔'+''+'ҴߘҴߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767((88+9))$+9))$ $$ (( (( 20485382048538 ݒҀݤݒҀݤ88ҴߘҴߘ ՙҴߘ܍ՙҴߘٗ܍ٗ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767((8+9))$8+9))$ $$ (( (( 204853204853 (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ (( 204853204853 ૜ॠ॔૜ॠ॔''''ҴߘҴߘ ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ (( 204853204853 ૜ॠ॔૜ॠ॔''''Ӟߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ (( 204812067204812067 ݒҀݤݒҀݤ6767ҴߘҴߘ ૜ॠ॔ߘ૜ॠ॔ߘ ݒҀݤߘݒҀݤߘ ࡉ੕ࡉ੕ ړ਽૖ړ਽૖ ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 11 238 (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ (( 20481202048120 ૜ॠ॔૜ॠ॔''''ҴߘҴߘ ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ (( 20481202048120 ૜ॠ॔૜ॠ॔'!''!'Ӟߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5(5!!6767(8+9))$(8+9))$ $$ (!(! 2048520485 ૜ॠ॔૜ॠ॔'!''!'ҴߘҴߘ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)((55!!6767(8+9))$(8+9))$ $$ (( 2048520485 ݒҀݤݒҀݤ55ҴߘҴߘ ՙҴߘ܍ՙҴߘٗ܍ٗ (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)((5!5!6767(8+9))$(8+9))$ $$ (( 20482048 (0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ 20482048 ૜ॠ॔૜ॠ॔''''ҴߘҴߘ ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ 20482048 ૜ॠ॔૜ॠ॔'*''*'Ӟߘ ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $*$* 2102421024 ૜ॠ॔૜ॠ॔'^''^'Ӟߘ ՙӞߘ܍ՙӞߘٗ܍ٗ (0!+1)*2^(3!+4(0!+1)*2^(3!+4))(5!(5!6767(8+9))$(8+9))$ $*^$*^ 22102210 ਸ਼۝ਸ਼Ӟߘ۝(0!+1)*2^(3!+4(0!+1)*2^(3!+4))(5!(5!6767(8+9))$(8+9))$ $*^($*^( 22102210 ૜ॠ॔૜ॠ॔'+''+'Ӟߘ (0!+1)*2^(3!+(0!+1)*2^(3!+44))(5!(5!6767(8+9))$(8+9))$ $*^(+$*^(+ 22642264 ݒҀݤݒҀݤ44ҴߘҴߘ (0!+1)*2^(3!(0!+1)*2^(3!++4)4)(5!(5!6767(8+9))$(8+9))$ $*^(+$*^(+ 226226 ૜ॠ॔૜ॠ॔'+''+'ҴߘҴߘ ਸ਼۝ਸ਼Ӟߘ۝((0!+1)*2^(3!0!+1)*2^(3!++4)4)(5!(5!6767(8+9))$(8+9))$ $*^($*^( 226226 ૜ॠ॔૜ॠ॔'!''!'Ӟߘ (0!+1)*2^(3(0!+1)*2^(3!!+4)+4)(5!(5!6767(8+9))$(8+9))$ $*^($*^(͜͜ 223223 ૜ॠ॔૜ॠ॔'!''!'ҴߘҴߘ ૜ॠ॔ߘ૜ॠ॔ߘ ݒҀݤߘݒҀݤߘ ࡉ੕ࡉ੕ ړ਽૖ړ਽૖ ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 237 (0!+1)*2^((0!+1)*2^(33!+4)!+4)(5!(5!6767(8+9))$(8+9))$ $*^($*^( 223223 ݒҀݤݒҀݤ33ҴߘҴߘ ՙҴߘ܍ՙҴߘٗ܍ٗ (0!+1)*2^(0!+1)*2^((3!+4)3!+4)(5!(5!6767(8+9))$(8+9))$ $*^($*^( 2222 (0!+1)*2(0!+1)*2^^(3!+4)(3!+4)(5!(5!6767(8+9))$(8+9))$ $*^$*^ 2222 ૜ॠ॔૜ॠ॔'^''^'ҴߘҴߘ (0!+1)*(0!+1)*22^(3!+4)^(3!+4)(5!(5!6767(8+9))$(8+9))$ $*$* 2222 ݒҀݤݒҀݤ22ҴߘҴߘ (0!+1)(0!+1)**2^(3!+4)2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $*$* 22 ૜ॠ॔૜ॠ॔'*''*'ҴߘҴߘ ՙӞߘ܍ՙӞߘٗ܍ٗ (0!+1(0!+1))*2^(3!+4)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ 22 ਸ਼۝ਸ਼Ӟߘ۝(0!+1(0!+1))*2^(3!+4)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $($( 22 ૜ॠ॔૜ॠ॔'+''+'Ӟߘ (0!+(0!+11)*2^(3!+4))*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $(+$(+ 1111 ݒҀݤݒҀݤ11ҴߘҴߘ (0!(0!++1)*2^(3!+4)1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $(+$(+ 11 ૜ॠ॔૜ॠ॔'+''+'ҴߘҴߘ ਸ਼۝ਸ਼Ӟߘ۝(0!(0!++1)*2^(3!+4)1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $($( 11 ૜ॠ॔૜ॠ॔'!''!'Ӟߘ (0(0!!+1)*2^(3!+4)+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $(!$(! 00 ૜ॠ॔૜ॠ॔'!''!'ҴߘҴߘ ((00!+1)*2^(3!+4)!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $($( 00 ݒҀݤݒҀݤ00ҴߘҴߘ ՙҴߘ܍ՙҴߘٗ܍ٗ )$)$ $((((0!+1)*2^(3!+4)0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9 ફ׶ߗ੫Ҵߘړફ׶ߗ੫Ҵߘ਽૖ړ(0!+1)*2^(3!+4)(0!+1)*2^(3!+4)(5!(5!6767(8+9))$(8+9))$ $$ ਽૖ ૜ॠ॔ߘ૜ॠ॔ߘ ݒҀݤߘݒҀݤߘ ࡉ੕ࡉ੕ ړ਽૖ړ਽૖ ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 236 };}; /*/* \\0*/0*/ '<','<','<','<','<','<','<','','=''<','<','<','<','<','<','<','','=' /*|)*//*|)*/ '','','','','','','','','','','','','','','','','','', /*/*॔॔ (*/(*/ '<','<','<','<','<','<','<','=','','<','<','<','<','<','<','<','=','', /*/*ॠॠ !*/!*/ '>','>','>','>','>','>','','>','>','>','>','>','>','>','>','','>','>', /*/*૜૜ ^*/^*/ '>','>','>','>','>','<','<','>','>','>','>','>','>','>','<','<','>','>', /*/*஌஌ /*//*/ '>','>','>','>','<','<','<','>','>','>','>','>','>','<','<','<','>','>', /*/*ߘߘ **/**/ '>','>','>','>','<','<','<','>','>','>','>','>','>','<','<','<','>','>', /*|/*| */*/'>','>','<','<','<','<','<','>','>','>','>','<','<','<','<','<','>','>', /*/* +*/+*/'>','>','<','<','<','<','<','>','>','>','>','<','<','<','<','<','>','>', //+//+ */^!()*/^!()\\00 Ӹ૜ॠ॔ ||ڡӸ૜ॠ॔ڡ || //// [[ӸڡӸڡ][][constcharconstcharpri[N_OPTR][N_OPTRpri[N_OPTR][N_OPTR]={]={////૜ॠ॔ѨҮॗঃ૜ॠ॔ѨҮॗঃ [[ߘ஌ߘ஌ ֚ஐ͵ѨҮঃ਽֚ஐ͵ѨҮঃ਽ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 235 }}//switch//switch }} ////case'>'case'>' break;break; opnd.push(calcu(opnd.popopnd.push(calcu(opnd.pop(),op,(),op,opnd.popopnd.pop()))()))ͺͺ͵ܯНѽЈࣻ͵ܯ}} ////НѽЈࣻ opnd.opnd.pushpush((calcucalcu(pOpnd1,op,pOpnd2));(pOpnd1,op,pOpnd2)); floatpOpnd2=floatpOpnd2=opnd.opnd.poppop(),pOpnd1=(),pOpnd1=opnd.opnd.poppop();(); else{else{ ////зҪ૜ॠ॔зҪ૜ॠ॔ if('!'if('!'===op)=op)opnd.opnd.pushpush((calcucalcu(op(op,,opnd.opnd.poppop()));()));////▁Ҫ૜ॠ॔▁Ҫ૜ॠ॔ ࣩ૜ॠڀتਸ਼۝ࣩ૜ॠߘ஌૜ॠ॔Ӟߘͧڀتਸ਼۝charop=charop=optr.optr.poppop();(); ////ߘ஌૜ॠ॔Ӟߘͧ ࣩਗ਼ॠͧগ߈Ҵߘڀளͧؗݲࣼޟࣩਗ਼ॠͧগ߈Ҵߘߘ஌૜ॠ॔ѨҮঃڀளͧؗݲࣼޟcase'>':{case'>':{////ߘ஌૜ॠ॔ѨҮঃ ॔؀ݗЇ▁Жܯٶՙ܍ড়॔؀ݗЇ▁Жܯٶՙ܍optr.optr.poppop();S++;break;();S++;break; ////ড় ଑⫩ҽ''\\0'0'ͤͤـۋՙͧ܍Ӹ૜ॠ॔Н՗ڡ଑⫩ҽـۋՙͧ܍Ӹ૜ॠ॔Н՗ڡcase'=':case'=':////ѨҮঃࣼॗͣѨҮঃࣼॗͣ Ӹ૜ॠ॔ૡߘૡߘڡӸ૜ॠ॔ਗ਼ॠܱ૥ͧڡoptr.optr.pushpush(*S);S++;break;(*S);S++;break;////ਗ਼ॠܱ૥ͧ ѹޟѹߘ஌૜ॠ॔ѨҮঃޟcase'<':case'<':////ߘ஌૜ॠ॔ѨҮঃ 55 switch(switch(orderBetweenorderBetween(optr.(optr.toptop(),*S)){(),*S)){ ࣂݱࡄדࣂݱࡄؗࢻ͵ЈբѨҮঃדؗࢻ͵ЈբѨҮঃ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 234 };}; /*/* \\0*/0*/ '<','<','<','<','<','<','<','','=''<','<','<','<','<','<','<','','=' /*|)*//*|)*/ '','','','','','','','','','','','','','','','','','', /*/*॔॔ (*/(*/ '<','<','<','<','<','<','<','=','','<','<','<','<','<','<','<','=','', /*/*ॠॠ !*/!*/ '>','>','>','>','>','>','','>','>','>','>','>','>','>','>','','>','>', /*/*૜૜ ^*/^*/ '>','>','>','>','>','<','<','>','>','>','>','>','>','>','<','<','>','>', /*/*஌஌ /*//*/ '>','>','>','>','<','<','<','>','>','>','>','>','>','<','<','<','>','>', /*/*ߘߘ **/**/ '>','>','>','>','<','<','<','>','>','>','>','>','>','<','<','<','>','>', /*|/*| */*/'>','>','<','<','<','<','<','>','>','>','>','<','<','<','<','<','>','>', /*/* +*/+*/'>','>','<','<','<','<','<','>','>','>','>','<','<','<','<','<','>','>', //+//+ */^!()*/^!()\\00 Ӹ૜ॠ॔ ||ڡӸ૜ॠ॔ڡ || //// [[ӸڡӸڡ][][constcharconstcharpri[N_OPTR][N_OPTRpri[N_OPTR][N_OPTR]={]={////૜ॠ॔ѨҮॗঃ૜ॠ॔ѨҮॗঃ [[ߘ஌ߘ஌ ؗࢻ͵ѨҮঃ਽ؗࢻ͵ѨҮঃ਽ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 233 }} դࣩਗ਼ॠগ߈ޣӞڞդࣩਗ਼ॠগ߈ޣӞڞ////;();()returnreturnopnd.opnd.poppop }} //while//while ࣂ */*/}}דࣂӢӯדswitch(switch(orderBetweenorderBetween(optr.(optr.toptop(),*S)){(),*S)){/*/*Ӣӯ Н૜ॠ॔ͧӨੑҾЉߘ஌૜ॠ॔У୎ѨҮঃࣩளѹੑҾЉߘ஌૜ॠ॔У୎ѨҮঃࣩளѹ॔؀ӸڡН૜ॠ॔ͧӨਂ॔؀Ӹڡਂ////elseelse ѸࣩͤݒҀݤיѸࣩͤݒҀݤ੾ҴͣՔৗיreadNumberreadNumber(S(S,opnd);,opnd);////੾ҴͣՔৗ НݒҀݤͧӨ॔؀ӸڡНݒҀݤͧӨਂ॔؀Ӹڡਂ//// ((if(if(isdigitisdigit(*S))(*S ࣻ২ͧࣻ২૜ॠ॔ߘी૜ॠ॔ߘीͧ॔؀ࣂ՞ד૵Ж॔؀ࣂ՞דwhile(!while(!optr.optr.emptyempty()){()){////૵Ж ҽநҮҴߘ⫩עҽநҮҴߘЬҀН⫩עҽ''\\0'0'ЬҀН⫩ـҽ⫩ـ////;('optr.optr.pushpush('('\\0');0 StackStack<opnd;>opnd;StackStack<optr;>optr;////૜ॠݤߘ澝૜ॠ॔ߘ૜ॠݤߘ澝૜ॠ॔ߘ ࠭Қړ࠭ҚЗথ਽૖ړ55 floatfloatevaluateevaluate(char(char*S*S,char*&RPN){){////Зথ਽૖ ؗࢻ͵Оॠࡄؗࢻ͵Оॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 248 {for(j=9;j;i>8?printf("%10d",a[j]):q(|a)||(q(=a)=i,main(i+1),q(=a)=0)); } main(i,j) a[39]; #defineq(o)a[j]o[j+i+7]o[ji+31] /*Andor@net9.org,2002*/ j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&++a[i]);} !l<'case'>' /*.........................................*//*.........................................*/ ҴҴRPNRPNܯܯ////;(charop=charop=optr.optr.poppop();();appendappend((RPNRPN,op);,op Ҿذਗ਼ॠࣩբݿڀਸ਼ࣼ۝ਸ਼ͧӨ֧۝ҾЋՔैԮذਗ਼ॠࣩբݿڀਸ਼ࣼ۝ਸ਼ͧӨ֧۝case'>':{case'>':{////ЋՔैԮ /*.........................................*//*.........................................*/ switch(switch(orderBetweenorderBetween(optr.(optr.toptop(),*S)){(),*S)){ Н૜ॠ॔॔؀ӸڡН૜ॠ॔ਂ॔؀Ӹڡਂ////elseelse ҴRPNRPNܯٶҴ੾Ӟܯٶ{{ readNumberreadNumber(S(S,opnd);,opnd); appendappend((RPNRPN,op);,op); }} ////੾Ӟ ҾذܯҾࣻذܯНݒҀݤͧӨࣻ॔؀ӸڡНݒҀݤͧӨਂ॔؀Ӹڡਂ////((if(if(isdigitisdigit(*S))(*S ࣻ২ͧࣻ২૜ॠ॔ߘी૜ॠ॔ߘीͧ॔؀ࣂ՞ד૵Ж॔؀ࣂ՞דwhile(!while(!optr.optr.emptyempty()){()){////૵Ж /*.........................................*//*.........................................*/ ܦૃܦ55 floatfloat evaluateevaluate(char(char*S,*S,char*&char*&RPNRPN){){////RPNRPNૃ ॠࡄܦૃ͵ॠࡄܦૃ͵infixinfixӰӰpostfixpostfix DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 245 ......ܦૃܦRPNRPNૃۉ؎тՔբݿͧىࣛҞ۞ۉ؎тՔբݿͧى࠭Қॠࡄevaluate()evaluate()ࣛҞ۞ړ࠭ҚॠࡄЗথړЗথ 55 ^^ 55 ++44 ** !!2323 ++11 !!00 ڰहжݦࣂͧԮڰ4)4)हжݦࣂͧԮ 00!! 11++ 2323!! ** 44++ 55 ^^ ՙ܍ޥےՙ㝂Խ܍ޥے3)3)㝂Խ {([0]{([0]!! 1)1)++ ([(2[3]([(2[3]!! ))** 4]4]++ 5)5) }}^^ ՙդ܍ࣩ՗ڀت૜ॠ॔शӰذՙդ܍ࣩ՗ڀت૜ॠ॔शӰذ(2)2 {([0{([0!! ]]++ 1)1)^^ ([(2([(2** [3[3!! ])])++ 4]4] 5)}5)} ਽ठѨҮঃ֩ړދՙ܍਽ठѨҮঃ࣐֩ړދՙ܍1)1)࣐ Ҝ੦͵жҮޭػ૜ॠ॔У୎ࣩѨҮঃһॸҞ૚џѽংؓҜ੦͵жҮޭػ૜ॠ॔У୎ࣩѨҮঃһॸҞ૚џѽংؓ (5(5 44 ++ !!33 **2)2) ^^(1(1 ++ !!0)0)͵ױ҅͵ױ҅ 55 ܦૃٖە͵ܦૃٖە͵infixinfixӰӰpostfixpostfix DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 244 20092009 99 99 20182018 20182018 20162016 ** 22 88 252252 88 22 252252 77 3636 2020 66 120120 2424 88 !! 22 ** 22 3636 77 22 22 1616 // 22 120120 1616 66 22 1616 !! 22 55 1616 55 22 1616 22 44 88 44 22 88 ^^ 22 33 22 33 22 22 22 22 ++ 22 11 11 11 !! 11 00 00 0!1+23^4!0!1+23^4! 5!6/5!6/ 7*8*7*8* 99 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 243 18801880 20042004 1692016920 42304230 50405040630630 36003600 720720 11124124 !! 88 // ++ ** 99 // ++123123 ++ 44 55 66 !! ** 7700 !! RPNRPN࠭Қ͵ؗ҅࠭Қ͵ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 242 ځߘފЬځߘފ૞֚ߘ஌૞֚ߘ஌;;////Ь }}//while//while }}//else//else ૜ॠগ߈ଜݰԸҴS;S;ذٶ૜ॠগ߈ଜݰԸҴ૜ॠͧذٶӞࣩݒҀݤؗݲxx૜ॠͧڞتӞࣩݒҀݤؗݲڞت ;;୻ݤࣹࣩݒҀݤے୻ݤࣹࣩݒҀݤےӞ૜ॠ॔xxڞӞ૜ॠ॔ЗڞёߘёߘSSЗ ૜ॠ॔ފ૜ॠ॔ފelse{else{//x//x xxԸҴԸҴS;S;ذذ ((ݒҀݤފݒҀݤފif(xif(x ੾Ҵ੾ҴexprexprࣩЇ▁ҪॹࣩЇ▁Ҫॹx;x; ࠣ){){؎ܵ۟ޭظࠣ؎ܵ۟ޭظwhile(exprwhile(expr ࠭Қړ࠭ҚݜтवҀߘړݚݒҀݤ;;////ݜтवҀߘ؁ݚݒҀݤ࣐ͧј؁ҴߘSS࣐ͧјڕҴߘڕ exprexprࣩ੶ࡄࠔगࣩ੶ࡄࠔगړ਽૖ړ55 rpnEvaluate(exprrpnEvaluate(expr))////ҜؓҜؓRPNRPN਽૖ RPNRPN࠭Қ͵ॠࡄ࠭Қ͵ॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 241 ीߡͤױीߡͤͣࠢױࠢͣ॔؀Ҫ॔؀Ҵ▁ЖફӢ୫Ҁ࣐ࣩҪڕטҴ▁ЖફӢ୫Ҁ࣐ࣩҀН਼ңͧஏ஛ڕט55 ҀН਼ңͧஏ஛ postfixpostfixͤͤͣړтवҀդথͣړinfixinfixͤͧͤͧRPNRPNтवҀդথͣړ࣐ࣩЗথ҄ٮиݺتࣼͣړ࣐ࣩЗথ҄ٮиݺتࣼ 55 99 *8*7*8*7 5656 123+4!123+40!0 99 8*7*(856*7*(56 44 1+23)1+23) !0!0 ͵ױՀ͵ױՀ 55 +/9*+/8!7*!123+456!+0/9*+/8!7*!123+456!0 9/(8/!+7!6*5)*+123+4!90/(8/!+7!6*5)*+123+4!0 ͵ױ҅͵ױ҅ 55 ՙͣparenthesisparenthesisfreefreeͤԮՔ਽ठ٪ѨҮঃࣩ૜ॠһॸͤԮՔ਽ठ٪ѨҮঃࣩ૜ॠһॸ܍ՙͣЈ࣐҄܍Ј࣐҄ Зړࣩ਽૖ۉЗͤঐړࣩ਽૖ۉ55 ֧࣒૜ॠ֧࣒॔ͣ૜ॠ॔ͣoperatoroperatorͤոݒҀݤͣͤոݒҀݤͣoperandoperandͤঐ J.LukasiewiczJ.Lukasiewiczͣͣ12/21/187812/21/1878–– 02/13/195602/13/1956ͤͤ ReversePolishNotationReversePolishNotationͤͤͣړࡇҹ਽૖૱ͣړ55 ૱ࡇҹ਽૖ RPNRPN DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 256 }} Ӿ߉ۋӢݖٜ֭੃߳ߓޥےӾ߉ࣻ২ۋӢݖٜ֭੃߳ߓޥے}while((0<}while((0if(N>q.yq.y ӰՔ݃ݚЇ▁࣫դࣩӦۨੰعӰՔ݃ݚЇ▁࣫դࣩӦۨੰع////;++;++q.yq.y ت࣫դࣩࠢޥૹ૚Љٜت࣫դࣩࠢޥwhile((while((q.yq.y solu;>solu;QueenQueen q(0,0);q(0,0 ڿୋஙࣩ੐ࠆՔџͧڿ࣫դ㔀ݤ࣫դ㔀ݤͧୋஙࣩ੐ࠆՔџ== سלߵࣶسל55 voidvoidplaceQueensplaceQueens(int(int N){N){////N=N=ߵࣶ ૹ࣐ॠࡄૹ࣐ॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 255 ୠѹͧѷॠࡄࣩૹ࣐㓬㺚҃ללڅ޳וୠѹͧѷॠࡄࣩૹ࣐㓬㺚҃ללڅ޳ו//// {{ {{ ;(nSolunSolu++;++;displaySolution(soludisplaySolution(solu,4);,4 if(!if(!collidecollide(solu(solu,3)){,3)){ ////Ӿ߉Ӿ߉ for(solu[3]=0;solu[3]<4;solu[3]++)for(solu[3]=0;solu[3]<4;solu[3]++) if(!if(!collidecollide(solu(solu,2)),2)) ////Ӿ߉Ӿ߉ for(solu[2]=0;solu[2]<4;solu[2]++)for(solu[2]=0;solu[2]<4;solu[2]++) if(!if(!collidecollide(solu(solu,1)),1)) ////Ӿ߉Ӿ߉ for(solu[1]=0;solu[1]<4;solu[1]++)for(solu[1]=0;solu[1]<4;solu[1]++) if(!if(!collidecollide(solu(solu,0)),0)) ////Ӿ߉Ӿ߉ for(solu[0]=0;solu[0]<4;solu[0]++)for(solu[0]=0;solu[0]<4;solu[0]++) intsolu[4];intsolu[4];////җ૲੕঩ऑզଞҗ૲੕঩ऑզଞ 55 voidvoidplace4Queensplace4Queens(){(){//4//4࣫դӾ߉ॠࡄ࣫դӾ߉ॠࡄ Ӿ߉Ӿ߉ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 254 ள૖O(4^4)=O(4^4)=O(n^nO(n^n))څ޳וள૖څ޳ו////{{ {{ ;(nSolunSolu++;++;displaySolution(soludisplaySolution(solu,4);,4 if(if(collidecollide(solu(solu,3))continue;,3))continue; if(if(collidecollide(solu(solu,2))continue;,2))continue; if(if(collidecollide(solu(solu,1))continue;,1))continue; if(if(collidecollide(solu(solu,0))continue;,0))continue; җ૲੕ޥےҗ૲੕߇Пޥےfor(solu[3]=0;solu[3]<4;solu[3]++){for(solu[3]=0;solu[3]<4;solu[3]++){////߇П for(solu[2]=0;solu[2]<4;solu[2]++)for(solu[2]=0;solu[2]<4;solu[2]++) for(solu[1]=0;solu[1]<4;solu[1]++)for(solu[1]=0;solu[1]<4;solu[1]++) for(solu[0]=0;solu[0]<4;solu[0]++)for(solu[0]=0;solu[0]<4;solu[0]++) intsolu[4];intsolu[4];////җ૲੕঩ऑզଞҗ૲੕঩ऑզଞ 55 voidvoidplace4Queens_BruteForceplace4Queens_BruteForce(){(){//4//4࣫դ塃Ԃॠࡄ࣫դ塃Ԃॠࡄ 塃Ԃܽॺ塃Ԃܽॺ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 253 7 5 3 1 6 4 2 0 ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ 4 7 3 0 6 1 5 2 ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ੕ͤͣفЖՔਸ਼١▁ފڶӦͧޭܭ੕ͤՅУͧࠠ▁ૠߞࣩͣفЖՔਸ਼١▁ފڶӦͧޭܭ55 ՅУͧࠠ▁ૠߞࣩ ӦܭӦࣩ▁Жܭҗ૲੕ͤ଒Ք঩ऑНݦݤ{0,...,n{0,...,n1)1)ࣩ▁Жͣف١▁җ૲੕ͤ଒Ք঩ऑНݦݤ֛ࠕͧࠠͣف١▁55 ֛ࠕͧࠠ Ж࣫դ▁ޥЋэޥࠠਸ਼ͣӦͤ͵اЖ࣫դ੏▁ޥЋэޥࠠਸ਼ͣӦͤ͵ا55 ੏ ঩ऑ঩ऑ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 252 ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 251 ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ᄰ ...,1,0,0,1,2,1,6,12,46,92...,1,0,0,1,2,1,6,12,46,92 ͵וЈ੤ଜ͵וЈ੤ଜ ...,1,0,0,2,10,4,40,92...,1,0,0,2,10,4,40,92 ͵וҩ੤ଜ͵וҩ੤ଜ 55 n=1,2,3,4,...n=1,2,3,4,... ժিਨݵૃݵૃ澝澝঻ૃ঻ૃУդࣩॗўͺУդࣩॗўͺފժিਨފ ͺفѽ١ױͺفमՔਸ਼ࣩ١شיޥͺفѽ١ױͺفमՔਸ਼ࣩ١شיޥ ࠕкЈݙӟڨׯњڰࠕкЈݙӟЖ࣫դͧ҄ڨׯњڰ55 ֧֧nnnnࣩߵࣶІݚ঴ࣩߵࣶІݚ঴nnЖ࣫դͧ҄ Ҷ࣫դҶ࣫դ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 250 Ҳࢻͺړڤগ߄ࣩܨѽјݤױҲࢻͺړڤগ߄ࣩܨѽјݤױ ࢤҒݯࣩࡄؖࢤҒݯࣩࡄؖ ==঎僕঎僕 ++७॒७॒ 55 ܮটডੰܮটডੰ Ӿ߉֚䆵Ӿ߉֚䆵//// ٶͧڅݗব২Ӹ▁େٶͧڅݗব২Ӹ▁େ ઑͧӨסઑͧӨ▁ݻՆࢻࡉؓੌסݻՆࢻࡉؓੌ▁ ܮੰܮੰ//// څ׶ͧ૵ࡰ׌ԅҗ૲੕େڏڅ׶ͧ૵ࡰ׌ԅҗ૲੕େڏёё00 ړࠆͤړ䆵ͣprobeprobebacktrackbacktrackͤࠆ֚ܮ䆵ͣੰ֚ܮੰ 55 ઄Ӿ߉ͣpruningpruningͤͤے઄Ӿ߉ͣࠕԮےࠕԮ ୣҗ૲੕ܭ֩ۦۉҍՔͧڡڰୣҗ૲੕ࠕݿՐੌज़ࣛܭ֩ۦۉҍՔͧڡڰࠕݿՐੌज़ࣛ ժ՟ࣂފժ՟ࣂͧԮՔӭݮҾފԮՔӭݮҾͧڪ଑ࢤفڪ଑ࢤفҗ૲੕ࣩߏमܨҗ૲੕ࣩߏमжؗІͧߠܨжؗІͧߠ 55 ڮࣩ੐ޥӮ࣐Ҿࢤٶ࣐ୋஙͧڀஏ࡬ӵࣂ੕ڮࣩ੐ޥӮ࣐Ҿࢤٶ࣐ୋஙͧڀஏ࡬ӵࣂ੕ ୣҗ૲੕ܭ澝ؿՔৗݾ֩יୣҗ૲੕НؿՔৗܭ澝ؿՔৗݾ֩י55 НؿՔৗ 䆵֚䆵Ӿ߉Ӿ߉֚ܮੰܮੰ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 249 BeijingBeijing WuhanWuhan ShanghaiShanghai GuangzhouGuangzhou Xi'anXi'an јӃړ஍יӱ֧ܰڅ޳וݿ୎ذ࢐ݸࡄڶࠕͧױјӃړ஍יӱ֧ܰڅ޳וݿ୎ذ࢐ݸࡄڶࠕͧױ ժ՟ࣂފ߳ߓҾٶՔৗࣩҗ૲੕ͧۉժ՟ࣂ୻૵▁࣏ފ߳ߓҾٶՔৗࣩҗ૲੕ͧۉ୻૵▁࣏ 55 ਂଙ࣐塃Ԃज़ࣛ࠭੕ਂଙ࣐塃Ԃज़ࣛ࠭੕ n!/nn!/n =(n=(n1)!=1)!=O(nO(nnn))ޥН҅ͧҺޥѐјѐјTSPTSPН҅ͧҺ ࣩܽॺी୎לЖ߃▁ۉ߄ککЖҗ૲੕ͧ▁ފӦঐ՟଒ܭ▁ࣩܽॺी୎ࠠלЖ߃▁ۉ߄ککЖҗ૲੕ͧ▁ފӦঐ՟଒ܭ▁55 ࠠ ѹࣩࢺࡶષ঎ޣޯۉѹࣩࢺࡶષ঎Жּ٠У୎㔀ޣޯۉјјTSPTSPୋஙН҅ͧԮচؓୋஙН҅ͧԮচؓnnЖּ٠У୎㔀 Ӧٽࣩۉ߄ٽࢤؓࠊܘҪॹٳІ଒ՔऀҀਂړڤୋஙࣩ੕ͧיڭӦٽࣩۉ߄ٽࢤؓࠊܘҪॹٳІ଒ՔऀҀਂړڤୋஙࣩ੕ͧיڭ 55 ݤࢗ䎮ܗݤࢗ䎮ܗ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 264 ՌЉؗࢻܯՌЉؗࢻ୓Ӧܯ(e)(e)୓Ӧ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 263 ૹ࣐ॠࡄૹ࣐ॠࡄ 55 ...... ܮও䎯ݱզѨҮੰ͵ڵ帙ܮও䎯ݱզѨҮੰ͵ڵ帙 ࡤԽٶષ͵ؿՔৗՆࢻڜࡤԽٶષ͵ؿՔৗՆࢻڜ ࡤԽٶࡤԽࢺષ͵ؿՔৗՆࢻٶࢺષ͵ؿՔৗՆࢻ एࣩૹષͺޟѽۨӞױएࣩૹષͺޟѽۨӞױ 55 ݘӉݘӉneighboneighbor()r()ͧ۞Ҭ֙Жݱզͧ۞Ҭ֙Жݱզ Ҷ૤ૹ૜ԇ੐ӨͺܕѽݖױҶ૤ૹ૜ԇ੐Өͺܕѽݖױ 55 ՞ݱզܮ՞ݱզଙ࣐୩޲ज़ࣛͧॗࠃࢴੰܮଙ࣐୩޲ज़ࣛͧॗࠃࢴੰ 㖊Ӑࣩࠃࢴͺ֯ޣѽୠѹױ㖊Ӑࣩࠃࢴͺ֯ޣѽୠѹױ 55 ૡ▁ࣩࠖিਨૡ▁ࣩࠖিਨ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 262 inlineESWNinlineESWNnextESWN(ESWNnextESWN(ESWN eswneswn){returnESWN(){returnESWN(eswneswn +1);}+1);} ݱզܯݱզ҈ࠊૃ২Ї▁଍ܯ////҈ࠊૃ২Ї▁଍ typedeftypedefenumenum {UNKNOWN,EAST,SOUTH,WEST,NORTH,NO_WAY}ESWN;{UNKNOWN,EAST,SOUTH,WEST,NORTH,NO_WAY}ESWN; ݱզ͵͵ޭؓ澝А澝Ԧ澝ੋ澝ԗ澝ݸષՔૹޭؓ澝А澝Ԧ澝ੋ澝ԗ澝ݸષՔૹܯ଍تݱզԤҪࣩࣼܯ଍ت////ԤҪࣩࣼ typedeftypedefenumenum {AVAILABLE,ROUTE,BACKTRACKED,WALL}Status;{AVAILABLE,ROUTE,BACKTRACKED,WALL}Status; ઑդ֚䆵૚澝ЈՔ࣐҄ͣ׋ͤסੰعݱզ֭ޥےІ澝ګӸષڡઑդ֚䆵૚澝ЈՔ࣐҄ͣ׋ͤ澝֧סੰعݱզ֭ޥےІ澝ګӸષڡ////ࢩ㓆͵ࢩ㓆͵Ք࣐Ք࣐澝֧ 55 ࣼһ६ֹࣼһ६ֹ };}; ESWNincoming,outgoing;ESWNincoming,outgoing;////ૡҴ澝઩ӞݱզૡҴ澝઩Ӟݱզ StatusStatusstatusstatus;;////६ֹ६ֹ intx,y;intx,y;////ְߗְߗ public:public: 55 classCell{classCell{ ////૪؜ԤҪ૪؜ԤҪ গ߄ܨগ߄ݤܨݤ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 261 }} returnfalse;returnfalse; }while(!}while(!path.path.emptyempty());()); }} //else//else cc>outgoing=UNKNOWN;c>outgoing=UNKNOWN;c>status=ROUTE;>status=ROUTE; path.path.pushpush(c(c ==advance(cadvance(c));)); ࠖ▁ܮࠖժӨͧզӸੰ▁ܮ}else{}else{////ժӨͧզӸੰ cc>status=BACKTRACKED;c=>status=BACKTRACKED;c=path.path.poppop();();////ͣͣTheseusTheseusࣩࣩ७॒ͤ७॒ͤ ૚ͧӨզդ֚䆵▁ࠖͧӨզդ֚䆵▁ࠖੰعݱզ଒ٜޥے૚ਂੰعݱզ଒ٜޥےਂ////}(if(NO_WAY<=cif(NO_WAY<=c>outgoing){>outgoing ࣩݱզܮੰޭظࣩݱզܮੰޭظ////;if(AVAILABLE==if(AVAILABLE==neighborneighbor(c(c))>status)break;>status)break while(NO_WAY>(cwhile(NO_WAY>(c>outgoing=>outgoing=nextESWNnextESWN(c(c>outgoing)))>outgoing)))////ߓۨՏ▁ߓۨՏ▁ Cell*c=Cell*c=path.path.toptop();if(c==t)returntrue;();if(c==t)returntrue;////ۨӰૹષۨӰૹષͶͶժӨժӨ...... Քৗޥےীिؿۋ૖ও䎯ͧۺ澝֚䆵ͧࣻӰܮՔৗЈݮੰޥےীिؿۋ૖ও䎯ͧۺ澝֚䆵ͧࣻӰܮdo{do{////Јݮੰ ss>incoming=UNKNOWN;s>incoming=UNKNOWN;s>status=ROUTE;>status=ROUTE;path.path.pushpush(s(s););////ёફ䎯ӞՆёફ䎯ӞՆ ૹષͣTheseusTheseusࣩ঎僕ࣩ঎僕ͤͤڢૹષ࣐ͣߘ੢ڢStackStack<path;*>path;////࣐ߘ੢ 55 boolboollabyrinthlabyrinth((CellCell Laby[MAX][MAXLaby[MAX][MAX],],CellCell*s,*s,CellCell*t){*t){ ॠࡄॠࡄ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 260 ֧ͤ؁߈ױҾ୎ۨӞ▁޻֙૤ૹࣩૹષ֧֧ͣͧͤ؁߈ױࣩફ䎯ssոও䎯ոও䎯tt֧ͧҾ୎ۨӞ▁޻֙૤ૹࣩૹષͣؓܗࣩફ䎯ؓܗ 55 ޲֗ъࢉࡶҾ୎ͧࠠࠖՐৗ૜ԇӰА澝Ԧ澝ੋ澝ԗݱզࣩߏ▁଍ߡ޲֗ъࢉࡶҾ୎ͧࠠࠖՐৗ૜ԇӰА澝Ԧ澝ੋ澝ԗݱզࣩߏ▁଍ߡ ݱߡН୭ङࢢٳ׋ͧЗ୎ਂ֟ۉࣩ૪؜ͧ֙մݱߡ߄ۉݱߡН୭ङࢢݱߡঐٳ׋ͧЗ୎ਂ֟ۉࣩ૪؜ͧ֙մݱߡ߄ۉ55 nnnnݱߡঐ ׁࣩޯୋங־ৗॗஔޕׁࣩޯୋஙͤͧॸъٖ־ৗॗஔޕ੐ӥͣpathplanningpathplanningͤͧॸъٖګ੐ӥͣષګષ 55 ګث૪؜ګث૪؜ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 259 Anonymous Whenallelsefails,trybruteforce. "NoMatterWhat",Boyzone We'llfindourownwayback. Nomatterwheretheytakeus, ګث䆵ࡄ͵૪؜֚ܮੰګث䆵ࡄ͵૪؜֚ܮੰ((d2)(d2 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 258 };}; booloperator!=(booloperator!=(QueenQueen const&q){return!(*this==q);}const&q){return!(*this==q);} }} ੔঎ӎूت੔঎ӎूࡀՅت||(x||(x y==y==q.xq.x q.yq.y););////ࡀՅ ੔঎ӎूت੔঎ӎूࡀࠔت||(x+y==||(x+y==q.xq.x ++q.yq.y))////ࡀࠔ ||(y==||(y==q.yq.y))////ӦӎूӦӎू return(x==return(x==q.xq.x))////ਸ਼ӎूͣЈѪՆ࣏ͧՔࣾࣛͤਸ਼ӎूͣЈѪՆ࣏ͧՔࣾࣛͤ booloperator==(booloperator==(QueenQueen const&q){const&q){////ଜૉӭॗݒҀ॔ଜૉӭॗݒҀ॔ QueenQueen(int(int xx=0,intxx=0,intyyyy =0):=0):x(xxx(xx),),y(yyy(yy){};){}; intx,y;intx,y;////࣫դ֧ߵࣶІࣩѸ঴ְߗ࣫դ֧ߵࣶІࣩѸ঴ְߗ public:public: 55 classclassQueenQueen {{////࣫դ६࣫դ६ 55 ؓТ࣫դ६ؓТ࣫դ६QueenQueenͧଜݰؓТӭॗ֗ͧ҄У֧ͧଜݰؓТӭॗ֗ͧ҄У֧੶Т੶ТІЉІЉ““ӎूӎू””ॗўॗў ՌͺܯѽӮ࣐ߘͣզଞࣩͤߓۨױՌͺͧܯѽӮ࣐ߘͣզଞࣩͤߓۨױͧ””((55 јІॠࡄЗࣩјІॠࡄЗࣩ““solu.solu.findfind(q(q গ߄ܨগ߄ݤܨݤ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 10 257 xx xx ᄰᄰ 22 ᄰᄰ 00 xxxx xx ᄰᄰ 33 11xx ᄰᄰ ᄰᄰ 00 xxxx xx ᄰᄰ 33 11xx ᄰᄰ xxxx xx ᄰᄰ 33 11xx ᄰᄰ 11xx ᄰᄰ xxxx xx xx 00ᄰᄰ xxxx xx xx 33xxxx xx ᄰᄰ 00ᄰᄰ 11xx ᄰᄰ 33xxxx xx ᄰᄰ ᄰᄰ xxxx xx xx 00 11xx ᄰᄰ 33xxxx xx ᄰᄰ 00ᄰᄰ 33xxxx xx ᄰᄰ22xx xx ᄰᄰ ᄰᄰ 00ᄰᄰ xxxx xx xx 00 22xx xx ᄰᄰ 00ᄰᄰ 00ᄰᄰ ؗ҅ؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 9 272 (x)(x)SteapSteap ++QueapQueap 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 271 }} ࣐clear()clear()Ӣӯ࡯ीͺͤӢӯ࡯ीͺͤઁ֩ړދժ୻ੌފ࣐ͣઁ֩ړދժ୻ੌފ୓Ӧͣޥے୓Ӧଚݚޥےdelete[]windows;delete[]windows;////ଚݚ while//while//{{ ޢܯԆ؎ࠣӨӞӦ࣒ͧդটஐؙާޢܯwindows[i].windows[i].dequeuedequeue();();////ާԆ؎ࠣӨӞӦ࣒ͧդটஐؙ ՈާԆܯՈާԆܯif(if( windows[i].windows[i].frontfront().time().time <=0)<=0)////୓நஐؙ୓நஐؙ if(!if(!windows[i].windows[i].emptyempty())())////՞ஂी୓Ӧ՞ஂी୓Ӧ for(inti=0;i*windows=new>*windows=newQueueQueue<[>[nWinnWin];]; voidvoidsimulatesimulate(int(int nWinnWin,int,intservTimeservTime){){ 55 ܈ଲਸ਼ާԆࠆ܈ଲਸ਼ާԆࠆ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 270 win0win0 win1win1 win2win2 win3win3 };}; unsignedinttime;unsignedinttime;////ާԆݿେާԆݿେ Ռͣ୓ӦͤॅيےՌͣ୓Ӧͤॅيے////;intwindow;intwindow public:public: 55 classCustomer{classCustomer{////ஐؙ६ஐؙ६ servTimeservTime ////ਗЎݿେਗЎݿେ Կݤ͵Կݤ͵ nWinnWin ////ॅՌͣ୓ӦͤݤࣹॅՌͣ୓Ӧͤݤࣹ 55 ͤـए୓ӦࣩͣޮޣոԅҴ܌ஐؙӰ૖դͧ১ԇ֩૲ͤـए୓ӦࣩͣޮޣոԅҴ܌ஐؙӰ૖դͧ১ԇ֩૲ ୓ॗҗܭѸஐؙͧҾєஐؙ▁ڬܯי୓ॗҗџ▁ݿӵͧࠠЖॅՌ২ܭѸஐؙͧҾєஐؙ▁ڬܯיџ▁ݿӵͧࠠЖॅՌ২ ࠆֹ͵ࠆֹ͵ ܶ҇ܶ҇nnЖާԆॅՌЖާԆॅՌ 55 ܈ଲਸ਼ާԆࠆ܈ଲਸ਼ާԆࠆ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 269 #3 #3 4 # 4 # 2 # 2 # #1 #1 #5 #5 0 # 0 # #6 6 # #7#7 CPU एࣩૹષૹષޣएࣩޣ55 Ӯ࣐୓Ӧݘૡ૪؜ॠࡄͧۨӞӮ࣐୓Ӧݘૡ૪؜ॠࡄͧۨӞ }} }} Q.enqueue(e);Q.enqueue(e);////ଜݰҴ୓ଜݰҴ୓ ՈާԆͧ࢐դܯՈާԆͧ࢐դܯ////;(sserve(e);erve(e ٶӞ୓ͧې୓நؙࣩٶӞ୓ͧېe=e=Q.dequeueQ.dequeue();();////୓நؙࣩ ֩וԆһ୊УӸͧՅާ֧֩וwhile(!while(!ServiceClosedServiceClosed()){()){////֧ާԆһ୊УӸͧՅ ୓ӦQQۉঐېؙޥے୓ӦԿЉઞࡼӢକࣩۉঐېؙޥےQueueQueueQ(clientsQ(clients););////ԿЉઞࡼӢକࣩ ࢺӢକ֗ڱࢺӢକ֗ڱ////}} RoundRobinRoundRobin 55 یלճҺхۙԬ޲ͧીૅۉ؛ؗயͧیלճҺхۙԬ޲ͧીૅۉ؛ҺхCPUCPUͧؗயٽ࣐सڀЖיͧױҺхࠢٽ࣐सڀЖיͧױࠢ ЉЉݝࢴݝࢴͺͺٴҷٴѽӂஐҷױѽӂஐͤҺхբ▁ઞࡼݿͧױclientclientͤҺхբ▁ઞࡼݿͧͣېসؙ▁ͣې55 ▁সؙ ࢺӢକڱࢺӢକઞࡼڱઞࡼ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 268 ࣐ڀ࣐୓Ӧڀ(f)(f)୓Ӧ 4.4.ߘЉ୓ӦߘЉ୓Ӧ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 267 ѽͺױՌͧݝࢴܯؗࢻࣩ୓Ӧےѽͺ੪࡚͵ҁױՌͧݝࢴܯؗࢻࣩ୓Ӧے੪࡚͵ҁ 55 ੿դ͵ׁи੿դ͵ׁиզଞզଞͧࡖ࣏ؓТ୓Ӧࠆ߂६ͧࡖ࣏ؓТ୓Ӧࠆ߂६ Ռ֭ͧՐ୻O(1)O(1)ݿ୎ݿ୎ܯࠕؗࢻࣩ୓Ӧױ͵Ռ֭ͧՐ୻गੜܯࠕؗࢻࣩ୓Ӧױ͵55 गੜ ஜҕ૚޼⩲ͺஜҕ૚޼⩲ͺ ———— ــ//עॏН୓Ӧޮע};};////јӦ਽நјӦ਽ந//ޮॏН୓Ӧ T&front(){returnT&front(){returnfirstfirst()()>data;}>data;}////୓ந୓ந Tdequeue(){returnTdequeue(){returnremoveremove((firstfirst());}());}////Ӟ୓Ӟ୓ voidvoidenqueue(Tenqueue(T const&e){const&e){insertAsLastinsertAsLast(e(e);});}////Ҵ୓Ҵ୓ ࡀ࣐ܯࡀ࣐ࣻܯpublicpublic:: //size()//size()ЉЉempty()empty()ࣻ classclassQueueQueue::publicpublic ListList{{////࣒Ӧ਽ࡖ࣏ࣩ୓Ӧࠆ߂६࣒Ӧ਽ࡖ࣏ࣩ୓Ӧࠆ߂६ 55 templateT> Ӧ਽ࡖ࣏ۋиզଞׁܯӦࣩࢤӦͧݜтՔࣻٽиيӦ਽ࡖ࣏୓Ӧݹ࢐ۋиզଞׁܯӦࣩࢤӦͧݜтՔࣻٽиي55 ୓Ӧݹ࢐ ࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 266 empty()empty() falsefalse 33 77 33 size()size() 44 77 66 1111 33 front()front() 33 33 77 33 front()front() 33 77 66 1111 33 enqueue(3)enqueue(3) 33 77 33 dequeue()dequeue() 77 77 66 1111 33 enqueue(7)enqueue(7) 77 33 dequeue()dequeue() 33 77 66 1111 33 77 dequeue()dequeue() 55 33 enqueue(7)enqueue(7) 77 66 1111 33 77 33 enqueue(3)enqueue(3) 33 55 empty()empty() falsefalse 66 1111 33 77 33 enqueue(5)enqueue(5) 55 enqueue(6)enqueue(6) 66 1111 33 77 33 empty()empty() truetrue size()size() 44 1111 33 77 33 Queue()Queue() enqueue(11)enqueue(11) 1111 33 77 33 ͤע୓Ӧͣ՗ҊН୓ͤעݒҀݒҀ ૎Ӟ૎Ӟ ୓Ӧͣ՗ҊН୓ ͤע୓Ӧͣ՗ҊН୓ͤעݒҀݒҀ ૎Ӟ૎Ӟ ୓Ӧͣ՗ҊН୓ ݒҀؗ҅ݒҀؗ҅ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 265 dequeue enqueue Ռ͵getMax()...getMax()...ܯى۞͵Ռܯى۞ 55 դૡդӞͣդૡդӞͣLILOLILOͤͤ 55 ҮૡҮӞͣҮૡҮӞͣFIFO)FIFO) frontfrontͤӬୣոߓੳͤӬୣոߓੳ dequeue()/front()dequeue()/front()ͣעՐৗ֧୓ͣעՐৗ֧୓ rearrearܷͤҴܷͤҴ enqueue()enqueue()ͣـՐৗ֧୓ͣـՐৗ֧୓ ӦٽӦࣩٽՈୡՈୡࣩފЬͤފ55 ୓Ӧ୓ӦͣͣqueuequeueͤЬ ՌܯՌݒҀЉܯݒҀЉ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 280 vv rr subtree(v) height(T) height(v) depth(v) ։▁ڰ։▁ࣩդіЈ੎ڰvvࣩդіЈ੎ ։▁ڶӨ֧؁։▁ࣩढҮਂڶӨ֧؁vvࣩढҮਂ ͵څџ▁࡬֧͵څ55 ֧џ▁࡬ properproperͤढҮͤढҮͣͣँँފࣩढҮͧטୣ১ાјފࣩढҮͧט55 ୣ১ાј ؈њࣩդіդіͣͣdescendentdescendentͤͤފ؈њࣩފvv 55 path(vpath(v))Іৼ䎯֭НІৼ䎯֭НvvࣩࣩढҮढҮͣͣancestorancestorͤͤ depthdepthͤͤͣͣڅ࡬څवҀवҀvvࣩࣩ࡬ 55 depth(vdepth(v)=|)=|path(vpath(v)|)| ੢Ҁ੢Ҁpath(vpath(v,r)=,r)=path(vpath(v)) ګષګ։▁ષ֧؁▁։֧؁55 ݜͧџ▁ৼ䎯ݜͧџ▁ৼ䎯vvЉߠУ୎ЉߠУ୎ ݸࢺ֢לݸࢺ֢߃ל૤ૹ֢ ==߃س૤ૹ֢߃س55 ߚߚ ==ݸࢺ૤ૹ֢ݸࢺ૤ૹ֢ ==߃ ࠊقЉڅࠊ࡬قЉڅ࡬ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 279 kk 11 22 00 Јլࢺષࣩ֢ͧवҀЈլࢺષࣩ֢ͧवҀݸࢺ֢ݸࢺ֢ͣͣacyclicgraphacyclicgraphͤͤ ࣩ֢ͧवҀ૤ૹ֢૤ૹ֢ͣͣconnectedgraphconnectedgraphͤͤګષޥৼ䎯У୎֭ڿࣩ֢ͧवҀџګષޥৼ䎯У୎֭ڿ55 џ vvkk =v=v00ͧӨवҀͧӨवҀࢺષࢺષͣͣcycle/loopcycle/loopͤͤਂਂ څࣩݧࢳјৼ䎯ݤНେޥڅࣩݧࢳјৼ䎯ݤНେޥ//// څେګवҀષڅେګ|=k|=kवҀષ || ={(v={(v00,v,v11),(v),(v11,v,v22),......,(v),......,(vkk11,v,vkk)})} pathpathͤͤͣͣګષګ޻ષ▁ۉࣼ৊ͧ߄ـ޻޻૕ந▁ۉࣼ৊ͧ߄ـ55 VVЗࣩЗࣩk+1k+1Жৼ䎯ͧૹ૚Жৼ䎯ͧૹ૚EEЗࣩЗࣩkk޻૕ந ЉࢺષګЉࢺષષګષ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 278 ............ rr11 rr22 rrdd rr subtree(rsubtree(r11)) subtree(rsubtree(r22)) subtree(rsubtree(rdd)) ߚͣͣorderedtreeorderedtreeͤͤٽޥߚٽޥӨӨTTवҀवҀ TTiiҀНҀНTTࣩॕࣩॕii㱾׾ߚͧ㱾׾ߚͧrriiҀНҀНrrࣩॕࣩॕiiЖЖ؆׾؆׾ؓܗਂؓܗਂ 55 ݿͧՔјnnҀНԿ࢑ҀНԿ࢑څ޳וݿͧՔјݜ֧਺ଞࣼһڅ޳וݜ֧਺ଞࣼһ ڡԮৼ䎯ݤЉ૕ݤࣼͧڡݤ㔀ո =n=n11ͧԮৼ䎯ݤЉ૕ݤࣼڅݤ㔀ոৼ䎯څm=m=ৼ䎯͵ރঈ੩ڠՔ͵ރঈ੩ڠ55 Ք degreedegreeͤͤͣͣڅڅrrНҾНҾ࢛щ࢛щͣͣparentparentͤͧͤͧd=d=degree(rdegree(r))ННrrࣩͣӞࣩͤͣӞͤ siblingsiblingͤͤͣͣڗҫڗ55 rriiवҀवҀrrࣩࣩ؆׾؆׾ͣͣchildchildͤͧͤͧrriiУ୎кवУ୎кवҫ ߚٽޥߚٽޥ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 277 ............ rr11 rr22 rrdd rr subtree(rsubtree(r11)) subtree(rsubtree(r22)) subtree(rsubtree(rdd)) ੢Ҁ੢ҀTTii ==subtree(rsubtree(rii)) иTTͧͧTTiiवҀјवҀјrriiНߠࣩНߠࣩ׾ߚ׾ߚͣͣsubtreerootedatrsubtreerootedatriiͤͤتиࣼتࣼ 55 ފЬފӨӨT=((T=((%%VVii))%%{r},({r},(%%EEii))%%{|1>|1 ii d})d})Ь ߠߚޥߠߚНޥ55 ਂਂTT11,T,T22,...T,...TddН ߠߚͣͣrootedtreerootedtreeͤͤޥߠߚޥџ▁џ▁ৼ䎯ৼ䎯rr++ VVҀНҀНߠդͧߠդͧTTԮवҀԮवҀؓܗؓܗ 55 ࢤࣩ֢ࠛT=(V,E)T=(V,E)ͧৼ䎯ݤͧৼ䎯ݤ|V|=n|V|=nͧ૕ݤͧ૕ݤ|E|=m|E|=mފࢤࣩ֢ࠛߚފ55 ߚ ߠߚޥߠߚޥ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 276 SlytherinSlytherin RavenclawRavenclaw HufflepuffHufflepuff GryffindorGryffindor HogwartsHogwarts yahoo amazon ...googlefacebook frcn dk edu org com ... root)). ڪ঎㓬ࢤޥУդͧҿڪ঎㓬ࢤޥУդͧҿٽߏमࠊٽ֧ग֧ؓगؓߏमࠊ फ़Ԥࣩ঎㓬গ߄ͧѷފफ़Ԥࣩ঎㓬গ߄ͧѷЈӆފ55 Ԡ঎㓬͵Ԡ঎㓬͵ Јӆ ૻܷҴܷҴ澝澝ӬୣӬୣڼૻтՔڼ2.2. тՔ ૻߓۨߓۨڼૻݹՔڼ1.1. ݹՔ VectorVectorոոListListࣩѨ䎯ࣩѨ䎯ۉ୳ۉ55 ণ՟㓬͵ণ՟㓬͵ ୳ ...... InternetURLInternetURL ૚सڠ૚सӠݤઁ࣐ո૶ڠӠݤઁ࣐ո૶ ݧѝॸঞݧѝॸঞ ړ਽૖ړ਽૖ ࠊһॸࣩ਽ठقࠊһॸࣩ਽ठق 55 ࣐ڀ࣐ڀ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 275 RobertFrost,1915 AndsorryIcouldnottravelboth Tworoadsdivergedinayellowwood (a)(a)ࠃ૩ࠃ૩ 5.5.зՁߚзՁߚ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 274 QQ PP QueapQueap =Queue+Heap=Queue+Heap =enqueue+dequeue+getMax=enqueue+dequeue+getMax DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 273 PP SS SteapSteap =Stack+Heap=Stack+Heap =push+pop+getMax=push+pop+getMax DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 288 (c)(c)зՁߚࠃ૩зՁߚࠃ૩ 5.5.зՁߚзՁߚ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 287 GG HH KK DD EE FF AA BB CC RR ՌЬэ୻OO(1)(1)ݿ୎ݿ୎ܯՌЬэ୻ܯ()࣐ͧӨparent()parentڕ࣐ͧӨڕ55 ਂӆ੦঴ਂӆ੦঴parentparent ؆׾ޥے؆׾ݿ୎ӃଁԶҾޥےݤНddࣩৼ䎯ͧՔ֧ࣩৼ䎯ͧՔ֧OO(d+1)(d+1)ݿ୎ӃଁԶҾڅиتࠕͧױݤНڅиتࠕͧױ 55 ࠇ͵ࠇ͵nextSiblingnextSibling()() উ͵উ͵firstChildfirstChild()() ࣐ڕ࣐ࠠЖৼ䎯֭੦ГЖڕࠠЖৼ䎯֭੦ГЖ 55 ڗҫڗେ׾େ׾ ++ҫ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 286 7 8 9 ^ 0 1 2 ^ 6 ^ 3 5 ^ 99 KK 66 ^^ 88 HH 66 ^^ 77 GG 66 ^^ 66 FF 22 55 EE 00 ^^ 44 RR 11 33 DD 00 ^^ 22 CC 44 11 BB 44 ^^ 00 AA 44 renren nodenode parentparent childchild G H K D E F A B C R ݘૡ͵࢛ৼ䎯ݘૡ͵࢛ৼ䎯 ++؆׾ৼ䎯؆׾ৼ䎯 55 ۄڭԯۄڭӞͧѷparent()parent()ԯۨڼڭ؆׾଒ՔޥےӞͧѷࢻ֧ͧۨڼڭ؆׾଒Քޥے55 ࢻ֧ͧ ࢛ৼ䎯࢛ৼ䎯 ++؆׾ৼ䎯؆׾ৼ䎯 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 285 7 8 9 ^ 0 1 2 ^ 6 ^ 3 5 ^ 99 KK ^^ 88 HH ^^ 77 GG ^^ 66 FF 55 EE ^^ 44 RR 33 DD ^^ 22 CC 11 BB ^^ 00 AA renren nodenode childchild G H K D E F A B C R ݤڅӸࣩڡৼ䎯ڀتӢӯॗиڅݤ࣐҄Ӧ਽ͧӦ਽ࣩେڅӸࣩڡৼ䎯ڀتӢӯॗиڅ55 ࣐҄Ӧ਽ͧӦ਽ࣩେ  ी୎࣐҄ࢴी୎࣐҄ࢴ =(n=(n1)1) /(n/(n d)<1/dd)<1/d ݤddڅלޣঞ▁Нڅݤ࣐҄ЈՔ۞Ҭࣩզଞͧզଞࣩେڅלޣঞ▁Нڅ55 ࣐҄ЈՔ۞Ҭࣩզଞͧզଞࣩେ ࣐ڕ؆׾ࣩޥےݚҾ؁Ӧٽ࣐ࠠЖৼ䎯՞࣐▁Жڕ؆׾ࣩޥےݚҾ؁Ӧٽ55 ࠠЖৼ䎯՞࣐▁Ж ؆׾ৼ䎯؆׾ৼ䎯 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 284 GG HH KK DD EE FF AA BB CC RR 99 KK 66 88 HH 66 77 GG 66 66 FF 33 55 EE 11 44 DD 11 33 CC 00 22 BB 00 11 AA 00 00 RR 11 parent parent node node rank rank ࣩߓۨͺڗ؆׾澝ҫتѽԅૻױࣩߓۨͺڗ؆׾澝ҫتѽԅૻױ 55  nextSiblingnextSibling():(): OO((nn))  firstChildfirstChild()()͵͵ OO((nn)) OO(1)(1)ۋۋ( root()root()͵͵ OO(n)(n  parent()parent()͵͵ OO(1)(1) 55 ݿ୎㓬ৗݿ୎㓬ৗ 55 ी୎㓬ৗ͵ी୎㓬ৗ͵OO(n)(n) NULLNULLۋۋЖ““਩߄ࣩ਩߄ࣩ””࢛ৼ䎯࢛ৼ䎯11▁ޥЖ▁ޥ55 ߚߠߚߠЬЬ Ѹ঴ۋѸ঴࢛ৼ䎯ࣩ䰹ۋparentparent͵͵ ࢛ৼ䎯ࣩ䰹 nodenode͵͵ ޯાޯાࣩґ㔴ࣩґ㔴ͧјՂͧјՂ ؁Ґ؁Ӧͧ՞ৼ䎯ৼ䎯Ґٽৼ䎯ঐ঒Н▁Жذ͵Ӧͧ՞߄㓢ٽৼ䎯ঐ঒Н▁Жذ͵55 ߄㓢 Ж࢛ৼ䎯▁ޥЋэޥџ▁ৼ䎯ͧטୣߠ͵اЖ࢛ৼ䎯੏▁ޥЋэޥџ▁ৼ䎯ͧטୣߠ͵ا55 ੏ ࢛ৼ䎯࢛ৼ䎯 DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 283 traverse()traverse() ଁԶଁԶ remove(iremove(i)) ӬୣॕӬୣॕiiЖ؆׾ͣՂҾդіͤЖ؆׾ͣՂҾդіͤ eeҀНॕҀНॕiiЖ؆׾ܷҴЖ؆׾ܷҴذذ (insert(iinsert(i,e),e ڗҫڗnextSiblingnextSibling()() ҫ firstChildfirstChild()() େ׾େ׾ parent()parent() ࢛ৼ䎯࢛ৼ䎯 root()root() ߠৼ䎯ߠৼ䎯 ৼ䎯ৼ䎯 ԄৗԄৗ ՌܯՌܯ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 282 (b)(b)ߚࣩ਽ठߚࣩ਽ठ 5.5.зՁߚзՁߚ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 281 vv rr subtree(v) height(T) height(v) depth(v) іܗ澝ৼ䎯ո׾ߚՔࣼкګіષܗ澝ৼ䎯ո׾ߚՔࣼкګષ 55 Ј৩࠘ТݿЈ৩࠘Тݿ ѽݿՇॗՙͺѽݿՇॗՙͺ 55 depth(vdepth(v)+)+height(vheight(v)) height(Theight(T)) ՇҀ11څՇҀࢤӯ֩ͧीߚࣩளڅ55 ࢤӯ֩ͧीߚࣩள height(vheight(v)=)=height(subtree(vheight(subtree(v)))) heightheightͤͤͣͣڅளڅ׾ͤߚߠৼ䎯ࣩளͣފ׾ͤߚߠৼ䎯ࣩЬͣފЬ څளڅवҀͣ׾ͤߚࣩளڅ࡬לޣवҀͣ׾ͤߚࣩ՘׾ࣩڅ࡬לޣ55 ՘׾ࣩ դіࣩৼ䎯वҀ՘׾ͣleafleafͤͤޥդіࣩৼ䎯वҀ՘׾ͣ࠻ޥ࠻ Н00څৼ䎯ࣩͣҷҺͤढҮͧ࡬ޥےފНߠৼ䎯څৼ䎯ࣩͣҷҺͤढҮͧ࡬ޥےފ55 ߠৼ䎯 ࠊقЉڅࠊ࡬قЉڅ࡬ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 296 RC parent ^^ }} returns;returns; ਗ਼Ҵ՗׾ߚ੐ࠆڠਗ਼Ҵ՗׾ߚ੐ࠆ૶ڠif(rChild)s+=rChildif(rChild)s+=rChild>size();>size();////૶ ਗ਼Ҵٗ׾ߚ੐ࠆڠਗ਼Ҵٗ׾ߚ੐ࠆ૶ڠif(lChild)s+=lChildif(lChild)s+=lChild>size();>size();////૶ ints=1;ints=1;////ਗ਼Ҵޯાਗ਼Ҵޯા intintBinNodeBinNode::::sizesize(){(){////դі㔀ݤͧтԮјҾНߠࣩ׾ߚࣩ੐ࠆդі㔀ݤͧтԮјҾНߠࣩ׾ߚࣩ੐ࠆ 55 templateT> {returnrChild=new{returnrChild=newBinNodeBinNode(e(e,this);},this);} BinNodePosi(TBinNodePosi(T))BinNodeBinNode::::insertAsRCinsertAsRC(T(T const&e)const&e) 55 templateT> {returnlChild=new{returnlChild=newBinNodeBinNode(e(e,this);},this);} BinNodePosi(TBinNodePosi(T))BinNodeBinNode::::insertAsLCinsertAsLC(T(T const&e)const&e) 55 templateT> ՌؗࢻܯՌؗࢻܯBinNodeBinNode DataStructures&Algorithms(Fall2012),TsinghuaUniversity 2 295 height|npl|colorheight|npl|color rChildrChildparentparent parentparent rChildrChild lChildlChild lChildlChild datadata datadata };}; ԶଁٽԶ׾ߚդଁٽtemplatevoidVST>voidtravPosttravPost(VST(VST&);&);////׾ߚդ ԶଁٽԶ׾ߚЗଁٽtemplatevoidVST>voidtravIntravIn(VST(VST&);&);////׾ߚЗ ԶଁٽԶ׾ߚҮଁٽtemplatevoidVST>voidtravPretravPre(VST(VST&);&);////׾ߚҮ ࠊଁԶقࠊଁԶ׾ߚقtemplatevoidVST>voidtravLeveltravLevel(VST(VST&);&);////׾ߚ դটܯӸৼ䎯ࣩࣻڡТЇͤڿԶଁٽդটͣЗܯӸৼ䎯ࣩࣻڡТЇͤڿԶଁٽBinNodePosi(TBinNodePosi(T))succsucc();();////ͣЗ BinNodePosi(TBinNodePosi(T))insertAsRCinsertAsRC(T(T const&);const&);////ҀН՗؆׾ܷҴݰৼ䎯ҀН՗؆׾ܷҴݰৼ䎯 BinNodePosi(TBinNodePosi(T))insertAsLCinsertAsLC(T(T const&);const&);////ҀНٗ؆׾ܷҴݰৼ䎯ҀНٗ؆׾ܷҴݰৼ䎯 澝׾ߚ੐ࠆڅ澝׾ߚ੐ࠆளڅintheight;intintheight;intsizesize();(); ////ள Tdata;Tdata;BinNodePosi(TBinNodePosi(T)parent)parent,,lChildlChild,, rChild;rChild; ////࢛щ澝؆׾࢛щ澝؆׾ public:public: classclassBinNodeBinNode {{ 55 templateT> BinNodeBinNodeࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 1 294 (d)(d)зՁߚؗࢻзՁߚؗࢻ 5.5.зՁߚзՁߚ JKTM&ZYOTMN[GKJ[ITJKTM&ZYOTMN[GKJ[IT ݯ૱বݯ૱ব 293 KKKK KK HH HH HH GG GG GG FF FF FF EE EE EE DD DD DD CC CC CC BB BB BB AA AA AA RR RR RR nextSiblingnextSibling()=rChild()()=rChild() firstChildfirstChild()=lChild()()=lChild() ڀتЉٗ澝՗؆׾ॗݝ֩ࣼкڗେ׾澝ҫذ͵څڀتЉٗ澝՗؆׾ॗݝ֩ࣼкڗେ׾澝ҫذ͵څ55 НࠕՐ୻ݵૃНࠕՐ୻ݵૃ4545 ਽ठࡄ਽ठࡄ......””ڗҫڗେ׾େ׾ҫ““ڷ֚ڷ֚ ———— ਽ठНзՁߚٶՁߚ֭ՔૃԖי਽ठНзՁߚؗଢ଼ІͧٶՁߚ֭ՔૃԖי55 ؗଢ଼Іͧ Ҿܵ૩ৗԂԯરј੍ࣴդীҾܵ૩ৗԂԯરј੍ࣴդী ݿݿٽޥٽޥߠЋЋޥߠޥՁߚࣩࢤ҅ͧѷ֧יފՁߚࣩࢤ҅ͧѷ֧зՁߚיފ55 зՁߚ ՁߚיՁߚܵ૩יܵ૩ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 292 2200 2200 2200 2222 2211 2222 0000 0000 00 00 00 иঀுߚঀுߚУ६ࣩগ߄ͧँзՁߚՔјफ़Ԗܵ૩澝ࣂ੕澝ؗࢻոӢ߅У६ࣩগ߄ͧँзՁߚՔјफ़Ԗܵ૩澝ࣂ੕澝ؗࢻոӢ߅تиت 55 ؗઔ׌ԅޭٶڅ޳וУդͧҵߚ১ાࣩܦࠕૃױ͵ؗઔ׌ԅய੩ޭٶڅ޳וУդͧҵߚ১ાࣩܦࠕૃױ͵55 ய੩ џ▁зՁߚૃԖНँзՁߚँзՁߚͣͣproperbinarytreeproperbinarytreeͤͤذࠕͧԮՔױџ▁зՁߚૃԖНذࠕͧԮՔױ ݤঞ▁Н22څৼ䎯ޥ଑ৼ䎯ͧՔ҄Լטݤঞ▁НЖڅৼ䎯ޥ଑ৼ䎯ͧՔ҄ԼטҴnn11 +2n+2n00ЖڕҴૹ૚ڕ55 ૹ૚ ँзՁߚँзՁߚ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 291 0000 22 00 00 11 ݤ֭НҡݤͧЈլԤӢݖৼ䎯......څݤ֭НҡݤͧЈլԤӢݖৼ䎯ࠕݿͧৼ䎯څࠕݿͧৼ䎯 e=2ne=2n22 ոո nn00 =n=n22 +1=(n+1)/2+1=(n+1)/2 ޥݿͧޥnn11 =0=0ݿͧڡࢤӯ֩ͧڡ55 ࢤӯ֩ͧ ৼ䎯ݤৼ䎯ݤ n=nn=n00 +n+n11 +n+n22 =1+n=1+n11 +2n+2n22 =1+e=1+e ՘ৼ䎯ݤ՘ৼ䎯ݤ nn00 =n=n22 +1+1 ૕ݤ૕ݤ e=ne=n 1=n1=n11 +2n+2n22 nn00澝澝nn11ոոnn22ЖͧӨЖͧӨޥࣩৼ䎯՞ޥݤН00澝澝11ոո22ࣩৼ䎯՞څݤН੦څ55 ੦ ׁݤׁݤ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 290 h cc cc oo oo mm mm kk kk ii ii gg gg ee ee bb bb aa aa nn nn jj jj ff ff ll ll dd dd hh hh ઄ࢁзՁߚࢁзՁߚͣͣfullbinarytreefullbinarytreeͤͤے઄ݿͧԮے22ͤͤn=2n=2h+1h+1 11ݿͧԮ 11ͤͤn=h+1n=h+1ݿͧ૭ԖН▁޻Ԥ଴ݿͧ૭ԖН▁޻Ԥ଴ 55 h_size=x>_size=x>>sizesize();_size();_size=S=S>_size;>_size SS>_root=x;x>_root=x;x>parent=NULL;>parent=NULL;////ݰߚјݰߚјxxНߠНߠ ीߚڎीߚӪڎBinTreeBinTree*S=new*S=newBinTreeBinTree;;////Ӫ ੇخӢनӞࣩ׾ߚҞتјЇੇخӢनӞࣩ׾ߚҞتјЇ//// څढҮࣩளޥےݰԼߚЗޟڅढҮࣩளޥےݰԼߚЗޟ////;(updateHeightAboveupdateHeightAbove(x(x>parent);>parent ଢܗଢӣݮ޼১࢛ৼ䎯ࣩܗFromParentToFromParentTo(*x)=NULL;(*x)=NULL;////ӣݮ޼১࢛ৼ䎯ࣩ BinTree*BinTree*BinTreeBinTree::::secedesecede(BinNodePosi(T(BinNodePosi(T)x){)x){ 55 templateT> ࣐ীઁق૞֚চІٶͧੇخӢनӞ޼ࣩ׾ߚଜݰتиͧ୻֧ד࣐ীЈբУઁق૞֚চІٶͧੇخӢनӞ޼ࣩ׾ߚଜݰتиͧ୻֧דЈբУ 55 ૚सЉјІࣩ׾ߚӬୣݒҀ૚सЉјІࣩ׾ߚӬୣݒҀBinTreeBinTree::::removeremove()()ׁޯ▁৩ׁޯ▁৩ ׾ߚӢन׾ߚӢन DataStructures&Algorithms(Fall2012),TsinghuaUniversity 8 301 }} returnn;returnn;////૞֚੃Ӭୣৼ䎯㔀ݤ૞֚੃Ӭୣৼ䎯㔀ݤ ٶଚݚ੃݅ୣৼ䎯ͧٶreleaserelease(x(x>data);>data);releaserelease(x(x););////ଚݚ੃݅ୣৼ䎯ͧ intn=1+intn=1+removeAtremoveAt(x(x>lChild)+>lChild)+removeAtremoveAt(x(x>rChild);>rChild); ڠওࠓиी׾ߚͧժӨٗ澝՗૶ڠif(!x)return0;if(!x)return0;////ওࠓиी׾ߚͧժӨٗ澝՗૶ 55 templateT> staticintstaticintremoveAtremoveAt(BinNodePosi(T(BinNodePosi(T)x){)x){ }} returnn;returnn;////૞֚੃Ӭୣৼ䎯㔀ݤ૞֚੃Ӭୣৼ䎯㔀ݤ ݰ੐ࠆޟݰ੐ࠆՂҾդіͧޟӬୣxxՂҾդіͧڠӬୣ૶ڠintn=intn=removeAtremoveAt(x(x);_size);_size=n;=n;////૶ ҾѾৼ䎯тЈՉͤͣҾѾৼ䎯тЈՉͤͣڅݰढҮளޟڅݰढҮளޟ////;(updateHeightAboveupdateHeightAbove(x(x>parent);>parent ଢܗଢӣݮ޼১࢛ৼ䎯ࣩܗFromParentToFromParentTo(*x)=NULL;(*x)=NULL;////ӣݮ޼১࢛ৼ䎯ࣩ Ҵࣩ૱૚सܯҴࣩ૱૚स׾ߚܯintBinTree::intBinTree::removeremove(BinNodePosi(T(BinNodePosi(T)x){)x){ ////׾ߚ 55 templateT> ׾ߚӬୣ׾ߚӬୣ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 7 300 T ^LC SS parent (b) (a) TT SS LC parent व T'T'تवت()()}}////attachAsLCattachAsLC ҴѸ঴ܯҴѸ঴૞֚ܯreturnx;returnx;////૞֚ releaserelease(S(S);S=NULL;);S=NULL;////ଚݚଚݚSS SS>_root=NULL;S>_root=NULL;S>_size=0;>_size=0; څݰ੐ࠆՂढҮளޟڅݰ੐ࠆՂढҮளޟ////;(;(_size+=S_size+=S>_size;>_size;updateHeightAboveupdateHeightAbove(x(x ҴܯҴܯ////;if(xif(x>rChild>rChild== SS>_root)x>_root)x>rChild>rChild>parent=x;>parent=x attachAsRCattachAsRC(BinNodePosi(T(BinNodePosi(T)x,)x,BinTreeBinTree*&S){*&S){ 55 templateT>BinNodePosi(TBinNodePosi(T))BinTreeBinTree:::: ҴܯҴ׾ߚܯ׾ߚ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 6 299 ^^ ^ ^LC parent (a) (b) TT LC parent ^^ T'T' }} returnxreturnx>rChild;>rChild; ࢐ЈՉЈՉڶ࢐׌ԅͧҾѾৼ䎯ڶՔৗՔৗ׌ԅͧҾѾৼ䎯څढҮࣩளڅupdateHeightAboveupdateHeightAbove(x(x);); //x//xढҮࣩள _size++;x_size++;x>>insertAsRCinsertAsRC(e(e);); वتवت()insertAsRCinsertAsRC(BinNodePosi(T(BinNodePosi(T)x,Tconst&e))x,Tconst&e) {{ //insertAsLC()//insertAsLC 55 templateT> BinNodePosi(TBinNodePosi(T))BinTreeBinTree:::: ৼ䎯ܷҴৼ䎯ܷҴ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 5 298 }} {{updateHeightupdateHeight(x(x);x=x);x=x>parent;}>parent;} ՉͧԮՔওࠓޭڅՉͧԮՔওࠓՔѨԖ͵▁ݻளޭڅwhile(x)while(x) ////ՔѨԖ͵▁ݻள voidvoidBinTreeBinTree::::updateHeightAboveupdateHeightAbove(BinNodePosi(T(BinNodePosi(T)x))x) {{ څՂҾԶіढҮࣩளڅݰvvՂҾԶіढҮࣩளޟݰޟ////<55 templateT }} 1+1+max(stature(xmax(stature(x>lChild),>lChild),stature(xstature(x>rChild));>rChild)); returnreturn xx>height=>height= intintBinTreeBinTree::::updateHeightupdateHeight(BinNodePosi(T(BinNodePosi(T)x))x) {{ ڐҿѼ੐Ө֛ߚЈբুͧڅளڐҿѼ੐Ө֛ߚЈբুͧڅݰৼ䎯xxளޟݰৼ䎯ޟ////<55 templateT Н11څНংؓीߚளڅংؓीߚள ———— څৼ䎯ளڅ////ৼ䎯ள 55 #define#definestature(pstature(p)((p)?(p))((p)?(p)>height:>height:1)1) ݰޟڅݰளޟڅள DataStructures&Algorithms(Fall2012),TsinghuaUniversity 4 297 }} Ռ ...*/...*/ܯՌଁԶܯ/*.../*...ଁԶ Ռ ...*/...*/ܯҴ澝ӬୣոӢनܯՌ׾ߚܯҴ澝ӬୣոӢनܯ/*.../*...׾ߚ BinNodePosi(TBinNodePosi(T)&root(){return_root;})&root(){return_root;}////ߚߠߚߠ boolempty()const{return!_root;}boolempty()const{return!_root;}////ӭीӭी int&size(){return_size;}int&size(){return_size;}////੐ࠆ੐ࠆ public:public: څՂढҮࣩளڅݰxxՂढҮࣩளޟݰޟ////;(voidvoidupdateHeightAboveupdateHeightAbove(BinNodePosi(T(BinNodePosi(T)x);)x څࣩளڅݰৼ䎯xxࣩளޟݰৼ䎯ޟ////;(virtualintvirtualintupdateHeightupdateHeight(BinNodePosi(T(BinNodePosi(T)x);)x BinNodePosi(TBinNodePosi(T)_root;)_root;////ߠৼ䎯ߠৼ䎯 int_size;int_size;////੐ࠆ੐ࠆ protected:protected: 55 templateclassT>classBinTreeBinTree {{ BinTreeBinTreeࠆ߂६ࠆ߂६ DataStructures&Algorithms(Fall2012),TsinghuaUniversity 3 312 }} }} ࣩફ䎯ۦ▁ӞЇڞࣩફ䎯ۦ▁ӞЇڞ//// ;()if (if (S.emptyS.empty()) break; ()) break; xx = S.pop(); = S.pop Ӹৼ䎯xxӞՆӞՆڡё֭ۦӸৼ䎯ࠠڡё֭ۦvvisitAlongLeftBranchisitAlongLeftBranch((xx, visit, S); , visit, S); ////ࠠ ੧ୋৼ䎯ͧࣻ২ોԈߘीৼ䎯ͧࣻ২ોԈߘीۦ੧ୋ૵ۦwhile (true) {while (true) { ////૵ StackStack< S;)> S; ////ોԈߘોԈߘ 55 void void travPre_I2travPre_I2(BinNodePosi(T) (BinNodePosi(T) xx, VST& visit) {, VST& visit) { }} { { visit(xvisit(x-->data); >data); S.push(xS.push(x-->rChild); x = x>rChild); x = x-->lChild; >lChild; }} Ӟߘͤٽ૱޼ذࡀ૷՗؆׾҈ࠊҴߘͣͧځӞߘͤࡀٗҊ଴੧ୋ২ٽ૱޼ذࡀ૷՗؆׾҈ࠊҴߘͣͧځwhile (x)while (x) ////ࡀٗҊ଴੧ୋ২ ((BinNodePosi(TBinNodePosi(T) x, VST& visit, ) x, VST& visit, StackStack<)>&& S) {S) { static void static void vvisitAlongLeftBranchisitAlongLeftBranch ////Ӣ㢉Ӣ㢉O(1)O(1) 55 template