Skip to content

Commit

Permalink
Merge pull request keshavsingh4522#697 from HarshitGupta150/master
Browse files Browse the repository at this point in the history
Create are-generic-trees-similar CPP Code
  • Loading branch information
keshavsingh4522 authored Oct 2, 2021
2 parents ed0ddfd + 02ed090 commit 64d4148
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
149 changes: 149 additions & 0 deletions CPP/Data Structures/Trees/Generic Trees/are-generic-trees-similar
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

struct Node
{
int data;
vector<Node *> children;
};

Node *construct(int arr[], int n)
{
Node *root = NULL;
stack<Node *> st;
for (int i = 0; i < n; i++)
{
if (arr[i] == -1)
{
if (st.size() != 0)
st.pop();
}
else
{
Node *t = new Node();
t->data = arr[i];

if (st.size() > 0)
{
Node *top = st.top();
top->children.push_back(t);
}

else
{
root = t;
}
st.push(t);
}
}
return root;
};

int size(Node *node)
{
int s = 0;

for (int i = 0; i < node->children.size(); i++)
{
s += size(node->children[i]);
}
s += 1;

return s;
}

vector <int> galvin(Node *root, int b){
if(root->data == b){
vector <int> path;
path.push_back(root->data);
return path;
}

for(Node *child: root->children){
vector<int> ptc = galvin(child, b);
if(ptc.size() > 0){
ptc.push_back(root->data);
return ptc;
}
}
return vector<int>();


}

int distanceBetweenNodes(Node *root, int d1, int d2){
vector<int> p1=galvin(root, d1);
vector<int> p2=galvin(root, d2);

int i = p1.size() - 1;
int j = p2.size() - 1;

while(i >= 0 && j >= 0 && p1[i] == p2[j]){
i--;
j--;
}


return i+1 + j+1;
}


bool areSimilar(Node *root,Node *node)
{
if(root->children.size()!=node->children.size())
return false;

for (int i = 0; i < node->children.size() ; i++) {
Node *c1 = root->children[i];
Node *c2 = node->children[i];
if (areSimilar(c1, c2) == false) {
return false;
}
}

return true;
}


void display(Node *root)
{
if(root == NULL)return;
string str = to_string(root->data) + "->";
for (Node* child : root->children)
{
str +=to_string(child->data) + ",";
}
str += ".";
//cout << (str) << endl;

// for (Node * child : root->children)
// {
// display(child);
// }
}

int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];

int m;cin>>m;
int brr[m];
for (int i = 0; i < m; i++)
cin >> brr[i];

Node *root = construct(arr, n);
Node *node = construct(brr,m);
bool a = areSimilar(root,node);
if(a==1)
cout<<"true"<<endl;

else
cout<<"false"<<endl;
}
56 changes: 56 additions & 0 deletions DP/Goldmine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;cin>>n>>m;
int arr[n][m];


for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>arr[i][j];
}
}

int **dp;
dp=new int*[n];
for(int i=0;i<n;i++)
dp[i]=new int[m];
for(int j=m-1;j>=0;j--)
{
for(int i=n-1;i>=0;i--)
{
if(j==m-1)
{
dp[i][j]=arr[i][j];
}
else if(i==n-1)
{
dp[i][j]=arr[i][j]+max(dp[i][j+1],dp[i-1][j+1]);
}
else if(i==0)
{
dp[i][j]=arr[i][j]+max(dp[i][j+1],dp[i+1][j+1]);

}
else
{

dp[i][j]=arr[i][j]+max(dp[i][j+1],max(dp[i+1][j+1],dp[i-1][j+1]));

}
}
}

int b=dp[0][0];
for(int i=1;i<n;i++)
{

b =max(b,dp[i][0]);

}

cout<<b<<endl;
}

0 comments on commit 64d4148

Please sign in to comment.