//
// main.cpp
// Binary Search Tree (Basic)
//
// Created by Himanshu on 16/09/21.
//
#include <iostream>
using namespace std;
struct node {
int info = 0;
struct node *left, *right;
};
typedef struct node Node;
node* newNode(int data)
{
node* Node = new node();
Node->info = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
Node* insert (Node *root, int n) {
if (root == NULL) {
root = newNode(n);
} else {
if (root->info > n) {
root->left = insert (root->left, n);
} else {
root->right = insert (root->right, n);
}
}
return root;
}
Node* search (Node *root, int n) {
if (root == NULL || root->info == n) {
return root;
} else {
if (root->info > n) {
return search (root->left, n);
} else {
return search (root->right, n);
}
}
}
int main() {
Node *root = newNode(5);
insert(root, 3);
insert(root, 2);
insert(root, 4);
insert(root, 7);
//Searching 4 in the tree
if (search (root, 4)) {
cout<<"4 is present in the BST"<<endl;
} else {
cout<<"4 is not in the BST"<<endl;
}
//Searching 8 in the tree
if (search (root, 8)) {
cout<<"8 is present in the BST"<<endl;
} else {
cout<<"8 is not in the BST"<<endl;
}
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBCaW5hcnkgU2VhcmNoIFRyZWUgKEJhc2ljKQovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxNi8wOS8yMS4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBub2RlIHsKICAgIGludCBpbmZvID0gMDsKICAgIHN0cnVjdCBub2RlICpsZWZ0LCAqcmlnaHQ7Cn07CnR5cGVkZWYgc3RydWN0IG5vZGUgTm9kZTsKCm5vZGUqIG5ld05vZGUoaW50IGRhdGEpCnsKICAgIG5vZGUqIE5vZGUgPSBuZXcgbm9kZSgpOwogICAgTm9kZS0+aW5mbyA9IGRhdGE7CiAgICBOb2RlLT5sZWZ0ID0gTlVMTDsKICAgIE5vZGUtPnJpZ2h0ID0gTlVMTDsKIAogICAgcmV0dXJuKE5vZGUpOwp9CgpOb2RlKiBpbnNlcnQgKE5vZGUgKnJvb3QsIGludCBuKSB7CiAgICBpZiAocm9vdCA9PSBOVUxMKSB7CiAgICAgICAgcm9vdCA9IG5ld05vZGUobik7CiAgICB9IGVsc2UgewogICAgICAgIGlmIChyb290LT5pbmZvID4gbikgewogICAgICAgICAgICByb290LT5sZWZ0ID0gaW5zZXJ0IChyb290LT5sZWZ0LCBuKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByb290LT5yaWdodCA9IGluc2VydCAocm9vdC0+cmlnaHQsIG4pOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiByb290Owp9CgpOb2RlKiBzZWFyY2ggKE5vZGUgKnJvb3QsIGludCBuKSB7CiAgICBpZiAocm9vdCA9PSBOVUxMIHx8IHJvb3QtPmluZm8gPT0gbikgewogICAgICAgIHJldHVybiByb290OwogICAgfSBlbHNlIHsKICAgICAgICBpZiAocm9vdC0+aW5mbyA+IG4pIHsKICAgICAgICAgICAgcmV0dXJuIHNlYXJjaCAocm9vdC0+bGVmdCwgbik7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIHNlYXJjaCAocm9vdC0+cmlnaHQsIG4pOwogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBOb2RlICpyb290ID0gbmV3Tm9kZSg1KTsKICAgIGluc2VydChyb290LCAzKTsKICAgIGluc2VydChyb290LCAyKTsKICAgIGluc2VydChyb290LCA0KTsKICAgIGluc2VydChyb290LCA3KTsKICAgIAogICAgLy9TZWFyY2hpbmcgNCBpbiB0aGUgdHJlZQogICAgaWYgKHNlYXJjaCAocm9vdCwgNCkpIHsKICAgICAgICBjb3V0PDwiNCBpcyBwcmVzZW50IGluIHRoZSBCU1QiPDxlbmRsOwogICAgfSBlbHNlIHsKICAgICAgICBjb3V0PDwiNCBpcyBub3QgaW4gdGhlIEJTVCI8PGVuZGw7CiAgICB9CgogICAgLy9TZWFyY2hpbmcgOCBpbiB0aGUgdHJlZQogICAgaWYgKHNlYXJjaCAocm9vdCwgOCkpIHsKICAgICAgICBjb3V0PDwiOCBpcyBwcmVzZW50IGluIHRoZSBCU1QiPDxlbmRsOwogICAgfSBlbHNlIHsKICAgICAgICBjb3V0PDwiOCBpcyBub3QgaW4gdGhlIEJTVCI8PGVuZGw7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cg==