Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <cmath>
- using namespace std;
- int data[10*1024*1024];
- void Insert(int pdata){
- int n=0;
- while (data[n]>0)
- if (pdata>data[n]) n=2*n+2;
- else if (pdata<data[n]) n=2*n+1;
- else { return; break;}
- data[n]=pdata;
- }
- bool Search(int a){
- int n=0;
- while (data[n]>0){
- cout <<data[n]<<endl;
- if (a>data[n]) n=2*n+2;
- else if (a<data[n]) n=2*n+1;
- else { return true; break;}
- }
- return false;
- }
- int GetMax(){
- int n=0;
- int np=0;
- while (data[n]>0){
- np=n;
- n=2*n+2;
- }
- return data[np];
- }
- int GetMin(){
- int n=0;
- int np=0;
- while (data[n]>0){
- np=n;
- n=2*n+1;
- }
- return data[np];
- }
- void Tampil(){
- for (int i=2000-128; i<2000; i++)
- cout << data[i] << " ";
- cout << endl;
- }
- int main()
- {
- for (int i=0; i<3000; i++) Insert(random()%5000+5);
- Insert(10000);
- Insert(3);
- Tampil();
- int cari= 0;
- while (cari>=0){
- cin>>cari;
- if (Search(cari)) cout<< "data ditemukan"<<endl;
- else cout<< "data tidak ada"<<endl;
- }
- cout<<"max:"<<GetMax()<<endl;
- cout<<"min:"<<GetMin()<<endl;
- // cout << "Hello World";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement