diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214213333370_9161.png b/▼配套习题解析/▼09 查找/_v_images/20181214213333370_9161.png new file mode 100644 index 0000000..bf261ff Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214213333370_9161.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214213408549_8068.png b/▼配套习题解析/▼09 查找/_v_images/20181214213408549_8068.png new file mode 100644 index 0000000..1f73e1b Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214213408549_8068.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214213449099_15405.png b/▼配套习题解析/▼09 查找/_v_images/20181214213449099_15405.png new file mode 100644 index 0000000..73d1ec3 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214213449099_15405.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214213551073_28565.png b/▼配套习题解析/▼09 查找/_v_images/20181214213551073_28565.png new file mode 100644 index 0000000..05405db Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214213551073_28565.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214213924381_20066.png b/▼配套习题解析/▼09 查找/_v_images/20181214213924381_20066.png new file mode 100644 index 0000000..185a6b1 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214213924381_20066.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214042887_18269.png b/▼配套习题解析/▼09 查找/_v_images/20181214214042887_18269.png new file mode 100644 index 0000000..96a4cfa Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214042887_18269.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214525317_23291.png b/▼配套习题解析/▼09 查找/_v_images/20181214214525317_23291.png new file mode 100644 index 0000000..a874a12 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214525317_23291.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214606440_10337.png b/▼配套习题解析/▼09 查找/_v_images/20181214214606440_10337.png new file mode 100644 index 0000000..c63e9bd Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214606440_10337.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214633281_23456.png b/▼配套习题解析/▼09 查找/_v_images/20181214214633281_23456.png new file mode 100644 index 0000000..547be20 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214633281_23456.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214720912_4862.png b/▼配套习题解析/▼09 查找/_v_images/20181214214720912_4862.png new file mode 100644 index 0000000..b59f5c9 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214720912_4862.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214910449_20293.png b/▼配套习题解析/▼09 查找/_v_images/20181214214910449_20293.png new file mode 100644 index 0000000..55655fb Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214910449_20293.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214214939408_12857.png b/▼配套习题解析/▼09 查找/_v_images/20181214214939408_12857.png new file mode 100644 index 0000000..82a9612 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214214939408_12857.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214215141119_17719.png b/▼配套习题解析/▼09 查找/_v_images/20181214215141119_17719.png new file mode 100644 index 0000000..8c3ce9e Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214215141119_17719.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214215223320_3867.png b/▼配套习题解析/▼09 查找/_v_images/20181214215223320_3867.png new file mode 100644 index 0000000..5eb5770 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214215223320_3867.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214215634299_17793.png b/▼配套习题解析/▼09 查找/_v_images/20181214215634299_17793.png new file mode 100644 index 0000000..3592c6c Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214215634299_17793.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214220147072_20491.png b/▼配套习题解析/▼09 查找/_v_images/20181214220147072_20491.png new file mode 100644 index 0000000..324dc41 Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214220147072_20491.png differ diff --git a/▼配套习题解析/▼09 查找/_v_images/20181214220301460_10605.png b/▼配套习题解析/▼09 查找/_v_images/20181214220301460_10605.png new file mode 100644 index 0000000..eb8d50c Binary files /dev/null and b/▼配套习题解析/▼09 查找/_v_images/20181214220301460_10605.png differ diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_25.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_25.txt new file mode 100644 index 0000000..4a1fc8f --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_25.txt @@ -0,0 +1 @@ +200.0190.0180.0170.0160.0150.0140.0130.0120.0110.0100.090.080.070.060.050.040.030.020.010.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_26-9_27.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_26-9_27.txt new file mode 100644 index 0000000..580a37d --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_26-9_27.txt @@ -0,0 +1 @@ +10.020.030.040.050.060.070.080.090.0100.0110.0120.0130.0140.0150.0160.0170.0180.0190.0200.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_28.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_28.txt new file mode 100644 index 0000000..a5cd47b --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_28.txt @@ -0,0 +1 @@ +10.020.040.060.070.0110.0130.0150.0170.0190.0230.0250.0310.0330.0350.0380.0400.0440.0520.0590.0660.0680.0690.0720.0740.0890.0910.0930.0970.0990.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_29.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_29.txt new file mode 100644 index 0000000..59f5ae2 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_29.txt @@ -0,0 +1 @@ +1246711131517192325313335384044525966686972748991939799 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_30.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_30.txt new file mode 100644 index 0000000..59f5ae2 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_30.txt @@ -0,0 +1 @@ +1246711131517192325313335384044525966686972748991939799 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_31-9_34.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_31-9_34.txt new file mode 100644 index 0000000..9052c3e --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_31-9_34.txt @@ -0,0 +1 @@ +610.0780.092, 0.024 0.037 0.081 0.045 0.03 0.053 0.012 0.0-92 0.0400.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_35-9_37.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_35-9_37.txt new file mode 100644 index 0000000..f3e60a5 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_35-9_37.txt @@ -0,0 +1 @@ +240.0450.050, 0.0530.01000.037 0.0120.0610.0900.070 0.030.0300.0260.0850.070.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_38-9_39.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_38-9_39.txt new file mode 100644 index 0000000..93f51d1 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_38-9_39.txt @@ -0,0 +1,2 @@ +450.0240.053, 0.045 0.01 0.012 0.024 0.090 0.0 +610.0780.0100, 0.024 0.037 0.090 0.045 0.03 0.053 0.012 0.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_40.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_40.txt new file mode 100644 index 0000000..f0d2c15 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_40.txt @@ -0,0 +1 @@ +40.020.01, 0.07 0.08 0.05 0.06 0.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_41.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_41.txt new file mode 100644 index 0000000..f3e60a5 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_41.txt @@ -0,0 +1 @@ +240.0450.050, 0.0530.01000.037 0.0120.0610.0900.070 0.030.0300.0260.0850.070.0 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_42-9_43.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_42-9_43.txt new file mode 100644 index 0000000..f5a1d36 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_42-9_43.txt @@ -0,0 +1 @@ +CAICAOLILANCHACHANGWENCHAOYUNYANGLONGWANGZHAOLIUWUCHEN \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_44.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_44.txt new file mode 100644 index 0000000..407a6b7 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_44.txt @@ -0,0 +1 @@ +ZHAOQIANSUNLIZHOUWUZHENWANGFENGCHENCHUWEIJIANGSHENHANYANGZHUQINYOUXU \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_45.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_45.txt new file mode 100644 index 0000000..7a8bc5f --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_45.txt @@ -0,0 +1 @@ +19,14,23,01,68,20,84,27,55,11,10,79 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_46.txt b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_46.txt new file mode 100644 index 0000000..6f66c69 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Data/Algo_9_46.txt @@ -0,0 +1 @@ +769736729532399625561963165102314895501396816580585837733562774159865664090576224614646464282335892623213621044642142675828438657958648487786780762937142953986921195463827697646983708912609154996445743805897664674546950863423597431107822542184286358361112292495405225395558081687625156882635044542247645231183735497642409459384816935672526883298457443854959582790583881648886815349223585017854819371326064858841779375160386382978548979848947469927337735847488059838786329237915362579319116337907958925554055007632181259521971873148269604942415102654136853218594261891998125427004745421869564296365967985309547891264054208878916054611356226472561761429345794487342267831209669724612176936191392335488544751235462616328575189257528916211730991740771351151715227869570721478893130299871214431921396810505445156424943506593532983468889996498505603232445377998422345073918412251223566459093955327632994794925318397496282274377792457054178120678651382715555875471595079876431871744837572254151274789442082687166109202887702763072647661893018483405238316627694599934767783337142983893203210130205793960732787852172237578889420113388912246496474908850476454944381425921433377053516757802039839964559981395878944500996297038241085379854066229843555779658439245555977631811889623708349794302273317092219273808833004988102563778059445726284298962274785401253303859587284502881423446932037876282569635667947576534740150920924298747567681548390770062330981296487628018070317046864945183999578329509529975945278159391047701084488326197625493824119211356745036954789566524817944375097765186286386322111068977419114806877193205439308798688641048344275978426545677118548035945466109481691748745492166325715675508249765158300926915198212851993962378122214230365775549201304642374169926835907699616798236573992008965984724709338689519912122679287044366867637688274632163169400505384702519635170103321944559936871687312487952659421932479790574335713346215214541577764297965432575127079363158664670262197111280325985634909127357105481578141955715462538689062252320336463298955043081833941744674735979328953127251232184551398568493816654733458397631452326684788264149856262425603742171986501037253955848375816524991893568259653635586425672324208778335353438796316358456718767126419368524873885121975520810649316289915854206621258734825363697469689443881291188025042801455918831577325187077895133271734336452757257306485307515113319482632384601844073774669398707809931355346661617009844383571453682045105210721218649302734004623365284006045554195033655346959867992108824172001138759836017058489972369992031890056274436844270243390856851815213661087383988421340575429731095653638052675633146676518480174379218106233765632668489929504792137796854713698389258580534583260820292222062855119086699256291389664995440780461073145551488443550137873256062231964985158651849174887956742023478067258583839676951225960480807726308209937773492726189472151915349138865418492507134616444294499645871712730341169578828743847489994712295530799337069878679589977977570393875335601592562404485544264265366130590652665131028291430990514332393333020049014918262649996622031612250016194331480737117644198359135541612238796265236824297121458184326701253187396159902154191308228283588302196822138872163573905540166787741803474166475124105985211152558417317202842929168818745244281867829145223546185565450793278615821967517640146020950296038186922039581664636521528404269884413536528527376350966366402790590243182056819021638674671616562404792286312543582159259998813862623352256515661497352368995468737232782366333772606438881266902290472945502118003425659404336924805277494818053112880657348710011573437664886234506442788834214283540263714659779586716342827573185986744223174709593709621197574659086123818925046468852440547328827517416573806727133180215614797041053573591856125723699814570545340356954543376750608621459550465639498788935351059519381531783965696086871862219914902837783458369361255625897667790981935905941167096224253887177807284320897197696429719448507899857863809754951777620725769788456680963317214511019105929768834268345973469057769569473693682848256546059032267339558649142927284177370718739617564601203165116339653269669733405647832585572115879452203617484074663238317456839011531053477954227888352545898907185693147315079061793867647128501011989948308208620398142099679564540429362919091442944719084240041881457527402358812962045995658391177162559366626843269223208029049187063811728664682715474424550432873235056133161083461782101693147217502473691751321997086948588722646216970210557291901769412144986229629484438155795393533209454585771547714564566635618237720860925972726836059222386438152527552447746925435647368452011139818532394854771673029705295544928249344356859507651346995654947232735311416617270974983688393751562271221237083151953813172945952112959683514622727992057267975919252618716564461388170638522216954661474601941958259758694821489689114935710469222814324718508618264988045555533311085743924619394366327886261812539155576429791884171651177681843864227991547326039549742503137341091579537220745641520012832216253596422842538049834369460589250673405443137195155554587673734235549879678877591684886475555971691317917829196658987837541818736267151753568225871742974212449820711073964817051606785831741496852463207259174114035675023356620811683635749777874306608380983320236971533711314211261792883252387656339624166225268491137230671931665130433510569396295219934722657968206583440915854353376128264407204325466957499408122387147845772256908096995674993407134124097895067968615983831250418935495344425171795744353312151138013360403867611671458944448230467116034988441032084282393802943792714815627946996938247406462629448208292588662661975967209182553874475164679364061152414641984943285178845678213373299956781025560547612179116735676643855071565770377812590655169648753430251466556382249176215178799781975367593792302548821371643311692632778869313217353273370935550688808679944602313454845751353356522496989097946039769248435326916376964715679417175847193063305019298752745271745781749759309714191994819103962573625760385717919213530418856328743305143869552756215751467362687563052877686483341179508670242687231081885523440845111727822626354945525734027390095449998116938183035244998314954726730365175463567436425346763778265767602165088332468709364610343023588690787112748496187211575592433527065545859125854950382244855641887582209444191217831113771502745715371367127474146664427618369654417679938789758841813585333189162703545207274783296058359850961398796457111131359985720378616041071917716730871384567316421174932892842580492427624676648173236697384427106351347842827691833618140924496543292964948464239279713042815325778986756366092266167474440591285576746434519149545966976329951344483169315396883674441113286365478282632614932627264189054227292665864137059871132193153281280032416339207481755641322295845611517138305115559244613175182149257695267169826824120914829420067154731938240841968925664043023970990287089934921651695575266736281292143456291886473311757950104426439266727327786916746044539258303308751749518181235957802726442565157346465255052664751225763798075572282569680382563133946036812779141391096454101041936688108364754339498761076207496357083412254034659119048787380858525119154326551830710781193149749249264815191516792717251384473129697162102370932547298271695898219839156311376158756351193991222921766062421143621229223313275719350539081689980338766041181360634497175319319762590824337465942667089914838940410015921050227223613323683824140728261272288046479332662647558324619957921960980783100836693492688287476695832952808261813562624477259485064823269489048457719235339427647873344113177494544219681491304522015779331904295952594248422266339352722339746219363597920352731757161422406002367361172977433543742509398250884579646029032162285278767119568756492606254627986359537945805439337683592727974762625679575255552589225825635681497271504619937458388671926278769351807950455398122937937085795648051811356038263844711961829582690979677575674524869476396932360277775022631452453696620939712387228639295983464232919293754767132105922547155588223737548583676038775563397662185925045558743329733743880343798959154421358696388168656491857536306320906624100604282971429978763715748245171234592962841750531634490381622192340510470296876333011240595641988790779439754681975525557785158197981088259864756455231960641359373772745810756175846662880986372766247987284919217285979718371214927992369513820769573787441377692788653826110314834442185282686208691672523652446874892508529246203157927254126962910130660465645244576824957154441112990312 \ No newline at end of file diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.25-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.25-main.c new file mode 100644 index 0000000..8e9333c --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.25-main.c @@ -0,0 +1,43 @@ +#include +#include "../../../α㷨ʵ/09 /00 Base/Base.c" //**09 **// + +/* 궨 */ +#define MAX 20 //ؼָ + +/* ԭ */ +int Algo_9_25(Table T, KeyType key); + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T; + KeyType key = 15; + int i; + + printf("һұԴС...\n"); + fp = fopen("Data/Algo_9_25.txt", "r"); + Create(fp, &T, MAX+1); //࿪һλ + Traverse(T, PrintKey); + printf("\n"); + + i = Algo_9_25(T, key); + if(i==MAX+1) + printf("ؼ %d δҵ\n", key); + else + printf("ؼ %d ڵ %d λá\n", key, i); + printf("\n"); + + return 0; +} + +int Algo_9_25(Table T, KeyType key) +{ + int i; + + T.elem[MAX+1].key = key; + + for(i=1; T.elem[i].key!=key; ++i) + ; + + return i; +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.26~9.27-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.26~9.27-main.c new file mode 100644 index 0000000..c54c821 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.26~9.27-main.c @@ -0,0 +1,81 @@ +#include +#include "../../../α㷨ʵ/09 /00 Base/Base.c" //**09 **// + +/* 궨 */ +#define MAX 20 //ؼָ + +/* ԭ */ +int Algo_9_26(Table T, KeyType key, int low, int high); +int Algo_9_27(Table T, KeyType key); + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T; + KeyType key = 18; + int i; + + printf("һұС...\n"); + fp = fopen("Data/Algo_9_26-9_27.txt", "r"); + Create(fp, &T, MAX); + Traverse(T, PrintKey); + printf("\n"); + + printf(" 9.26 ֤... \n"); + i = Algo_9_26(T, key, 1, T.length); + if(!i) + printf("ؼ %d δҵ\n", key); + else + printf("ؼ %d ڵ %d λá\n", key, i); + printf("\n"); + + printf(" 9.27 ֤... \n"); + i = Algo_9_27(T, key); + printf("ؼ %d ڵ %d 䡣\n", key, i); + printf("\n"); + + return 0; +} + +int Algo_9_26(Table T, KeyType key, int low, int high) +{ + int mid; + + if(low>high) //δҵʱ0 + return 0; + + mid = (low+high)/2; + + if(T.elem[mid].key==key) + return mid; + else if(T.elem[mid].key>key) + return Algo_9_26(T, key, low, mid-1); + else + return Algo_9_26(T, key, mid+1, high); +} + +int Algo_9_27(Table T, KeyType key) //ʵDzѯڵ +{ + int low, high, mid; + + if(key=T.elem[T.length].key) + return T.length; + + low = 1; + high = T.length; + + while(high-low>1) //ұԪش1 + { + mid = (low + high) / 2; + + if(T.elem[mid].key<=key) + low = mid; + else + high = mid; + } + + return low; +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.28-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.28-main.c new file mode 100644 index 0000000..ea0f529 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.28-main.c @@ -0,0 +1,105 @@ +#include +#include "../../../α㷨ʵ/09 /00 Base/Base.c" //**09 **// + +/* 궨 */ +#define MAX 30 //ؼָ + +/* Ͷ */ +typedef struct +{ + int start; //ǰ + int min; //ǰԪСֵ +}Index; +typedef struct +{ + Table T; //пԪ + Index Block[MAX]; + int bnum; //ĸ +}Lookup; //ؼֵIJұ + +/* ԭ */ +int Algo_9_28(Lookup ST, KeyType key); +void CreateBlock_9_28(FILE* fp, Lookup *ST); + +int main(int argc, char *argv[]) +{ + FILE *fp; + Lookup ST; + KeyType key = 89; + int i; + + printf("һұ...\n"); + fp = fopen("Data/Algo_9_28.txt", "r"); + CreateBlock_9_28(fp, &ST); + Traverse(ST.T, PrintKey); + printf("\n"); + + i = Algo_9_28(ST, key); + if(!i) + printf("ؼ %d δҵ\n", key); + else + printf("ؼ %d ˳ %d λá\n", key, i); + printf("\n"); + + return 0; +} + +int Algo_9_28(Lookup ST, KeyType key) +{ + int low, high, mid; + + if(key=ST.Block[ST.bnum].min) + low = ST.bnum; + else + { + low=1, high=ST.bnum; + + while(high-low>1) + { + mid = (low + high) / 2; + + if(ST.Block[mid].min<=key) + low = mid; + else + high = mid; + } + } + + if(low) //ҵĿ󣬼˳Tв + { + if(low==ST.bnum) + high = ST.T.length; + else + high = ST.Block[high].start-1; + + while(low<=high) + { + mid = (low+high)/2; + + if(keyST.T.elem[mid].key) + low = mid + 1; + else + return mid; + } + } + + return 0; //δҵ +} + +void CreateBlock_9_28(FILE* fp, Lookup *ST) //ұ +{ + int i; + + Create(fp, &ST->T, MAX); //¼йؼִ˳ + + for(i=1,ST->bnum=0; i<=ST->T.length; i+=5) //趨5Ԫعһ + { + ST->bnum++; + ST->Block[ST->bnum].start = i; + ST->Block[ST->bnum].min = ST->T.elem[i].key; + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.29-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.29-main.c new file mode 100644 index 0000000..246776a --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.29-main.c @@ -0,0 +1,121 @@ +#include +#include +#include "../../../α㷨ʵ/01 /Status.h" +#include "../../../α㷨ʵ/01 /Scanf.c" + +/* Ͷ */ +typedef struct LNode +{ + int elem; + struct LNode* next; +}LNode; +typedef struct +{ + LNode* h; //h̶ָѭеСؼ + LNode* t; //tиÿβҽָϴβĵط +}CLinkLIst; + +/* ԭ */ +LNode* Algo_9_29(CLinkLIst *CL, int key); +void CreateList_9_29(FILE* fp, CLinkLIst *CL); +void PrintList_9_29(CLinkLIst CL); + +int main(int argc, char *argv[]) +{ + CLinkLIst CL; + FILE *fp; + LNode *p; + int key = 59; + + fp = fopen("Data/Algo_9_29.txt","r"); + CreateList_9_29(fp, &CL); + PrintList_9_29(CL); + printf("\n"); + + p = Algo_9_29(&CL, key); + if(p) + printf("ؼ %d ҳɹ\n", p->elem); + else + printf("ʧܣ\n"); + printf("\n"); + + return 0; +} + +LNode* Algo_9_29(CLinkLIst *CL, int key) +{ + LNode *p; + + while(1) + { + if(keyt->elem) + { + for(p=CL->h; p->elemnext!=CL->t; p=p->next) + ; //h + + CL->t = p; + + if(p->elem!=key) + return NULL; + else + return p; + } + else if(key>CL->t->elem) //t + { + if(CL->t->next!=CL->h) + CL->t = CL->t->next; + else + return NULL; + } + else + return CL->t; + } + + return p; +} + +void CreateList_9_29(FILE* fp, CLinkLIst *CL) +{ + int tmp; + LNode *p; + + CL->h = CL->t = NULL; + + while(Scanf(fp, "%d", &tmp)==1) + { + p = (LNode*)malloc(sizeof(LNode)); + p->elem = tmp; + + if(!CL->h) + CL->h = CL->t = p; + else + { + CL->t->next = p; + CL->t = CL->t->next; + } + } + + CL->t->next = CL->h; //β + CL->t = CL->t->next; //tʼʱָͷ +} + +void PrintList_9_29(CLinkLIst CL) +{ + LNode *p; + int count = 0; + + p = CL.h; + if(p) + printf("%2d ", p->elem); + count++; + + while(p->next!=CL.h) + { + p = p->next; + printf("%2d ", p->elem); + count++; + + if(count%10==0) + printf("\n"); + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.30-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.30-main.c new file mode 100644 index 0000000..c703eb0 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.30-main.c @@ -0,0 +1,114 @@ +#include +#include +#include "../../../α㷨ʵ/01 /Status.h" +#include "../../../α㷨ʵ/01 /Scanf.c" + +/* Ͷ */ +typedef struct LNode +{ + int elem; + struct LNode* pre; + struct LNode* next; +}LNode; +typedef LNode* DCLinkLIst; + +/*ȫֱ*/ +DCLinkLIst sp; + +/* ԭ */ +DCLinkLIst Algo_9_30(DCLinkLIst DL, int key); +void CreateList_9_30(FILE* fp, DCLinkLIst *DL); +void PrintList_9_30(DCLinkLIst DL); + +int main(int argc, char *argv[]) +{ + DCLinkLIst DL; + FILE *fp; + LNode *p; + int key = 59; + + fp = fopen("Data/Algo_9_30.txt","r"); + CreateList_9_30(fp, &DL); + PrintList_9_30(DL); + printf("\n"); + + p = Algo_9_30(DL, key); + if(p) + printf("ؼ %d ҳɹ\n", p->elem); + else + printf("ʧܣ\n"); + printf("\n"); + + return 0; +} + +DCLinkLIst Algo_9_30(DCLinkLIst DL, int key) +{ + while(1) + { + if(sp->elem==key) + return sp; + else if(sp->elemnext!=DL && sp->next->elem<=key) + sp = sp->next; + else + return NULL; + } + else + { + if(sp->pre!=DL && sp->pre->elem>=key) + sp = sp->pre; + else + return NULL; + } + } +} + +void CreateList_9_30(FILE* fp, DCLinkLIst *DL) +{ + int tmp; + DCLinkLIst p; + + *DL = NULL; + + while(Scanf(fp, "%d", &tmp)==1) + { + p = (DCLinkLIst)malloc(sizeof(LNode)); + p->elem = tmp; + + if(*DL==NULL) + *DL = sp = p; + else + { + sp->next = p; + sp->next->pre = sp; + sp = sp->next; + } + } + + sp->next = *DL; + sp->next->pre = sp; + sp = sp->next; +} + +void PrintList_9_30(DCLinkLIst DL) +{ + LNode *p; + int count = 0; + + p = DL; + if(p) + printf("%2d ", p->elem); + count++; + + while(p->next!=DL) + { + p = p->next; + printf("%2d ", p->elem); + count++; + + if(count%10==0) + printf("\n"); + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.31~9.34-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.31~9.34-main.c new file mode 100644 index 0000000..bd6c654 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.31~9.34-main.c @@ -0,0 +1,157 @@ +#include +#include +#include "../../../α㷨ʵ/09 /06 BinarySortTree/BinarySortTree.c" //**09 **// + +/* 궨 */ +#define FAIL INT_MAX //δҵֵʱı +#define MINKEY INT_MIN //ؼֵ + +/* ԭ */ +void Algo_9_31(BSTree BST, int *pre, int *flag); +void Algo_9_32(BSTree BST, int x, int *a, int *b, BSTree ptr[], int *count); +void Algo_9_33(BSTree BST, int x); +void Algo_9_34(BSTree *BST, int x); +void DestoryTree_9_34(BSTree *BST); + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T; + BSTree BST; + int x = 42; + + printf("ұ...\n"); + fp = fopen("Data/Algo_9_31-9_34.txt", "r"); + Create(fp, &T, 12); + printf("T = "); + Traverse(T, PrintKey); + printf("\n"); + + printf("Сؼ֣\n"); + CreateBSTree(&BST, T); + printf("BST = "); + InOrderTraverse_BST(BST, PrintKey); + printf("\n\n"); + + printf(" 9.31 ֤... \n"); + { + int pre = MINKEY; + int flag = TRUE; + Algo_9_31(BST, &pre, &flag); + printf(""); + flag ? printf("") : printf(""); + printf("\n"); + printf("\n"); + } + + printf(" 9.32 ֤... \n"); + { + int a ,b ,count; //countʼֵΪ0 + BSTree ptr[12]; //ؼָ + a = b = FAIL; + count = 0; + Algo_9_32(BST, x, &a, &b, ptr, &count); + printf("alchild) + Algo_9_31(BST->lchild, pre, flag); + + if(BST->data.key>=*pre) + *pre = BST->data.key; + else + *flag = FALSE; + + if(BST->rchild) + Algo_9_31(BST->rchild, pre, flag); + } +} + +void Algo_9_32(BSTree BST, int x, int *a, int *b, BSTree ptr[], int *count) +{ //countֵΪ0abֵΪFAIL京δҵʵֵ + if(BST) //С + { + Algo_9_32(BST->lchild, x, a, b, ptr, count); + ptr[(*count)++] = BST; + if(*count>=2) + { + if(ptr[*count-2]->data.keydata.key) + *a = ptr[*count-2]->data.key; + if(ptr[*count-2]->data.key<=x&&xdata.key) + *b = ptr[*count-1]->data.key; + if(*a!=FAIL&&*b!=FAIL) //abҵʱ + return; + } + Algo_9_32(BST->rchild, x, a, b, ptr, count); + } +} + +void Algo_9_33(BSTree BST, int x) +{ + if(BST) // + { + Algo_9_33(BST->rchild, x); + + if(BST->data.key>=x) + printf("%d ", BST->data.key); + else + return; + + Algo_9_33(BST->lchild, x); + } +} + +void Algo_9_34(BSTree *BST, int x) +{ + BSTree p; + + if(*BST) + { + if((*BST)->data.key>=x) + { + p = *BST; + *BST = p->lchild; + p->lchild = NULL; + DestoryTree_9_34(&p); //ٵǰ㼰ǰ + + Algo_9_34(BST, x); // + } + else + Algo_9_34(&(*BST)->rchild, x); // + } +} + +void DestoryTree_9_34(BSTree *BST) +{ + if(*BST) + { + DestoryTree_9_34(&(*BST)->lchild); // + DestoryTree_9_34(&(*BST)->rchild); // + free(*BST); + *BST = NULL; + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.35~9.37-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.35~9.37-main.c new file mode 100644 index 0000000..8c901ed --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.35~9.37-main.c @@ -0,0 +1,354 @@ +#include +#include +#include "../../../α㷨ʵ/09 /06 BinarySortTree/BinarySortTree.c" //**09 **// + +/*궨*/ +#define Max 15 //ұԪظ + +/* Ͷ */ +enum {Link, Thread}; //ǣLinkӣThread +typedef BSTree ThrBSTree; // + +/*ȫֱ*/ +ThrBSTree pre; //ָǰǰ +ThrBSTree parent; //ָǰ˫׽ + +/* ԭ */ +void Algo_9_35(ThrBSTree Thrt, int a, int b); +Status Algo_9_36(ThrBSTree Thrt, BSTElemType e); +Status Algo_9_37(ThrBSTree Thrt, KeyType key); +void Create_Thr(ThrBSTree *Thrt, ThrBSTree TBST); +void InOrder_Sub_Thr(ThrBSTree p); +void Print_InOrder(ThrBSTree Thrt); +Status SearchThr_9_36(ThrBSTree TBST, KeyType key, ThrBSTree f, ThrBSTree *p); + //pָؼkeyеλãfָp˫׽㣬ʧܣpָλ +Status DeleteThr_9_37(ThrBSTree *TBST, KeyType key); + //ɾؼkey +void DeleteNode_9_37(ThrBSTree *p); + //ɾpָĽ + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T; + ThrBSTree TBST, Thrt; + + printf("һұ...\n"); + fp = fopen("Data/Algo_9_35-9_37.txt", "r"); + Create(fp, &T, Max); + Traverse(T, PrintKey); + printf("\n"); + + printf("ؼ...\n"); + CreateBSTree(&TBST, T); // + Create_Thr(&Thrt, TBST); // + Print_InOrder(Thrt); + printf("\n\n"); + + printf(" 9.35 ֤... \n"); + { + int a, b; + a = 10; + b = 90; + printf(" %d С %d Ĺؼ֣", a, b); + Algo_9_35(Thrt, a, b); + printf("\n\n"); + } + + + printf(" 9.36 ֤... \n"); + { + BSTElemType e; + e.key=28, e.weight=0.0; + printf("ؼ %d ؼ...\n", e.key); + Algo_9_36(Thrt, e); + Print_InOrder(Thrt); + printf("\n\n"); + } + + printf(" 9.37 ֤... \n"); + { + KeyType key = 26; + printf("ɾؼ %d ؼ...\n", key); + Algo_9_37(Thrt, key); + Print_InOrder(Thrt); + printf("\n\n"); + } + + return 0; +} + +void Algo_9_35(ThrBSTree Thrt, int a, int b) //޵ݹ飩 +{ + ThrBSTree p = Thrt->lchild; //pָ + + while(p!=Thrt) + { + while(p->lchild) + p = p->lchild; + + if(adata.key&&p->data.keydata); + else + { + if(p->data.key>=b) + return; + } + + while(p->RTag==Thread && p->rchild!=Thrt) + { + p = p->rchild; + + if(adata.key&&p->data.keydata); + else + { + if(p->data.key>=b) + return; + } + } + + p = p->rchild; + } +} + +Status Algo_9_36(ThrBSTree Thrt, BSTElemType e) +{ + ThrBSTree p, s; + + if(!SearchThr_9_36(Thrt->lchild, e.key, NULL, &p)) //Ҳɹ + { + s = (ThrBSTree)malloc(sizeof(BSTNode)); + s->data = e; + s->lchild = NULL; + s->RTag = Thread; + + if(!p) //ԭΪ + { + Thrt->lchild = s; + s->rchild = Thrt; + } + else if(e.keydata.key) + { + p->lchild = s; + s->rchild = p; + } + else + { + s->rchild = p->rchild; + p->RTag = Link; + p->rchild = s; + } + + return TRUE; //e.key + } + else //ҳɹ + return FALSE; +} + +Status Algo_9_37(ThrBSTree Thrt, KeyType key) +{ + int tag; + + if(Thrt->lchild==Thrt) + return FALSE; + else + { + parent = Thrt; //parentָǰʽ˫׽ + tag = DeleteThr_9_37(&Thrt->lchild, key); + if(Thrt->lchild==NULL) //Ϊ + Thrt->lchild = Thrt; + return tag; + } + +} + +Status SearchThr_9_36(ThrBSTree TBST, KeyType key, ThrBSTree f, ThrBSTree *p) +{ //pָ·һܡʽ㣬fָThrt˫ףʼֵΪNULL + if(TBST->lchild==TBST) + { + *p = f; + return FALSE; + } + + if(key==TBST->data.key) + { + *p = TBST; + return TRUE; + } + else if(keydata.key) + { + if(!TBST->lchild) + { + *p = TBST; + return FALSE; + } + else + return SearchThr_9_36(TBST->lchild, key, TBST, p); + } + else + { + if(TBST->RTag==Link) + return SearchThr_9_36(TBST->rchild, key, TBST, p); + else + { + *p = TBST; + return FALSE; + } + } + +} + +Status DeleteThr_9_37(ThrBSTree *TBST, KeyType key) +{ + if(key==(*TBST)->data.key) + { + DeleteNode_9_37(TBST); //ҵҪɾĽ + return OK; + } + else if(key<(*TBST)->data.key) + { + if((*TBST)->lchild) + { + parent = *TBST; + return DeleteThr_9_37(&(*TBST)->lchild, key); + } + else + return FALSE; + } + else + { + if((*TBST)->RTag==Link) + { + parent = *TBST; + return DeleteThr_9_37(&(*TBST)->rchild, key); + } + else + return FALSE; + } +} + +void DeleteNode_9_37(ThrBSTree *p) +{ + ThrBSTree q, s; + + if((*p)->lchild&&(*p)->RTag==Thread) //Ϊ(û) + { + q = *p; + *p = (*p)->lchild; + if(*p) + { + s = *p; + while(s->RTag==Link) //ұѰ + s = s->rchild; + s->rchild = q->rchild; + } + free(q); + } + else if(!(*p)->lchild && (*p)->RTag==Link) //Ϊ(û) + { + q = *p; + *p = (*p)->rchild; + free(q); + } + else if(!(*p)->lchild && (*p)->RTag==Thread)//Ϊ(û) + { + q = *p; + + if(parent->lchild==q) + parent->lchild = NULL; + else + { + parent->RTag = Thread; + parent->rchild = q->rchild; + } + + free(q); + } + else //Ϊ(ǰ滻) + { + q = *p; + s = (*p)->lchild; + + while(s->RTag==Link) //Ѱpֱǰ + { + q = s; + s = s->rchild; + } + + (*p)->data = s->data; //pֱǰȡp + + if(q!=*p) + q->rchild = s->lchild; + else + { + q->lchild = s->lchild; + if(s->lchild) + { + q = s->lchild; + while(q->RTag==Link) + q = q->rchild; + q->rchild = *p; + } + } + + free(s); + } +} + +void Create_Thr(ThrBSTree *Thrt, ThrBSTree TBST) +{ + *Thrt = (ThrBSTree)malloc(sizeof(BSTNode)); + if(!TBST) + (*Thrt)->lchild = *Thrt; + else + (*Thrt)->lchild = TBST; + (*Thrt)->RTag = Thread; + (*Thrt)->rchild = *Thrt; + + pre = *Thrt; + InOrder_Sub_Thr(TBST); // + pre->RTag = Thread; //һ + pre->rchild = *Thrt; //һָͷ +} + +void InOrder_Sub_Thr(ThrBSTree p) +{ + if(p) + { + InOrder_Sub_Thr(p->lchild); // + + if(!pre->rchild) //Ϊһ + { + pre->RTag = Thread; + pre->rchild = p; + } + else + pre->RTag = Link; + + pre = p; //preǰŲһ + + InOrder_Sub_Thr(p->rchild); // + } +} + +void Print_InOrder(ThrBSTree Thrt) +{ + ThrBSTree p = Thrt->lchild; //pָ + + while(p!=Thrt) + { + while(p->lchild) + p = p->lchild; + + PrintKey(p->data); + + while(p->RTag==Thread && p->rchild!=Thrt) + { + p = p->rchild; + PrintKey(p->data); + } + + p = p->rchild; + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.38~9.39-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.38~9.39-main.c new file mode 100644 index 0000000..313ef24 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.38~9.39-main.c @@ -0,0 +1,120 @@ +#include +#include "../../../α㷨ʵ/09 /06 BinarySortTree/BinarySortTree.c" //**09 **// + +/* ԭ */ +void Algo_9_38(BSTree BST1, BSTree *BST2, void(*InsertNode)(BSTree*,BSTree)); //BST2ϲBST1 +void Algo_9_39(BSTree *BST, BSTree *BST1, BSTree *BST2, int x, void(*InsertNode)(BSTree*,BSTree));//ֽBST +void InsertNode_9_38(BSTree *BST, BSTree p); //pָʾĽ뵽BSTкʵλ + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T1, T2; + BSTree BST1, BST2, BST3; + int x = 45; + + printf("ұ...\n"); + fp = fopen("Data/Algo_9_38-9_39.txt", "r"); + Create(fp, &T1, 8); + Create(fp, &T2, 10); + printf("T1 = "); + Traverse(T1, PrintKey); + printf("T2 = "); + Traverse(T2, PrintKey); + printf("\n"); + + printf("öСؼ֣\n"); + CreateBSTree(&BST1, T1); + CreateBSTree(&BST2, T2); + printf("BST1 = "); + InOrderTraverse_BST(BST1, PrintKey); + printf("\n"); + printf("BST2 = "); + InOrderTraverse_BST(BST2, PrintKey); + printf("\n\n"); + + printf(" 9.38 ֤... \n"); + printf(" BST2 ϲ BST1 ...\n"); + Algo_9_38(BST1, &BST2, InsertNode_9_38); + printf("BST1 = "); + InOrderTraverse_BST(BST1, PrintKey); + printf("\n\n"); + + printf(" 9.39 ֤... \n"); + printf("ֽ BST1 BST2 BST3 %d Ϊ...\n", x); + BST2 = BST3 = NULL; + Algo_9_39(&BST1, &BST2, &BST3, x, InsertNode_9_38); + printf("BST2 = ", x); + InOrderTraverse_BST(BST2, PrintKey); + printf("\n"); + printf("BST3 = ", x); + InOrderTraverse_BST(BST3, PrintKey); + printf("\n\n"); + + return 0; +} + +void Algo_9_38(BSTree BST1, BSTree *BST2, void(*InsertNode)(BSTree*,BSTree)) +{ //InsertNode + BSTree l, r; + + if(*BST2) + { + l = (*BST2)->lchild; + r = (*BST2)->rchild; + (*BST2)->lchild = (*BST2)->rchild = NULL; //ӽҪʱ + + Algo_9_38(BST1, &l, InsertNode); + InsertNode(&BST1, *BST2); + Algo_9_38(BST1, &r, InsertNode); + } + + *BST2 = NULL; +} + +void Algo_9_39(BSTree *BST, BSTree *BST1, BSTree *BST2, int x, void(*InsertNode)(BSTree*,BSTree)) +{ //InsertNode + BSTree l, r; + + if(*BST) + { + l = (*BST)->lchild; + r = (*BST)->rchild; + (*BST)->lchild = (*BST)->rchild = NULL; //ӽҪʱ + + Algo_9_39(&l, BST1, BST2, x, InsertNode);//ȱ + Algo_9_39(&r, BST1, BST2, x, InsertNode); + + if((*BST)->data.key>=x) + InsertNode(BST1, *BST); + else + InsertNode(BST2, *BST); + } + + *BST = NULL; +} + +void InsertNode_9_38(BSTree *BST, BSTree p) +{ + if(*BST) + { + if(p->data.key<(*BST)->data.key) + { + if((*BST)->lchild==NULL) + (*BST)->lchild = p; + else + InsertNode_9_38(&(*BST)->lchild, p); + } + else if(p->data.key>(*BST)->data.key) + { + if((*BST)->rchild==NULL) + (*BST)->rchild = p; + else + InsertNode_9_38(&(*BST)->rchild, p); + } + else + free(p); + } + else + *BST = p; +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.40-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.40-main.c new file mode 100644 index 0000000..426a7cc --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.40-main.c @@ -0,0 +1,69 @@ +#include +#include "../../../α㷨ʵ/09 /07 BalancedBinarySortTree/BalancedBinarySortTree.c" //**09 **// + +/* 궨 */ +#define Max 15 + +/* ԭ */ +BBSTree Algo_9_40(BBSTree BBST, int k); //ҳƽеkСĽ +int fLsize_9_40(BBSTree BBST); //lsizeֵ + +int main(int argc, char *argv[]) +{ + + FILE *fp; + Table T; + BBSTree BBST, p; + int k = 5; + + printf("һұ...\n"); + fp = fopen("Data/Algo_9_40.txt", "r"); + Create(fp, &T, Max); + Traverse(T, PrintKey); + printf("\n"); + + printf("첢ӡеĹؼ֣"); + CreateAVL(&BBST, T); + printf("ӡƽ\n"); + PrintAVLTree(BBST); + printf("\n"); + + fLsize_9_40(BBST); + p = Algo_9_40(BBST, k); + printf("ƽдСе %d ؼΪ %d \n", k, p->data); + printf("\n"); + + return 0; +} + +BBSTree Algo_9_40(BBSTree BBST, int k) +{ + if(!BBST) + return NULL; + else + { + if(k==BBST->lsize) + return BBST; + else if(klsize) + return Algo_9_40(BBST->lchild, k); + else + return Algo_9_40(BBST->rchild, k-BBST->lsize); //kֵı + } +} + +int fLsize_9_40(BBSTree BBST) +{ + int lnum, rnum; + + if(BBST) + { + lnum = fLsize_9_40(BBST->lchild); // + rnum = fLsize_9_40(BBST->rchild); // + + BBST->lsize = lnum + 1; + + return lnum+rnum+1; + } + else + return 0; +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.41-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.41-main.c new file mode 100644 index 0000000..e4ccf40 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.41-main.c @@ -0,0 +1,43 @@ +#include +#include "../../../α㷨ʵ/09 /09 B+Tree/B+Tree.c" + +/* 궨 */ +#define Max 15 + +/* ԭ */ +Result Algo_9_41(B_Tree B_T, KeyType K); + +int main(int argc, char *argv[]) +{ + FILE *fp; + Table T; + B_Tree B_T; + KeyType Key = 37; + Result R; + + printf("ұ...\n"); + fp = fopen("Data/Algo_9_41.txt", "r"); + Create(fp, &T, Max); + Traverse(T, PrintKey); + printf("\n"); + + printf("B+...\n"); + CreateB_Tree(&B_T, T); + PrintB_Tree(B_T); + printf("\n"); + + printf("ҹؼ %d ...\n", Key); + R = Algo_9_41(B_T, Key); + if(R.tag) + printf("ҵؼ %d", R.pt->key[R.i]); + else + printf("δҵؼ֣"); + printf("\n\n"); + + return 0; +} + +Result Algo_9_41(B_Tree B_T, KeyType K) +{ + return SearchB_Tree(B_T, K); //Ѷ +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.42~9.43-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.42~9.43-main.c new file mode 100644 index 0000000..3329001 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.42~9.43-main.c @@ -0,0 +1,50 @@ +#include +#include "../../../α㷨ʵ/09 /11 TrieTree/TrieTree.c" + +/* ԭ */ +void Algo_9_42(TrieTree *TT, Record R); +void Algo_9_43(TrieTree *TT, KeysType K); +void Print(Record *r); + +int main(int argc, char *argv[]) +{ + FILE *fp; + TrieTree TT; + Record R = {"SUN"}; + KeysType K = {"YANG", 4}; + + printf("һü...\n"); + fp = fopen("Data/Algo_9_42-9_43.txt", "r"); + CreateTrie(fp, &TT); + TraverseTrie(TT, Print); + printf("\n\n"); + + printf(" 9.42 ֤... \n"); + printf("뺬ؼ %s ļ¼...\n", R.key); + Algo_9_42(&TT, R); + TraverseTrie(TT, Print); + printf("\n\n"); + + printf(" 9.43 ֤... \n"); + printf("ɾؼ %s ļ¼...\n", K.ch); + Algo_9_43(&TT, K); + TraverseTrie(TT, Print); + printf("\n\n"); + + return 0; +} + +void Algo_9_42(TrieTree *TT, Record R) +{ + InsertTrie(TT, R); //Ѷ +} + +void Algo_9_43(TrieTree *TT, KeysType K) +{ + DeleteTrie(TT, K); //Ѷ +} + +void Print(Record *r) +{ + printf("%s ", r->key); +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.44-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.44-main.c new file mode 100644 index 0000000..f8068ed --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.44-main.c @@ -0,0 +1,85 @@ +#include +#include +#include "../../../α㷨ʵ/01 /Status.h" //**01 **// +#include "../../../α㷨ʵ/01 /Scanf.c" + +/* 궨 */ +#define MAXSIZE 100 + +/* Ͷ */ +typedef struct +{ + char s[27]; //ؼֳȲ26 +}HNode; +typedef HNode HashTable[MAXSIZE]; + +/* ԭ */ +void Algo_9_44(HashTable H); +Status CreateHash_9_44(FILE *fp, HashTable H);//ϣ +int fHash_9_44(char s[]);//sĸ(д)ĸеλ + +int main(int argc, char *argv[]) +{ + FILE *fp; + int p; + HashTable H; + + fp = fopen("Data/Algo_9_44.txt","r"); + CreateHash_9_44(fp, H); + fclose(fp); + + printf("ĸֵ˳ϣйؼ֣\n"); + Algo_9_44(H); + printf("\n"); + + return 0; +} + +/* +9.44ؼĸ˳ϣйؼ֩ +*/ +void Algo_9_44(HashTable H) +{ + int i, j; + + for(i=1; i<=26; i++) + { + j = i; + + while(strcmp(H[j].s,"")!=0) + { + if(fHash_9_44(H[j].s)==i) + printf("%s ", H[j].s); + j = (j+1)%MAXSIZE; + } + } +} + +Status CreateHash_9_44(FILE *fp, HashTable H) +{ + int i, p; + char tmp[26]; + + for(i=0; i +#include +#include "../../../α㷨ʵ/01 /Status.h" //**01 **// +#include "../../../α㷨ʵ/01 /Scanf.c" + +/* 궨 */ +#define MAXSIZE 100 + +/* Ͷ */ +typedef struct KNode //ڽӱ +{ + int key; + struct KNode *next; +}KNode; +typedef struct +{ + KNode *first; +}HNode; +typedef HNode HashTable[MAXSIZE]; + +/* ԭ */ +void Algo_9_45(FILE *fp, HashTable H); //ϣ +int fHash_9_45(int key); //iĹϣַ +void Print_9_45(HashTable H); + +int main(int argc, char *argv[]) +{ + FILE *fp; + int p; + HashTable H; + + fp = fopen("Data/Algo_9_45.txt","r"); + Algo_9_45(fp, H); + fclose(fp); + + printf("ϣйؼ֣\n"); + Print_9_45(H); + printf("\n"); + + return 0; +} + +/* +9.45ϣַͻ +*/ +void Algo_9_45(FILE *fp, HashTable H) +{ + int i, p; + int tmp; + KNode *r, *h; + + for(i=0; ikey = tmp; + h->next = NULL; + + p = fHash_9_45(tmp); + + if(H[p].first==NULL) + H[p].first = h; + else + { + if(H[p].first->key>tmp) + { + h->next = H[p].first; + H[p].first = h; + } + else + { + r = H[p].first; + + while(r->next&&r->next->key<=tmp) + r = r->next; + + h->next = r->next; + r->next = h; + } + } + } +} + +int fHash_9_45(int key) //ϣ +{ + return key%13; //H(key) = key MOD 13 +} + +void Print_9_45(HashTable H) +{ + int i; + KNode *r; + + for(i=1; inext) + printf("%d ", r->key); + } + } +} diff --git a/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.46-main.c b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.46-main.c new file mode 100644 index 0000000..97c6e29 --- /dev/null +++ b/▼配套习题解析/▼09 查找/▼习题测试文档-09/Question-9.46-main.c @@ -0,0 +1,97 @@ +#include +#include "../../../α㷨ʵ/01 /Status.h" //**01 **// +#include "../../../α㷨ʵ/01 /Scanf.c" + +/* 궨 */ +#define MAXSIZE 20000 + +/* Ͷ */ +typedef struct +{ + int row; + int col; + int key; + int tag; //tag=0:Ϊ +}HNode; +typedef HNode HashTable[MAXSIZE]; + +/* ԭ */ +int Algo_9_46(HashTable H, int row, int col); +Status CreateHash_9_46(FILE *fp, HashTable H);//ϣ +int fHash_9_46(int row, int col);//ֵɹϣַ + +int main(int argc, char *argv[]) +{ + FILE *fp; + int p; + HashTable H; + + fp = fopen("Data/Algo_9_46.txt","r"); + CreateHash_9_46(fp, H); + fclose(fp); + + printf("Ҳԣ\n"); + p = Algo_9_46(H, 622,64); + printf("±Ϊ(%d %d)Ĺؼڹϣ %d λá\n", 622, 64, p); + printf("\n"); + + return 0; +} + +/* +9.46ֵָڹϣеλé +*/ +int Algo_9_46(HashTable H, int row, int col) +{ + int p; + + p = fHash_9_46(row, col); + + while(H[p].tag==1&&(H[p].row!=row||H[p].col!=col)) + p = (p+1)%MAXSIZE; //֡Чײ˳² + + if(H[p].tag==1&&H[p].row==row&&H[p].col==col) + return p; + else + return -1; //ֵµĹؼֲʱ-1 +} + +Status CreateHash_9_46(FILE *fp, HashTable H) +{ + int i, p; + int row, col, key; + + for(i=0; i (1)相同 +> (2)不相同 +> (3)不相同 + +### 9.2 试分别画出在线性表(a,b,c,d,e,f,g)中进行折半查找,以查关键字等于e,f和g的过程。 + +> ![9.2](_v_images/20181214213333370_9161.png) + +### 9.3 画出对长度为10的有序表进行折半查找的判定树,并求其等概率时查找成功的平均查找长度。 + +> ![9.3](_v_images/20181214213408549_8068.png) +> +> ASLsucc=(1*1+2*2+3*4+4*3)/10=2.9 + +### 9.4 假设按下述递归方法进行顺序表的查找:若表长≤10,则进行顺序查找,否则进行折半查找。试画出对表长n=50的顺序表进行上述查找时,描述该查找的判定树,并求出在等概率情况下查找成功的平均查找长度。 + +> ![9.4](_v_images/20181214213449099_15405.png) +> +> ASLsucc=(1*1+2*2+3*4+(4+5+6+7+8)*8+9*3)/50=5.68 + +### 9.5 下列算法为斐波那契查找的算法: + +```c +int FibSearch(SqList r, KeyType K) +{ + j=1; + while(fib(j)0) && !found) + switch + { + case K=r[mid].key: + found = TRUE; + break; + case Kr[mid].key: + if(f1=1) + mid = 0; + else + { + mid = mid + f2; + f1 = f1 - f2; + f2 = f2 – f1; + } + break; + } + + if(found) + return mid; + else + return 0; +}//FibSearch + +其中fib(i)为斐波那契序列(参见9.1.2节注)。试画出对长度为20的有序表进行斐波那契查找的判定树,并求在等概率时查找成功的平均查找长度。 +``` + +> ![9.5](_v_images/20181214213551073_28565.png) +> +> ASLsucc=(1*1+2*2+3*4+4*7+5*5+6*1)/20=3.8 + +### 9.6 假设在某程序中有如下一个if-then嵌套的语句 + +```c +if(C1) + if(C2) + if(C3) + … + if(Cn) + S; +``` + +#### 其中Ci为布尔表达式。显然,只有当所有的Ci都为TRUE时,语句S才能执行。假设t(i)为判别Ci是否为TRUE所需时间,p(i)为Ci是TRUE的概率,试讨论这n个布尔表达式Ci(i=1,2,…,n)应如何排列才能使该程序最有效地执行? + +> 暂未理解题意。如有好的见解,欢迎提交Issues。 + +### 9.7 已知一个有序表的表长为8N,并且表中没有关键字相同的记录。假设按如下所述方法查找一个关键字等于给定值K的记录:先在第8,16,24,…,8K,…,8N个记录中进行顺序查找,或者查找成功,或者由此确定出一个继续进行折半查找的范围。画出描述上述查找过程的判定树,并求等概率查找时查找成功的平均查找长度。 + +> (N+1)/2+17/8 + +### 9.8 已知含12个关键字的有序表及其相应权值为: +![9.8.1](_v_images/20181214214042887_18269.png) +##### (1)试按次优查找树的构造算法并加适当调整画出由这12个关键字构造所得的次优查找树,并计算它的PH值; +##### (2)画出对以上有序表进行折半查找的判定树,并计算它的PH值。 + +> (1)次优查找树如下图,其PH=133 +> ![9.8.2](_v_images/20181214213924381_20066.png) +> +> (2)折半查找时:PH=156 + +### 9.9 已知如下所示长度为12的表:(Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec) +##### (1)试按表中元素的顺序依次插入一棵初始为空的二叉排序树,画出插入完成之后的二叉排序树,并求其在等概率的情况下查找成功的平均查找长度。 +##### (2)若对表中元素先进行排序构成有序表,求在等概率的情况下对此有序表进行折半查找时查找成功的平均查找长度。 +##### (3)按表中元素顺序构造一棵平衡二叉排序树,并求其在等概率的情况下查找成功的平均查找长度。 + +> (1)ASLsucc=42/12=3.5 +> ![9.9.1](_v_images/20181214214525317_23291.png) +> +> (2)排序后的关键字如下,其平均查找长度为:ASLsucc=37/12=3.08 +> Apr, Aug, Dec, Feb, Jan, July, June, Mar, May, Nov, Oct, Sept +> +> (3)ASLsucc=38/12=3.17 +> ![9.9.2](_v_images/20181214214606440_10337.png) + +### 9.10 可以生成如下二叉排序树的关键字的初始排列有几种?请写出其中的任意5个。 +![9.10](_v_images/20181214214633281_23456.png) + +> 总共30种。 +> +> 例:(5,6,7,4,2,1,3)(5,4,6,7,2,1,3)(5,4,2,6,7,1,3)(5,4,2,1,6,7,3)(5,4,2,1,3,6,7)。 + +### 9.11 试推导含12个结点的平衡二叉树的最大深度,并画出一棵这样的树。 + +> ![9.11](_v_images/20181214214720912_4862.png) +> 深度为h的平衡二叉树中含有的最少结点数为Nh=Nh-1+Nh-2+1,由此可推出含有12个结点的平衡二叉树的最大深度为5. + +### 9.12 在B-树定义中,特性(3)的意图是什么?试思考:若把“┌m/2┐”改为“┌2m/3┐”或“┌m/3┐”是否可行?所得到的树结构和B-树有何区别? + +> 特性3的意图在于保证B-树中结点空间的利用率不低于某个下限。改为┌2m/3┐不行,因为某结点因插入关键字而使其中关键字书目为m时,无法分裂成两个子树个数大于┌2m/3┐的结点;改为┌m/3┐是可行的,但它的结点利用空间利用率较低,不过分裂不如B-树那样频繁。 + +### 9.13 含9个叶子结点的3阶B-树中至少有多少个非叶子结点?含10个叶子结点的3阶B-树中至多有多少个非叶子结点? +> (1)至少4个 +> (2)至多8个 + +### 9.14 试从空树开始,画出按以下次序向2-3树即3阶B-树中插入关键码的建树过程:20,30,50,52,60,68,70。如果此后删除50和68,画出每一步执行后2-3树的状态。 + +> 创建过程如下: +> ![9.14.1](_v_images/20181214214910449_20293.png) + +> 删除过程如下: +> ![9.14.2](_v_images/20181214214939408_12857.png) + +### 9.15 试证明:高度为h的2-3树中叶子结点的数目在2h-1与3h-1之间。 + +> 设2-3树每层结点关键字数量为n,则n=1或n=2。 +> 显然,当各结点关键字数量为1时,2-3树即为一颗完全二叉树,当其高度为h时,其叶子结点数目有最小值2h-1。 +> 同理,当树中各结点关键字数量为2时,可得高度为h的2-3树叶子结点数量为3h-1。 +> 故结论得证。(画图辅助证明会更直观) + +### 9.16 在含有n个关键码的m阶B-树中进行查找时,最多访问多少个结点? + +> B-树中查找时,总是顺着树的分支往下走,且一次走一层,所以欲使访问果的结点最多,则只需使n个关键码构成的B-树有最大高度即可。 +> 对于含n个关键码的m阶B-树,其高度最多为:h=logt((n+1)/2)+1,t=┌m/2┐,故其最多访问的结点数为h。 + +### 9.17 B+树和B-树的主要差异是什么? +> B-树:多路搜索树,每个结点存储M/2到M个关键字,非叶子结点存储指向关键字范围的子结点;所有关键字在整颗树中出现,且只出现一次,非叶子结点可以命中; +> B+树:在B-树基础上,为叶子结点增加链表指针,所有关键字都在叶子结点中出现,非叶子结点作为叶子结点的索引;B+树总是到叶子结点才命中。 + +### 9.18 试画一个对应于关键字集{program, programmer, programming,processor, or}的Trie树,对每个关键字从右向左取样,每次一个字母。 + +> 注:只写出了使用的字母 +> ![9.18](_v_images/20181214215141119_17719.png) + +### 9.19 选取哈希函数H(k)=(3k) MOD 11。用开放定址法处理冲突,di= i((7k) MOD 10+1) (i=1,2,3, …)。试在0~10的散列地址空间中对关键字序列(22, 41, 53, 46, 30, 13, 01, 67)造哈希表,并求等概率情况下查找成功时的平均查找长度。 + +> 由题意可知: +> +> H(22)=0; +> H(41)=2; +> H(53)=5; +> H(46)=6; +> H(30)=2; -> H(30)=3; +> H(13)=6; -> H(13)=8; +> H(01)=3; -> H(01)=0; -> H(01)=8; -> H(01)=5; -> H(01)=2; -> H(01)=10; +> H(67)=3; -> H(67)=2; -> H(67)=1; +> +> 故构造函数的哈希表为: +> ![9.19](_v_images/20181214215223320_3867.png) +> +> 等概率时查找成功的平均查找长度为: +> ASL = (1+3+1+2+1+1+2+6)/8=17/8 + +### 9.20 试为下列关键字建立一个装载因子不小于0.75的哈希表,并计算你所构造的哈希表的平均查找长度。 +##### (ZHAO, QIAN, SUN, LI, ZHOU, WU, ZHENG, WANG, CHANG, CHAO, YANG, JIN) + +> 这里使用线性探测开放定址法建立哈希表 +> +> (1)确定哈希表长度 +> 由装载因子=记录数/表长得:表长≤12/0.75=16 +> +> (2)将单词key转换为一个数字num,转换依据是:(还有其他转换依据) +>> int num = 0; +>> for(i=0; i> { +>> num = 37*num + (key[i]-‘A’+1); //姓氏字串不会太长,可以遍历所有字符构造哈希值 +>> } +> 由此可得: +> num(ZHAO)=1327982; num(QIAN)=873473; num(SUN)=26802; num(LI)=453; +> num(ZHOU)=1328506; num(WU)=872; num(ZHENG)=49140780; num(WANG)=1166913; +> num(CHANG)=6029601; num (CHAO)=162963; num (YANG)=1268219; num (JIN)=14037; +> +> (3)映射为哈希值,出现冲突时,顺次后移一位。 +> h(ZHAO)=1327982%16=14; +> h(QIAN)=873473%16=1; +> h(SUN)=26802%16=2; +> h(LI)=453%16=5; +> h(ZHOU)=1328506%16=10; +> h(WU)=872%16=8; +> h(ZHENG)=49140780%16 = 12; +> h(WANG)=1166913%16=1; h(WANG)=2; h(WANG)= 3; +> h(CHANG)=6029601%16=1; h(CHANG)=2; h(CHANG)=3; h(CHANG) = 4; +> h(CHAO)=162963%16=3; h(CHAO)=4; h(CHAO)=5; h(CHAO) = 6; +> h(YANG)=1268219%16=11; +> h(JIN)=14037%16=5; h(JIN) = 6; h(JIN) = 7; +> +> (4)构造的哈希查找表如下图: +> ![9.20](_v_images/20181214215634299_17793.png) +> 故平均查找长度为:ASL=(1+1+3+4+1+4+3+1+1+1+1+1)/12=1.8 + +### 9.21 在地址空间为0~16的散列区中,对以下关键字序列构造两个哈希表: +##### (Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec) +##### (1)用线性探测开放定址法处理冲突; +##### (2)用链地址法处理。 +##### 并分别求这两个哈希表在等概率情况下查找成功和不成功时的平均查找长度。 +##### 设哈希函数为H(x)= └i/2┘,其中i为关键字中第一个字母在字母表中的序号。 + +> (1)处理冲突时采用序号增一的方式,结合题设可得: +> H(Jan)=5; +> H(Feb)=3; +> H(Mar)=6; +> H(Apr)=0; +> H(June)=5; H(June)=6; H(June)=7; +> H(May)=6; H(May)=7; H(May)=8; +> H(July)=5; H(July)=6; H(July)=7; H(July)=8; H(July)=9; +> H(Aug)=0; H(Aug)=1; +> H(Sep)=9; H(Sep)=10; +> H(Oct)=7; H(Oct)=8; H(Oct)=9; H(Oct)=10; H(Oct)=11; +> H(Nov)=7; H(Nov)=8; H(Nov)=9; H(Nov)=10; H(Nov)=11; H(Nov)=12; +> H(Dec)=2; +> ![9.21.1](_v_images/20181214220147072_20491.png) +> +> 查找成功时,待查找字符串肯定是题设中给出的12个字串之一,故其查找成功的平均查找长度为: +> ASL=(1+2+1+1+1+1+3+3+5+2+5+6)/12=31/12 +> +> 查找失败时,待查找字符串可能是任意字串,它的首字母的取值范围是[0,13](1/2=0, 26/2=13)。 +> 当待查字串的哈希值为0时,需要与0,1,2,3,4比较5次才能确定不匹配; +> 当待查字串的哈希值为1时,需要与1,2,3,4比较4次才能确定不匹配; +> ... +> 当待查字串的哈希值为4时,只需与4比较1次就能确定不匹配; +> ... +> 当待查字串的哈希值为12时,需要与12,13比较2次才能确定不匹配; +> 当待查字串的哈希值为13时,只需与13比较1次就能确定不匹配; +> 故其查找失败的平均长度为: +> ASL=(5+4+3+2+1+9+8+7+6+5+4+3+2+1)/14=60/14 +> +> +> (2)按照平均查找长度的定义,公式中的Ci是指:“关键字与给定值比较的个数”则在用链地址处理冲突时,和“空指针”的比较不计在内。 +> H(Jan)=5; H(June)=5; H(July)=5; +> H(Feb)=3; +> H(Mar)=6; H(May)=6; +> H(Apr)=0; H(Aug)=0; +> H(Sep)=9; +> H(Oct)=7; H(Nov)=7; +> H(Dec)=2; +> ![9.21.2](_v_images/20181214220301460_10605.png) +> +> 查找成功时,待查找字符串肯定是题设中给出的12个字串之一: +> 若给定字串是Apr,则需在0处比对一次; +> 若给定字串是Aug,则需在0处比对两次; +> ... +> 若给定字串是July,则需在5处比对三次; +> ... +> 故其查找成功的平均查找长度为: +> ASL=(1*7+2*4+3*1)/12=18/12 +> +> 查找失败时,字串的哈希值取值范围仍为[0,13]: +> 若给定字串的哈希值为0,则需与Apr、Aug分别比较2次后才能确定失配; +> 若给定字串的哈希值为2,则需与Dec比较1次后就能确定失配; +> ... +> 若给定字串的哈希值为9,则需与Sep比较1次后就能确定失配。 +> 故查找失败的平均长度: +> ASL=(2+1+1+3+2+2+1)/14=12/14 + +### 9.22 已知一个含有1000个记录的表,关键字为中国人姓氏的拼音,请给出此表的一个哈希表设计方案,要求它在等概率情况下查找成功的平均查找长度不超过3。 + +> 暂未想到合适的解答。如有好的见解,欢迎提交Issues。 + +### 9.23 设有一个关键字取值范围为正整数的哈希表,空表项的值为-1,用开放定址法解决冲突。现有两种删除策略:一是将待删表项的关键字置为-1;二是将探测序列上的关键字顺序递补,即用探测序列上下一个关键字覆盖待删关键字,并将原序列上之后一个关键字置为-1。这两种方法是否可行?为什么?给出一种可行的方法,并叙述它对查找和插入算法所产生的影响。 + +> 两种方案都不行。前者切断了探测链,后者可能移动了非同义词。 +> +> 一种可行的解决方案是:将待删除的关键字置为0,以区别于空表项。查找和插入算法都应相应地进行调整。 + +### 9.24 某校学生学号由8位十进制数字组成:C1C2C3C4C5C6C7C8。C1C2为入学时年份的后两位;C3C4为系别:00~24分别代表该校的25个系;C5为0或1,0表示本科生,1表示研究生;C6C7C8为对某级某系某类学生的顺序编号:对于本科生,它不超过199,对于研究生,它不超过049,共有4个年级,四年级学生1996年入学。 +##### (1)当在校生人数达极限情况时,将他们的学号散列到0~24999的地址空间,问装载因子是多少? +##### (2)求一个无冲突的哈希函数H1,它将在校生学号散列到。0~24999的地址空间。其簇聚性如何? +##### (3)设在校生总数为15000人,散列地址空间为,0~19999,你是否能找到一个(2)中要求的H1?若不能,试设计一个哈希函数H2及其解决冲突的方法,使得多数学号可只经一次散列得到(可设各系各年级本科生平均人数为130,研究生平均人数为20)。 +##### (4)用算法描述语言表达H2,并写出相应的查找函数。 + +> 暂未想到合适的解答。如有好的见解,欢迎提交Issues。 + +## 二、算法设计题 + +### 9.25 假设顺序表按关键字自大至小有序,试改写教科书9.1.1节中的顺序查找算法,将监视哨设在高下标端。然后画出描述此查找过程的判定树,分别求出等概率情况下查找成功和不成功时的平均查找长度。 + +> [Question-9.25-main.c](▼习题测试文档-09/Question-9.25-main.c) + +### 9.26 试将折半查找的算法改写成递归算法。 +### 9.27 改写教科书9.1.2节中折半查找的算法,当r[i].key≤K<r[i+1].key(i=1,2,…,n-1)时,返回i;当K [Question-9.26~9.27-main.c](▼习题测试文档-09/Question-9.26~9.27-main.c) + +### 9.28 试编写利用折半查找确定记录所在块的分块查找算法。并讨论在块中进行顺序查找时使用“监视哨”的优缺点,以及必要时如何在分块查找的算法中实现设置“监视哨”的技巧。 + +> [Question-9.28-main.c](▼习题测试文档-09/Question-9.28-main.c) + +### 9.29 已知一非空有序表,表中记录按关键字递增排列,以不带头结点的单循环链表作存储结构,外设两个指针h和t,其中h始终指向关键字最小的结点,t则在表中浮动,其初始位置和h相同,在每次查找之后指向刚查到的结点。查找算法的策略是:首先将给定值K和t->key进行比较,若相等,则查找成功;否则因K小于或大于t->key而从h所指结点或t所指结点的后继结点起进行查找。 +##### (1)按上述查找过程编写查找算法; +##### (2)画出描述此查找过程的判定树,并分析在等概率查找时查找成功的平均查找长度(假设表长为n,待查关键码K等于每个结点关键码的概率为1/n,每次查找都是成功的,因此在查找时,t指向每个结点的概率也为1/n)。 + +> [Question-9.29-main.c](▼习题测试文档-09/Question-9.29-main.c) + +### 9.30 将9.28题的存储结构改为双向循环链表,且外设一个指针sp,其初始位置指向关键字最小的结点,在每次查找之后指向刚查到的结点。查找算法的策略是:首先将给定值K和sp->key进行比较,若相等,则查找成功;否则依K小于或大于sp->key继续从*sp的前驱或后继结点起进行查找。编写查找算法并分析等概率查找时查找成功的平均查找长度。 + +> [Question-9.30-main.c](▼习题测试文档-09/Question-9.30-main.c) + +### 9.31 试写一个判别给定二叉树是否为二叉排序树的算法,设此二叉树以二叉链表作存储结构。且树中结点的关键字均不同。 +### 9.32 已知一棵二叉排序树上所有关键字中的最小值为-max,最大值为max,又-max [Question-9.31~9.34-main.c](▼习题测试文档-09/Question-9.31~9.34-main.c) + +### 9.35 假设二叉排序树以后继线索链表作存储结构,编写输出该二叉排序树中所有大于a且小于b的关键字的算法。 +### 9.36 同9.35题的结构,编写在二叉排序树中插入一个关键字的算法。 +### 9.37 同9.35题的结构,编写从二叉排序树中删除一个关键字的算法。 + +> [Question-9.35~9.37-main.c](▼习题测试文档-09/Question-9.35~9.37-main.c) + +### 9.38 试写一算法,将两棵二叉排序树合并为一棵二叉排序树。 +### 9.39 试写一算法,将一棵二叉排序树分裂为两棵二叉排序树,使得其中一棵树的所有结点的关键字都小于或等于x,另一棵树的任一结点的关键字均大于x。 + +> [Question-9.38~9.39-main.c](▼习题测试文档-09/Question-9.38~9.39-main.c) + +### 9.40 在平衡二叉排序树的每个结点中增设一个lsize域,其值为它的左子树中的结点数加1。试写一时间复杂度为O(logn)的算法,确定树中第k小的结点的位置。 + +> [Question-9.40-main.c](▼习题测试文档-09/Question-9.40-main.c) + +### 9.41 为B+树设计结点类型并写出算法,随机(而不是顺序)地查找给定的关键字K,求得它所在的叶子结点指针和它在该结点中的位置(提示:B+树中有两种类型的指针,结点结构也不尽相同,考虑利用记录的变体)。 + +> [Question-9.41-main.c](▼习题测试文档-09/Question-9.41-main.c) + +### 9.42 假设Trie树上叶子结点的最大层次为h,同义词放在同一叶子结点中,试写在Trie树中插入一个关键字的算法。 +### 9.43 同9.42的假设,试写在Trie树中删除一个关键字的算法。 + +> [Question-9.42~9.43-main.c](▼习题测试文档-09/Question-9.42~9.43-main.c) + +### 9.44 已知某哈希表的装载因子小于1,哈希函数H(key)为关键字(标识符)的第一个字母在字母表中的序号,处理冲突的方法为线性探测开放定址法。试编写一个按第一个字母的顺序输出哈希表中所有关键字的算法。 + +> [Question-9.44-main.c](▼习题测试文档-09/Question-9.44-main.c) + +### 9.45 假设哈希表长为m,哈希函数为H(x),用链地址法处理冲突。试编写输入一组关键字并建造哈希表的算法。 + +> [Question-9.45-main.c](▼习题测试文档-09/Question-9.45-main.c) + +### 9.46 假设有一个1000×1000的稀疏矩阵,其中1%的元素为非零元素,现要求用哈希表作存储结构。试设计一个哈希表并编写相应算法,对给定的行值和列值确定矩阵元素在哈希表上的位置。请将你的算法与在稀疏矩阵的三元组表存储结构上存取元素的算法(不必写出)进行时间复杂度的比较。 + +> [Question-9.46-main.c](▼习题测试文档-09/Question-9.46-main.c) + + + + + + +