Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Advanced/C++/BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//SakhinetiPraveena
//Given array 1 2 3 4 5, code for constructing a balanced binary search tree .
#include<bits/stdc++.h>
using namespace std;

struct Node{
int data;
Node *left,*right;
Node(int val){
data=val;
left=NULL;
right=NULL;
}
};
// As the given array is sorted, to construct balanced BST the mid element is taken as root

Node* ConstructBST(int arr[],int start, int end_){
if(start>end_) return NULL;
int mid=(start+end_)/2;
Node * root=new Node(arr[mid]);
root->left=ConstructBST(arr,start,mid-1);
root->right=ConstructBST(arr,mid+1,end_);
return root;
}

// function to print the inorder of BST
void print_inorder(Node* root){
if(root==NULL) return;
if (root != NULL){
print_inorder(root->left);
cout << root->data <<" ";
print_inorder(root->right);
}
}

int main(){
int arr[5]={1,2,3,4,5};
Node* root=ConstructBST(arr,0,4);
cout<<"Inorder for the BST is :";
print_inorder(root);
}
28 changes: 28 additions & 0 deletions Intermediate/C++/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//SakhinetiPraveena
//Binary Search for 3 in 2 4 8 9 1 3 6

#include <bits/stdc++.h>
using namespace std;

void binarySearch(int arr[], int key,int n){
int s=0,e=n-1;
while(s<=e){
int mid=(s+e)/2;
if(arr[mid]==key){
cout<<key<<" is found in the array.";
return;
}
else if(arr[mid]<key){
s=mid+1;
}
else e=mid-1;
}
cout<<key<<" is not found in the array.";
}
int main(){
int n=7;
int arr[7]={2, 4, 8, 9, 1, 3, 6};
sort(arr, arr + n);
binarySearch(arr,3,7);
return 0;
}