Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node{
- Node(int v): val(v), lewo(NULL), prawo(NULL){}
- int val;
- Node* lewo;
- Node* prawo;
- };
- bool Search(int y, Node*& root){
- if (root == NULL){
- return false;
- }
- else{
- if (root->val == y){
- return true;
- }
- else{
- if(root->val > y){
- cout<<"L";
- return Search(y,root->lewo);
- }
- else{
- cout<<"P";
- return Search(y,root->prawo);
- }
- }
- }
- }
- void Insert(int y, Node*& root){
- if(root==0){
- root=new Node(y);
- }
- else{
- if(root->val < y){
- if(root->prawo == 0){
- root->prawo=new Node(y);
- }
- else{
- Insert(y, root->prawo);
- }
- }
- else{
- if(root->lewo==0)
- {
- root->lewo=new Node(y);
- }
- else{
- Insert(y,root->lewo);
- }
- }
- }
- }
- int main(int argc, char* args[]){
- Node* root = 0;
- int k;
- cin>>k;
- for(int i=0; i<k;++i){
- int y;
- cin>>y;
- Insert(y, root);
- }
- cin>>k;
- cout<<Search(k, root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement