forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request keshavsingh4522#697 from HarshitGupta150/master
Create are-generic-trees-similar CPP Code
- Loading branch information
Showing
2 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
CPP/Data Structures/Trees/Generic Trees/are-generic-trees-similar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |