
- 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 Maximum Element in an Array using Binary Search
This is a C++ Program to find the maximum element of an array using Binary Search Tree. The time complexity of this program is O(log(n)).
Algorithm
Begin Construct the Binary Search Tree using the given data elements. Next traverse the root pointer to the rightmost child node available. Print the data part of the node as the maximum data element of the given data set. Print the Depth of the maximum data. 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; } int main() { int n, i, a[10] = {86, 63, 95, 6, 7, 67, 52, 26, 45, 98}; node *root = new node; root = NULL; cout<<"\nData set:\n"; for(i = 0; i < 10; i++) { cout<<a[i]<<" "; root = InsertIntoTree(root, a[i]); } cout<<"\n\nThe maximum element of the given data set is\n "; i = 0; while(root->right != NULL) { i++; root = root->right; } cout<<root->d<<"\n"<<"data found at "<<i<<" depth from the root."; return 0; }
Output
Data set: 86 63 95 6 7 67 52 26 45 98 The maximum element of the given data set is 98 data found at 2 depth from the root.
- Related Articles
- 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
- How to find minimum element in an array using binary search in C language?
- C++ Program to Find Minimum Element in an Array using Linear Search
- C program to search an array element using Pointers.
- C++ Program to Find the maximum subarray sum using Binary Search approach
- Search in an array with Binary search using JavaScript
- C++ Program to Search for an Element in a Binary Search Tree
- PHP program to find the maximum element in an array
- Swift Program to Search an Element in an Array
- Java Program to implement Binary Search on an array
- Write a Golang program to search an element in an array
- Java Program to Recursively Linearly Search an Element in an Array
- C# program to find maximum and minimum element in an array\n
- Program to find maximum XOR with an element from array in Python

Advertisements