我被要求为具有x,y和z坐标的数据集合编写BTS代码,其具有负数和正数,并且要求如下:
该程序能够根据数据集执行二叉树搜索。
请使用此电子邮件中提供的数据。它是X,Y和Z中坐标的集合
因此用户将输入X坐标,它将在输出中一起显示Y和Z,另一个是当用户想要搜索X的所有正/负时,它还将显示Y和Z X。 到目前为止我只知道二进制搜索只能对单个数字进行搜索,但不能对内部有3个数字的元素进行搜索。
这是X,Y和Z数据的少数集合,例如:
-0.090729 0.122568 0.030209 <--- one line of X Y Z coordinate
-0.179660 0.154953 0.033881
-0.335793 0.244996 0.269589
0.075957 0.149626 0.114472
我也将使用此代码作为基础
#include <cstdlib>
#include <iostream>
using namespace std;
int binary_search(int array[],int first,int last, int value);
int main() {
int list[10];
for (int k=0; k<11; k++)
list[k]=2*k+1;
cout<< "binary search results: "<< binary_search(list,1,21,11)<<endl;
return 0;
}//end of main
int binary_search(int array[],int first,int last, int search_key)
{
int index;
if (first > last)
index = -1;
else
{
int mid = (first + last)/2;
if (search_key == array[mid])
index = mid;
else
if (search_key < array[mid])
index = binary_search(array,first, mid-1, search_key);
else
index = binary_search(array, mid+1, last, search_key);
} // end if
return index;
}// end binarySearch
我还在考虑使用数组是不够的,我必须创建一个单独的文件,如数据库,用户可以使用一个输入搜索数据。 所以我有一个想法,我只为X坐标做二叉树,但是当用户输入X时,它会用数据集合中的X显示Y和Z,数据集合也很多,任何专家的将不胜感激:)仍然想知道二元搜索树是否能够做到这一点....
所以这是我需要使用的数据集合,有人可以告诉我如何制作能够从这个庞大的集合中调用数据的函数吗?
0.075957 0.149626 0.114472
0.000905 0.131220 0.031000
0.334059 -0.004790 0.207368
0.380561 -0.016845 0.110792
-0.083847 -0.009495 0.260374
0.316664 0.071467 0.120474
-0.307909 0.341245 -0.115581
0.411077 -0.098945 0.029724
-0.093728 0.413972 -0.045792
-0.445182 0.173948 0.306924
-0.339452 0.094466 0.338246
0.154974 0.153764 0.113986
0.383043 -0.017294 0.038540
0.371618 -0.036996 -0.034430
-0.488796 0.174331 0.109211
-0.425485 -0.003917 0.201166
-0.328634 0.319858 0.114390
-0.410262 0.298180 0.193996
0.155009 0.097454 0.225033
-0.225622 0.246139 0.118317
-0.399749 0.163849 0.335552
0.076578 0.133341 0.177679
-0.201047 0.142376 0.174390
-0.429153 -0.098775 -0.040573
0.230882 0.131755 0.118676
-0.483743 0.082920 0.040329
-0.078715 0.471042 -0.055900
-0.411718 0.237937 0.278383
-0.005249 0.003769 0.270521
-0.018101 0.489653 -0.124951
-0.133572 0.398057 -0.090000
0.154095 0.134174 0.180583
-0.172136 0.147088 0.112057
0.240332 0.079992 0.210373
-0.175250 0.391765 0.011504
-0.477955 0.162864 0.042821
-0.263429 0.246060 0.204495
0.408153 -0.094551 0.112774
0.388142 -0.103007 0.184936
-0.257106 0.309349 0.107726
-0.020619 0.343770 -0.051245
-0.366778 -0.278556 0.116261
-0.329815 0.313916 0.034602
-0.482418 0.071724 0.268572
0.296142 0.067455 0.185325
0.392588 -0.106540 -0.040632
-0.348415 0.380318 -0.132477
0.074743 0.098941 0.226089
0.313984 -0.095701 0.274736
-0.000127 0.359321 -0.099978
-0.389304 0.419894 -0.275472
-0.448952 -0.015681 0.018614
0.360441 0.017741 0.133138
0.072016 -0.429793 0.306404
0.157903 -0.423369 0.305657
-0.253995 0.329448 0.039563
-0.486770 0.073079 0.193422
-0.277697 0.136104 0.260296
-0.000924 0.131167 0.114723
-0.324768 0.304498 0.200203
-0.172816 0.407647 -0.040188
-0.262926 -0.426393 0.279458
-0.389169 0.326903 -0.115444
-0.171523 0.319164 0.036876
-0.166670 -0.433521 0.278390
-0.052386 -0.126211 0.329163
0.021908 0.122344 0.172256
-0.467692 -0.118237 0.042218
-0.339549 0.330551 -0.049267
-0.333589 0.232102 -0.053896
0.240226 -0.009687 0.271713
-0.125529 0.419457 -0.000220
-0.323461 0.062386 -0.069065
-0.335525 0.413992 -0.292639
-0.485317 0.253072 0.122003
0.160470 0.053417 0.252663
0.320594 -0.097082 -0.121452
0.153275 -0.012350 0.284012
-0.351329 0.151160 0.328284
0.155106 0.152666 0.034476
0.237370 -0.432505 0.276914
0.213352 -0.110928 0.333017
0.076081 0.050562 0.261882
-0.420589 0.067501 -0.027579
-0.404542 0.241335 -0.029536
0.154083 -0.010130 -0.141933
-0.011780 0.079933 0.212997
0.076419 0.148417 0.035528
-0.474790 0.231229 0.188058
0.065221 0.102536 -0.068695
-0.341908 0.470328 -0.362529
-0.081834 0.109006 0.134754
-0.445012 0.272483 0.212757
-0.471614 -0.110944 0.114503
-0.086541 0.096661 -0.045842
-0.087413 0.067613 0.198527
-0.400299 0.085220 0.335886
0.335514 0.004886 -0.052942
-0.331894 -0.098257 -0.115394
-0.384468 -0.109560 -0.094346
-0.429120 -0.083583 0.201641
-0.450774 0.192619 0.008875
-0.077900 -0.436097 0.335867
-0.001575 -0.432054 0.337764
-0.318593 0.418017 -0.378191
-0.081679 0.417342 -0.113276
-0.256147 0.180127 0.214579
-0.324634 0.166962 0.291635
-0.165520 0.315139 -0.053349
-0.244783 -0.239901 0.258588
-0.157444 -0.112794 -0.153111
0.001362 -0.102979 0.350567
0.076088 -0.090730 0.354817
0.395664 -0.267686 0.191676
0.304469 0.060476 -0.029716
-0.103680 -0.414289 0.294346
-0.240880 0.071427 0.229963
0.395003 -0.186405 -0.042120
0.312090 -0.430972 0.197739
-0.092992 -0.078157 -0.160328
0.367958 -0.017433 0.172606
-0.072734 -0.114118 -0.196052
0.001156 -0.422750 0.282151
0.140519 -0.396572 0.327980
0.401578 -0.409301 0.180077
-0.038587 0.485641 -0.078435
0.078437 -0.338751 0.352289
0.150609 -0.344197 0.348366
-0.468113 -0.013229 0.110504
-0.258706 -0.401441 0.198291
-0.080173 -0.353192 0.259849
0.076135 -0.012304 0.287421
-0.025898 -0.350688 0.291216
0.008889 -0.332588 0.332609
-0.259982 0.255333 -0.021288
-0.500000 0.074738 0.115107
-0.293236 0.334808 -0.197488
-0.450030 -0.006072 0.139787
0.338110 -0.354940 0.228053
0.398298 -0.341848 0.204755
-0.171607 0.062339 0.205716
-0.334439 0.323599 -0.285487
0.295641 -0.030420 0.258598
-0.078645 0.467777 -0.100027
-0.388458 -0.109277 0.249584
-0.010804 -0.060563 0.298001
-0.235267 -0.337188 0.190461
-0.169882 -0.344871 0.254487
-0.434959 0.052108 0.305670
0.211018 0.127704 0.169978
-0.141419 0.105761 0.161259
-0.277201 0.080277 0.260381
-0.348562 0.307347 -0.210293
-0.264058 0.296204 0.184212
-0.201645 -0.339661 0.235866
0.044728 0.475219 -0.187208
-0.467501 -0.011187 0.052141
-0.406464 0.305509 0.039260
-0.079797 0.121035 0.092894
-0.424583 0.308193 0.113378
-0.410118 -0.023560 -0.044817
-0.048690 0.100157 0.158385
-0.323503 0.344986 -0.356561
-0.403118 -0.246061 0.111481
0.051702 0.432676 -0.178928
-0.258646 -0.350652 0.045387
-0.005999 0.095570 -0.055052
-0.344346 0.255465 -0.122999
-0.157020 0.102027 -0.067144
-0.337089 0.267242 -0.193677
-0.241299 -0.345101 0.113604
0.003790 -0.261338 0.347452
0.075487 -0.264686 0.369264
0.162459 -0.267253 0.361906
0.020993 0.060885 0.246241
0.471800 -0.343702 0.172142
0.149844 -0.096394 0.350343
-0.169260 -0.266480 0.265656
-0.078075 -0.262286 0.267258
-0.135235 0.358184 -0.088433
-0.255263 -0.279737 0.222196
-0.387804 0.315919 -0.042996
-0.249325 0.097467 -0.066377
0.232603 -0.011736 -0.126844
0.468932 -0.278385 0.170881
-0.458816 -0.088186 0.015550
-0.389489 0.356552 -0.200456
-0.175817 0.130162 -0.025492
-0.091194 -0.184586 0.283638
-0.247519 -0.009323 -0.126505
-0.362708 0.015190 -0.066164
-0.250302 -0.104264 -0.143793
-0.206967 -0.275905 0.243332
-0.197050 0.247164 0.041679
-0.415699 0.144577 -0.034869
-0.160657 -0.012060 -0.135141
0.073840 -0.096450 -0.221220
0.076158 -0.010711 -0.150732
-0.079237 0.333988 -0.044751
-0.001993 -0.009713 -0.147751
-0.469462 0.198805 0.256553
-0.330455 0.136368 -0.060891
0.422080 -0.239554 0.139433
-0.155069 0.062187 -0.100190
0.045128 0.395109 -0.135353
-0.236349 0.328456 -0.022454
0.150712 -0.095474 -0.203321
-0.395556 -0.231558 0.172341
-0.297510 0.278742 -0.103171
-0.339099 -0.258986 0.198846
-0.244746 -0.177858 -0.137134
-0.173047 -0.190625 0.279613
-0.007145 -0.190266 0.364539
0.075964 -0.187064 0.378191
0.162311 -0.175585 0.365091
-0.446336 0.252967 0.034904
0.360230 0.017437 0.013711
-0.335841 -0.181269 0.265241
-0.253792 -0.188356 0.283896
0.367728 -0.129890 -0.095475
-0.309241 -0.450194 0.258058
-0.346159 0.401686 -0.210900
-0.292800 0.317122 -0.141242
-0.296030 0.360867 -0.265735
-0.338339 -0.101703 0.275068
-0.210562 0.208448 0.093963
-0.402655 0.015646 0.272364
0.234197 -0.188255 0.339100
0.150828 0.105100 -0.069723
-0.252361 -0.094102 0.288347
-0.169846 -0.103389 0.282147
-0.081747 -0.090851 0.290155
0.251737 -0.136450 0.321562
0.229362 0.131550 0.030055
-0.076129 -0.006871 -0.140948
0.238180 -0.085572 0.311821
0.076712 0.058579 -0.109601
-0.233470 0.362778 0.015787
0.352277 -0.078644 -0.083349
-0.487177 0.156348 0.191350
-0.332970 -0.005332 0.263005
-0.176265 -0.018775 0.261670
0.165160 -0.062183 0.318872
-0.251163 -0.025516 0.268080
-0.326562 0.052598 0.309106
-0.215845 0.215242 0.013283
-0.250863 0.162990 -0.035906
0.018121 0.489990 -0.177498
-0.079734 0.357054 -0.098577
-0.255718 0.023253 0.243169
-0.155250 0.018091 0.241299
-0.095910 0.017906 0.241005
0.280869 0.018851 0.239697
-0.194925 0.262219 -0.018530
-0.005598 -0.479311 0.331880
-0.002292 0.414560 -0.123543
-0.260753 -0.481258 0.276218
0.154172 -0.480161 0.274365
-0.167268 -0.479832 0.271694
-0.323638 -0.019706 -0.102023
0.285772 -0.408742 0.248108
0.366403 -0.172115 -0.097373
-0.442366 -0.184972 0.119327
0.467558 -0.397663 0.116399
0.238829 -0.357919 0.301619
0.164031 0.069292 -0.104950
-0.092374 0.325879 0.002629
0.304791 -0.336291 0.265589
0.500000 -0.348679 0.115310
0.227075 -0.117799 -0.181677
0.211965 -0.325049 0.327752
0.358338 -0.105352 0.238236
-0.015772 0.387595 -0.078307
0.214950 -0.264517 0.340296
0.254757 -0.262003 0.316288
0.313084 -0.262813 0.273214
0.359071 -0.263765 0.238343
0.495477 -0.261305 0.109474
0.325024 -0.193317 -0.130771
-0.386855 0.449060 -0.338864
-0.317542 -0.310432 0.112304
-0.417095 -0.173898 0.198758
0.241026 0.090858 -0.049727
0.318413 -0.186461 0.281366
0.361404 -0.188428 0.239352
0.392783 -0.186163 0.191749
0.419850 -0.187629 0.113618
-0.405129 -0.244415 0.041488
-0.078681 0.053919 -0.101020
-0.459326 0.038064 0.023302
0.021646 0.437884 -0.175533
0.231143 -0.479464 -0.043005
0.222780 -0.475271 -0.110977
0.243339 -0.475372 0.203179
0.403476 -0.440490 0.034505
0.265111 -0.443827 -0.056269
0.242083 -0.429440 -0.130781
0.389655 -0.410616 -0.027253
0.404388 -0.439917 0.113291
0.454583 -0.405555 0.034545
0.304492 -0.017036 -0.097924
0.318352 -0.418003 -0.044667
-0.006068 -0.085607 -0.207010
-0.092889 -0.485483 -0.191576
0.287708 -0.397662 -0.113398
0.481895 -0.258555 0.035148
0.459541 -0.348207 -0.020224
0.490604 -0.353829 0.030048
0.398522 -0.346518 -0.043788
-0.294172 0.394323 -0.352233
0.341075 -0.354125 -0.066500
0.310707 -0.341708 -0.120061
-0.001949 0.053880 -0.108140
-0.160536 -0.475848 -0.178375
0.081321 -0.427695 -0.184472
-0.323267 -0.485070 -0.031361
0.429826 -0.241506 0.032040
0.280277 -0.166830 -0.166326
-0.082011 -0.430622 -0.190980
0.259778 -0.371250 -0.160124
-0.063844 -0.479809 0.332759
0.222145 0.052023 -0.099953
0.325379 -0.262782 -0.125122
0.392030 -0.265942 -0.040276
0.455904 -0.291006 -0.015375
-0.474778 0.135820 0.279064
0.419232 -0.185902 0.034486
-0.350612 0.278498 -0.259807
-0.168321 -0.427839 -0.120334
-0.360026 0.463230 -0.317495
-0.387025 0.461928 -0.315415
0.316651 0.070958 0.034101
-0.394244 0.396200 -0.201302
0.249565 -0.098461 -0.160699
-0.052792 -0.184322 0.333604
-0.314333 -0.478718 0.103402
-0.312439 -0.480349 0.190804
-0.310186 -0.438183 0.197410
-0.309481 -0.479378 0.252555
-0.331159 -0.487284 0.035204
-0.333495 -0.435541 0.040352
-0.263413 -0.426976 0.128511
-0.303818 -0.438361 0.090061
-0.277050 -0.400649 0.037718
-0.038925 -0.278358 0.294233
-0.304313 -0.238548 0.246970
-0.011962 -0.176767 -0.257824
-0.166728 -0.485130 -0.130257
0.070000 -0.483251 -0.137440
-0.318461 -0.442320 -0.030378
-0.138111 -0.435125 -0.178330
-0.252236 -0.426105 -0.053975
-0.159840 -0.434426 -0.069893
-0.097390 -0.417033 -0.114525
-0.087536 -0.339895 -0.126204
-0.167931 -0.340827 -0.078534
-0.248072 -0.337747 -0.038942
-0.057447 -0.336240 -0.190659
-0.249331 0.064124 -0.092216
-0.010390 -0.350874 -0.214420
-0.338845 -0.258346 -0.047109
-0.367182 -0.279822 0.032717
-0.021345 -0.267534 -0.245406
-0.076570 -0.266564 -0.195832
-0.101198 -0.272924 -0.132843
-0.001765 -0.485288 -0.175093
-0.395722 -0.228026 -0.030905
0.215824 0.121247 -0.021429
-0.443022 -0.183083 0.031252
-0.408828 -0.184413 -0.046159
-0.089085 -0.186963 -0.210342
-0.119101 -0.171558 -0.153775
-0.334735 -0.182650 -0.112396
-0.223385 -0.485981 -0.089089
0.222636 -0.180295 -0.196924
0.020131 -0.131068 -0.255166
0.001177 -0.426232 -0.190799
-0.012155 -0.064992 -0.167676
-0.251505 -0.489586 -0.044734
0.162056 -0.065542 -0.166444
0.017214 0.121009 -0.020486
0.231414 -0.347292 -0.184091
0.052590 -0.187358 -0.254877
0.150268 -0.453553 -0.148873
0.231710 -0.263207 -0.195083
0.077347 0.134286 -0.022666
0.157028 0.134088 -0.024777
0.152628 -0.483117 -0.122336
0.312729 -0.474830 0.028494
0.156938 -0.408421 -0.178160
0.210296 -0.399260 -0.170051
0.155543 -0.345337 -0.204948
0.075077 -0.351087 -0.219512
-0.113516 -0.334652 -0.079797
0.070376 -0.262941 -0.241359
0.152999 -0.265258 -0.221395
-0.260925 -0.281522 -0.067153
-0.163813 -0.249589 -0.110536
-0.000517 -0.245935 -0.256464
-0.251757 -0.234363 -0.110374
-0.305968 -0.241161 -0.095382
0.153196 -0.189709 -0.227997
0.093251 -0.180480 -0.246527
-0.048941 -0.173545 -0.249073
-0.157017 -0.189159 -0.134516
-0.370694 -0.198645 -0.084469
-0.003756 -0.479136 -0.132966
-0.093078 -0.475263 -0.122568
0.075829 -0.460227 -0.099777
-0.001899 -0.459362 -0.105936
0.165415 -0.477174 -0.046786
0.144067 -0.458390 -0.051246
0.076365 -0.454779 -0.042957
-0.000796 -0.457043 -0.042454
-0.168778 -0.486562 -0.041229
0.240678 -0.480161 0.029208
-0.082080 -0.458459 -0.042333
0.149462 -0.481931 0.033089
0.122461 -0.463743 0.002471
0.076974 -0.458785 0.035224
-0.165019 -0.485403 0.035211
-0.002507 -0.458952 0.035797
0.305050 -0.475313 0.120096
-0.248056 -0.489990 0.034763
-0.072565 -0.460061 0.025380
0.109360 -0.475999 0.064561
0.243323 -0.479673 0.126123
-0.110629 -0.489074 0.043599
0.151045 -0.483545 0.115056
0.113069 -0.479908 0.084853
0.336442 -0.463570 0.135023
0.076638 -0.459867 0.113200
-0.117317 -0.480495 0.113990
-0.002076 -0.459748 0.113895
-0.057473 -0.462636 0.113188
-0.256639 -0.489621 0.112012
0.123161 -0.463285 0.143835
0.181877 -0.477548 0.192178
0.151977 -0.457373 0.192210
0.076190 -0.455132 0.193957
-0.102975 -0.477424 0.189320
-0.067928 -0.459351 0.209563
-0.002745 -0.457144 0.191918
-0.161571 -0.485037 0.194232
0.216362 -0.476830 0.268931
-0.250727 -0.485793 0.193098
-0.320925 -0.308279 0.034877
0.072334 -0.480626 0.296205
-0.087828 -0.482461 0.282326
-0.003897 -0.482100 0.299386
如果我将所有数据放入data.txt文件并在main.cpp中读取它,这段代码是否可行?
int main ()
{
{
ifstream myReadFile;
myReadFile.open("Data.txt");
char output[100];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
这是顶点X的冒泡排序,我知道排序需要很长时间,但只要代码有效,时间就没关系了。
for (i=0; i<500-1; i++)
for (int j=i+1; j<500; j++)
if (X[i] > X[j])
{
int temp = X[i];
X[i] = X [j];
X[j] = temp;
temp = Y[i];
Y[i] = Y [j];
Y[j] = temp;
temp = Z[i];
Z[i] = Z [j];
Z[j] = temp;
}
答案 0 :(得分:1)
您谈论的是binary search trees,但您显示的代码在已排序的数组上执行了binary search。那不是一回事。
使用二进制搜索的一种方法是将X,Y和Z值放在一个数组或自定义结构中。类似的东西:
struct Vertex {
float X;
float Y;
float Z;
};
修改二进制搜索功能,使其获取这些结构的数组而不是整数数组。您必须使搜索键成为X浮点值:
int binary_search(Vertex array[],int first,int last, float x_value);
将X,Y,Z值加载到Vertex
数组中,然后按X值排序:
bool vertexXComparator(const Vertex& lhs, const Vertex& rhs) {
return lhs.X < rhs.X;
}
std::sort(list, list+number_of_vertexes, vertexXComparator);
然后你应该能够在排序列表中使用二进制搜索(或std :: binary_search)。
我会谨慎使用float作为搜索键。 It is generally a bad idea测试浮点数是否相等。