
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Search for an Element in a Binary Search Tree
In this program we need to. 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 O(log(n)).
Algorithm
Begin Construct binary search tree for the given unsorted data array by inserting data into tree one by one. Take the input of data to be searched in the BST. Now starting from the root node, compare the data with data part of the node. if data < temp->d, move the temp pointer to the left child. if data > temp->d move the temp pointer to the right child. if data = temp->d print the tree depth where it is found and return to main. Else print item not found. End
Example Code
#include<iostream> using namespace std; struct node { int d; node *left; node *right; }; node* CreateNode(int d) { node *newnode = new node; newnode->d = d; newnode->left = NULL; newnode->right = NULL; return newnode; } node* InsertIntoTree(node* root, int d) { node *temp = CreateNode(d); node *t = new node; t = root; if(root == NULL) root = temp; else { while(t != NULL) { if(t->d < d) { if(t->right == NULL) { t->right = temp; break; } t = t->right; } else if(t->d > d) { if(t->left == NULL) { t->left = temp; break; } t = t->left; } } } return root; } void Search(node *root, int d) { int depth = 0; node *temp = new node; temp = root; while(temp != NULL) { depth++; if(temp->d == d) { cout<<"\nitem found at depth: "<<depth; return; } else if(temp->d > d) temp = temp->left; else temp = temp->right; } cout<<"\n item not found"; return; } int main() { char ch; int n, i, a[10] = {93, 53, 45, 2, 7, 67, 32, 26, 71, 76}; node *root = new node; root = NULL; for (i = 0; i < 10; i++) root = InsertIntoTree(root, a[i]); up: cout<<"\nEnter the Element to be searched: "; cin>>n; Search(root, n); cout<<"\n\n\tDo you want to search more...enter choice(y/n)?"; cin>>ch; if(c == 'y' || c == 'Y') goto up; return 0; }
Output
Enter the Element to be searched: 26 item found at depth: 7 Do you want to search more...enter choice(y/n)? Enter the Element to be searched: 1 item not found Do you want to search more...enter choice(y/n)?
- Related Articles
- Searching for values in an Javascript Binary Search Tree
- Program to find the kth smallest element in a Binary Search Tree in Python
- Python Program to Sort using a Binary Search Tree
- Binary Tree to Binary Search Tree Conversion in C++
- C++ Program to Implement Randomized Binary Search Tree
- Python Program for Depth First Binary Tree Search using Recursion
- Optimal Binary Search Tree
- Checking for univalued Binary Search Tree in JavaScript
- Binary Search Tree in Javascript
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Binary Search Tree - Search and Insertion Operations in C++
- Find the closest element in Binary Search Tree in C++
- Python Program for Binary Search
- 8085 program for Binary search
- C++ Program to Perform Dictionary Operations in a Binary Search Tree

Advertisements