Advertisement
Sn0weK

cosnietak

Nov 26th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5.     Node(int v): val(v), lewo(NULL), prawo(NULL){}
  6. int val;
  7. Node* lewo;
  8. Node* prawo;   
  9. };
  10.  
  11. bool Search(int y, Node*& root){
  12.     if (root == NULL){
  13.         return false;
  14.     }
  15.     else{
  16.         if (root->val == y){
  17.             return true;
  18.         }
  19.         else{
  20.             if(root->val > y){
  21.                 cout<<"L";
  22.                 return Search(y,root->lewo);
  23.             }
  24.             else{
  25.                 cout<<"P";
  26.                 return Search(y,root->prawo);
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. void Insert(int y, Node*& root){
  33.     if(root==0){
  34.         root=new Node(y);
  35.     }
  36.     else{
  37.         if(root->val < y){
  38.             if(root->prawo == 0){
  39.                 root->prawo=new Node(y);
  40.             }
  41.             else{
  42.                 Insert(y, root->prawo);
  43.             }
  44.         }
  45.         else{
  46.         if(root->lewo==0)
  47.         {
  48.             root->lewo=new Node(y);
  49.             }
  50.             else{
  51.            
  52.         Insert(y,root->lewo);
  53.     }
  54.         }
  55.        
  56.     }
  57. }
  58.  
  59. int main(int argc, char* args[]){
  60.     Node* root = 0;
  61.     int k;
  62.     cin>>k;
  63.     for(int i=0; i<k;++i){
  64.         int y;
  65.         cin>>y;
  66.         Insert(y, root);
  67.     }
  68.     cin>>k;
  69.     cout<<Search(k, root);
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement