 # C++ Programming Code Examples

## C++ > Sorting Searching Code Examples

### Program to Search for an Element in a Binary Search Tree

``` Program to Search for an Element in a Binary Search Tree - Implement binary search to find the existence of a search sequence in a binary search tree. - The worst case time complexity of Binary search is O(n) but for the average case, it is O(log(n)). - Construct binary search tree for the given unsorted data array. - Search the element starting from the root of the tree. - Proceed with the search by comparing an element to the data of the node. - Exit. #include<iostream> using namespace std; // A structure representing a node of a tree. struct node { int data; node *left; node *right; }; // A function creating new node of the tree and assigning the data. node* CreateNode(int data) { node *newnode = new node; newnode->data = data; newnode->left = NULL; newnode->right = NULL; return newnode; } // A function creating binary search tree. node* InsertIntoTree(node* root, int data) { // Create node using data from argument list. node *temp = CreateNode(data); node *t = new node; t = root; // If root is null, assign it to the node created. if(root == NULL) root = temp; else { // Find the position for the new node to be inserted. while(t != NULL) { if(t->data < data ) { if(t->right == NULL) { // If current node is NULL then insert the node. t->right = temp; break; } // Shift pointer to the left. t = t->right; } else if(t->data > data) { if(t->left == NULL) { // If current node is NULL then insert the node. t->left = temp; break; } // Shift pointer to the left. t = t->left; } } } return root; } // A function to search item in a BST. void Search(node *root, int data) { int depth = 0; node *temp = new node; temp = root; // Run the loop untill temp points to a NULL pointer. while(temp != NULL) { depth++; if(temp->data == data) { cout<<"\nData found at depth: "<<depth; return; } // Shift pointer to left child. else if(temp->data > data) temp = temp->left; // Shift pointer to right child. else temp = temp->right; } cout<<"\n Data not found"; return; } int main() { char ch; int n, i, a={88, 45, 78, 4, 9, 14, 26, 35, 48, 96, 47, 83, 41, 99, 73, 41, 25, 99, 27, 97}; node *root = new node; root = NULL; // Construct the BST. for(i = 0; i < 20; i++) root = InsertIntoTree(root, a[i]); up: cout<<"\nEnter the Element to be searched: "; cin>>n; Search(root, n); // Ask user to enter choice for further searching. cout<<"\n\n\tDo you want to search more...enter choice(y/n)?"; cin>>ch; if(ch == 'y' || ch == 'Y') goto up; return 0; } ```