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
49 changes: 49 additions & 0 deletions Binary Tree/BT_from_postorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;

struct Node{
int data;
struct Node* left;
struct Node* right;
Node(int n){
data=n;
left=NULL;
right=NULL;
}
};

int search(int in[],int curr,int start,int end){
for(int i=start;i<=end;i++){
if(in[i]==curr)
return i;
}
return -1;
}
Node* create_tree(int post[],int in[],int start,int end){
static int idx=end;
if(start>end)
return NULL;
int cur=post[idx];
idx--;
Node* node=new Node(cur);
if(start==end)
return node;
int pos=search(in,cur,start,end);
node->right=create_tree(post,in,pos+1,end);
node->left=create_tree(post,in,start,pos-1);
return node;
}
void display(Node* node){
if(node==NULL)
return;
display(node->left);
cout<<node->data<<" ";
display(node->right);
}

int main(){
int post[]={4,2,5,3,1};
int in[]={4,2,1,5,3};
Node* root=create_tree(post,in,0,4);
display(root);
}
55 changes: 55 additions & 0 deletions Binary Tree/BT_from_preorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

int search_pos(int in[],int el,int l,int h){

for(int i=l;i<=h;i++){
if(in[i]==el)
return i;
}
return -1;
}

node* create_tree(int in[],int pre[],int l,int h){
static int idx=0;
if(l>h){
return NULL;
}
int curr=pre[idx];
idx++;
node* root=new node(curr);
if(l==h){
return root;
}
int pos=search_pos(in,curr,l,h);
root->left=create_tree(in,pre,l,pos-1);
root->right=create_tree(in,pre,pos+1,h);
return root;
}

void inorder_traversal(node* root){
if(root==NULL){
return;
}
inorder_traversal(root->left);
cout<<root->data<<" ";
inorder_traversal(root->right);
}

int main(){
int in[]={4,2,1,5,3};
int pre[]={1,2,4,3,5};
node* root=create_tree(in,pre,0,4);
inorder_traversal(root);
}
24 changes: 24 additions & 0 deletions Binary Tree/Binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};


int main(){
node *root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
}
38 changes: 38 additions & 0 deletions Binary Tree/Diameter_of_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

int diameter_of_bt(node* root,int* h){
if(root==NULL){
*h=0;
return 0;
}
int lh=0,rh=0;
int leftdiameter=diameter_of_bt(root->left,&lh);
int rightdiameter=diameter_of_bt(root->right,&rh);
int currdiamter=lh+rh+1;
*h=max(lh,rh)+1;
return max(currdiamter,max(rightdiameter,leftdiameter));
}

int main(){
node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
int h=0;
cout<<diameter_of_bt(root,&h);
}
68 changes: 68 additions & 0 deletions Binary Tree/Height_balanced_BT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

int height(node* root){
if(root==NULL)
return 0;
return max(height(root->left),height(root->right)) + 1;
}

bool height_balance(node* root){
if(root==NULL)
return true;
if(height_balance(root->left)==false)
return false;
if(height_balance(root->right)==false)
return false;
int lh=height(root->left);
int rh=height(root->right);
if(abs(lh-rh)<=1)
return true;
else
return false;
}

bool height_balance_optimised(node* root,int *h){
if(root==NULL){
*h=0;
return true;
}
int lh=0,rh=0;
if(height_balance_optimised(root->left,&lh)==false)
return false;
if(height_balance_optimised(root->right,&rh)==false)
return false;
*h=max(lh,rh)+1;
if(abs(lh-rh)<=1)
return true;
else
return false;
}

int main(){
node* root2=new node(1);
root2->left=new node(2);
root2->left->left=new node(3);

node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
cout<<height_balance(root)<<endl;
int h=0;
cout<<height_balance_optimised(root2,&h);
}
34 changes: 34 additions & 0 deletions Binary Tree/Inorder_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

void inorder_traversal(node* root){
if(root==NULL){
return;
}
inorder_traversal(root->left);
cout<<root->data<<" ";
inorder_traversal(root->right);
}


int main(){
node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
inorder_traversal(root);
}
41 changes: 41 additions & 0 deletions Binary Tree/Left_view_BT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

void left_view(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
int s=q.size();
for(int i=0;i<s;i++){
node* temp=q.front();
q.pop();
if(i==0)
cout<<temp->data<<" ";
if(temp->left!=NULL)
q.push(temp->left);
if(temp->right!=NULL)
q.push(temp->right);
}
}
}
int main(){
node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
left_view(root);
}
76 changes: 76 additions & 0 deletions Binary Tree/Lowest_common_ancestor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
struct node* left;
struct node* right;
node(int n){
data=n;
left=NULL;
right=NULL;
}
};

node* Lca(node* root,int a,int b){
if(root==NULL)
return NULL;
if(root->data==a||root->data==b)
return root;
node* left=Lca(root->left,a,b);
node* right=Lca(root->right,a,b);
if(left!=NULL && right!=NULL)
return root;
if(left==NULL && right==NULL)
return NULL;
if(left!=NULL)
return Lca(root->left,a,b);
if(right!=NULL)
return Lca(root->right,a,b);
}

bool getpath(node* root,int a,vector<int> &path){
if(root==NULL)
return 0;
path.push_back(root->data);
if(root->data==a)
return 1;
if(getpath(root->left,a,path) || getpath(root->right,a,path))
return 1;
path.pop_back();
return 0;
}
int lowest_common_ancestor(node* root,int a,int b){
vector<int> path1,path2;

if(!getpath(root,a,path1) || !getpath(root,b,path2))
return -1;
int i;
for(i=0;i<path1.size() && path2.size();i++){
if(path1[i]!=path2[i])
break;
}
return path1[i-1];
}



int main(){
node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->right->left=new node(6);
root->right->right=new node(7);
int ans=lowest_common_ancestor(root,6,7);
if(ans==-1)
cout<<"Not exist"<<endl;
else
cout<<ans<<endl;
node* ans2=Lca(root,6,7);
if(root==NULL)
cout<<"not exist";
else
cout<<ans2->data;
}
Loading