
- 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 Find the Number of occurrences of a given Number using Binary Search approach
This is a C++ program to find the number of occurrences of a given number using Binary Search approach.
Algorithms
Begin function Insert() to insert nodes into the tree: Arguments: root, d. Body of the function: Create node using data from argument list. If tree is completely empty, then insert new node as root. If d = tmp->data, increase the count of that particular node. If d < tmp->data, move the tmp pointer to the left child. If d > tmp->data, move the tmp pointer to the right child. End Begin function SearchNode() to search item in the tree: Arguments: root, d. Body of the function: Run the for loop until temp points to a NULL pointer or data item is found. If d < tmp->data, move the tmp pointer to the left child. If d > tmp->data, move the tmp pointer to the right child. If d = tmp->data, print the item found and it’s count and then return to main. Else Print data not found. End
Example
#include<iostream> using namespace std; struct nod //declaration of node { int data; int cnt; nod *l; nod *r; }; nod* CreateNod(int d) //creation of new node { nod *newnod = new nod; newnod->data = d; newnod->cnt = 1; newnod->l= NULL; newnod->r = NULL; return newnod; } nod* Insert(nod* root, int d) //perform insertion { nod *tmp = CreateNod(d); nod *t = new nod; t = root; if(root == NULL) root = tmp; else { while(t != NULL) { if(t->data == d) { t->cnt++; break; } else if(t->data < d ) { if(t->r== NULL) { t->r= tmp; break; } t = t->r; } else if(t->data > d) { if(t->l== NULL) { t->l= tmp; break; } t = t->l; } } } return root; } void SearchNode(nod *root, int d) //perform searching { nod *tmp = new nod; tmp = root; while(tmp != NULL) { if(tmp->data == d) { cout<<"\nData item "<<d<<" is present "<<tmp->cnt<<" number of times."; return; } else if(tmp->data > d) tmp = tmp->l; else tmp = tmp->r; } cout<<"\n Data not found"; return; } int main() { char c; int n, i, a[20] = {8,1,3,6,4,7,10,14,13,7,6,1,26,4,26,20,21,12,10,1}; //take the elements of the array nod *root = new nod; root = NULL; for(i = 0; i < 20; i++) root = Insert(root, a[i]); up: cout<<"\nEnter the Element to be searched: "; cin>>n; SearchNode(root, n); cout<<"\n\nWant to search more...enter choice(y/n)?"; cin>>c; if(c == 'Y' || c == 'y') goto up; return 0; }
Output
Enter the Element to be searched: 7 Data item 7 is present 2 number of times. Want to search more...enter choice(y/n)?y Enter the Element to be searched: 6 Data item 6 is present 2 number of times. Want to search more...enter choice(y/n)?y Enter the Element to be searched: 4 Data item 4 is present 2 number of times. Want to search more...enter choice(y/n)?y Enter the Element to be searched: 15 Data not found Want to search more...enter choice(y/n)?n
- Related Articles
- C++ Program to find the median of two sorted arrays using binary search approach
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ Program to Find the peak element of an array using Binary Search approach
- C++ Program to Find the maximum subarray sum using Binary Search approach
- Find square root of number upto given precision using binary search in C++
- Java program to find the factorial of a given number using recursion
- Swift program to find the reverse of a given number using recursion
- Program to find number of special positions in a binary matrix using Python
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- MySQL Aggregate Function to find the number of occurrences?
- Write a Golang program to find the factorial of a given number (Using Recursion)
- Program to find longest distance of 1s in binary form of a number using Python
- How to find the number of digits in a given number using Python?
- Golang Program to find the parity of a given number.
- C++ program to Convert a Decimal Number to Binary Number using Stacks

Advertisements