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
61 changes: 61 additions & 0 deletions BINARY TREE Diameter Of a Tree Optimized Approach
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include<queue>
using namespace std;
class node{
public:
int data;
node*left;
node*right;

node(int d){
data = d;
left = NULL;
right = NULL;
}
};
node* buildTree(){
int d;
cin>>d;

if(d==-1){
return NULL;
}
node * root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
class Pair{
public:
int height;
int diameter;
};

Pair fastDiameter(node*root){
Pair p;
if(root==NULL){
p.diameter = p.height = 0;
return p;
}
//Otherwise
Pair left = fastDiameter(root->left);
Pair right = fastDiameter(root->right);

p.height = max(left.height,right.height) + 1;
p.diameter = max(left.height+right.height, max(left.diameter,right.diameter));
return p;
}

int main(){
node* root = buildTree();
Pair p = fastDiameter(root);
cout<<p.height<<endl;
cout<<p.diameter<<endl;
return 0;
}
/* i/p:
8 10 7 -1 -1 6 9 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1
i/p:
4
6
*/
117 changes: 117 additions & 0 deletions Binary Search Tree/Add_node_in_BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <iostream>
#include<queue>
using namespace std;

class node{
public:
int data;
node*left;
node*right;

node(int d){
data = d;
left = NULL;
right = NULL;
}
};

//Accepts the old root node & data and returns the new root node
node* insertinBST(node *root,int data){
//Base Case
if(root==NULL){
return new node(data);
}
//Rec Case - Insert in the Subtree and Update Pointers
if(data<=root->data){
root->left = insertinBST(root->left,data);
}
else{
root->right = insertinBST(root->right,data);
}
return root;
}

bool search(node*root,int data){
if(root==NULL){
return false;
}
if(root->data==data){
return true;
}
//Recursively search in left and right subtree
if(data<=root->data){
return search(root->left,data);
}
else{
return search(root->right,data);
}
}

node* build(){
//Read a list of numbers till -1 and also these numbers will be inserted into BST
int d;
cin>>d;

node*root = NULL;

while(d!=-1){
root = insertinBST(root,d);
cin>>d;
}
return root;
}
//Print the BST Level By Level
void bfs(node *root){
queue<node*> q;
q.push(root);
q.push(NULL);

while(!q.empty()){
node* f = q.front();
if(f==NULL){
cout<<endl;
q.pop();
if(!q.empty()){
q.push(NULL);
}
}
else{
cout<<f->data<<",";
q.pop();

if(f->left){
q.push(f->left);
}
if(f->right){
q.push(f->right);
}
}
}
return;
}
node* add(node* root,int data)
{
if(root==NULL)
{
return new node(data);
}
if(data<=root->data)
{
root->left=add(root->left,data);
}
if(data>=root->data)
{
root->right=add(root->right,data);
}
return root;
}
int main()
{
node*root=build();
bfs(root);
int data;
cin>>data;
add(root,data);
cout<<endl;
bfs(root);
}
129 changes: 129 additions & 0 deletions Binary Search Tree/BST Check For BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include<iostream>
#include<queue>
#include<climits>
using namespace std;
class node
{
public:
int data;
node*left;
node*right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
bool isBST(node* root ,int minV=INT_MIN, int maxV=INT_MAX)
{
if(root==NULL)
{
return true;
}
if(root->data>=minV and root->data<=maxV and isBST(root->left,minV,root->data) and isBST(root->right,root->data,maxV))
{
return true;
}
else
{
return false;
}
}
node* insertinBST(node*root,int data) //Accepts the old root node & data & returns the new node
{
if(root==NULL) //base case
{
return new node(data);
}
if(data<=root->data)
{
root->left=insertinBST(root->left,data);
}
else
{
root->right=insertinBST(root->right,data);
}
return root;
}
node*build() //Read a list of numbers till -1 and also these numbers will be inserted in the BST
{
int d;
cin>>d;
node*root=NULL;
while(d!=-1) // as -1 encountered insertion stops
{
root=insertinBST(root,d);
cin>>d;
}
return root;
}
void bfs(node*root) //to print the tree
{
queue<node*>q;
q.push(root);
q.push(NULL);
while(!q.empty())
{
node*f=q.front();
if(f==NULL)
{
cout<<endl;
q.pop();
if(!q.empty())
{
q.push(NULL);
}
}
else
{
cout<<f->data<<",";
q.pop();
if(f->left)
{
q.push(f->left);
}
if(f->right)
{
q.push(f->right);
}
}
}
return ;
}
void inorder(node*root) //printing left, root then right
{
if(root==NULL)
{
return;
}
inorder(root->left);
cout<<root->data<<",";
inorder(root->right);
}
int main()
{
node*root=build();
inorder(root);
cout<<endl;
bfs(root);
cout<<endl;
if(isBST(root))
{
cout<<"yes it's a BST";
}
else
{
cout<<"Not a BST!!";
}
return 0;
}
/*
i/p:
5 3 7 1 6 8 -1
o/p:
1,3,5,6,7,8,
5,
3,7,
1,6,8,
*/
Loading