Advertisement
AgungAlfiansyah

Untitled

Nov 19th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int data[10*1024*1024];
  8.  
  9. void Insert(int pdata){
  10. int n=0;
  11. while (data[n]>0)
  12. if (pdata>data[n]) n=2*n+2;
  13. else if (pdata<data[n]) n=2*n+1;
  14. else { return; break;}
  15. data[n]=pdata;
  16. }
  17.  
  18. bool Search(int a){
  19. int n=0;
  20. while (data[n]>0){
  21. cout <<data[n]<<endl;
  22. if (a>data[n]) n=2*n+2;
  23. else if (a<data[n]) n=2*n+1;
  24. else { return true; break;}
  25. }
  26.  
  27. return false;
  28. }
  29.  
  30. int GetMax(){
  31. int n=0;
  32. int np=0;
  33. while (data[n]>0){
  34. np=n;
  35. n=2*n+2;
  36. }
  37. return data[np];
  38. }
  39.  
  40. int GetMin(){
  41. int n=0;
  42. int np=0;
  43. while (data[n]>0){
  44. np=n;
  45. n=2*n+1;
  46. }
  47. return data[np];
  48. }
  49.  
  50. void Tampil(){
  51. for (int i=2000-128; i<2000; i++)
  52. cout << data[i] << " ";
  53. cout << endl;
  54. }
  55.  
  56. int main()
  57. {
  58. for (int i=0; i<3000; i++) Insert(random()%5000+5);
  59. Insert(10000);
  60. Insert(3);
  61.  
  62. Tampil();
  63.  
  64. int cari= 0;
  65. while (cari>=0){
  66. cin>>cari;
  67. if (Search(cari)) cout<< "data ditemukan"<<endl;
  68. else cout<< "data tidak ada"<<endl;
  69. }
  70.  
  71. cout<<"max:"<<GetMax()<<endl;
  72. cout<<"min:"<<GetMin()<<endl;
  73.  
  74. // cout << "Hello World";
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement