-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Tree data Structure #21
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extra line |
||
|
||
// Tree Node | ||
struct Node { | ||
int data; | ||
Node* left; | ||
Node* right; | ||
}; | ||
|
||
// Utility function to create a new Tree Node | ||
Node* newNode(int val) { | ||
Node* temp = new Node; | ||
temp->data = val; | ||
temp->left = NULL; | ||
temp->right = NULL; | ||
|
||
return temp; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extra line |
||
// Function to Build Tree | ||
Node* buildTree(string str) { | ||
// Corner Case | ||
if (str.length() == 0 || str[0] == 'N') return NULL; | ||
|
||
// Creating vector of strings from input | ||
// string after spliting by space | ||
vector<string> ip; | ||
|
||
istringstream iss(str); | ||
for (string str; iss >> str;) ip.push_back(str); | ||
|
||
// Create the root of the tree | ||
Node* root = newNode(stoi(ip[0])); | ||
|
||
// Push the root to the queue | ||
queue<Node*> queue; | ||
queue.push(root); | ||
|
||
// Starting from the second element | ||
int i = 1; | ||
while (!queue.empty() && i < ip.size()) { | ||
|
||
// Get and remove the front of the queue | ||
Node* currNode = queue.front(); | ||
queue.pop(); | ||
|
||
// Get the current node's value from the string | ||
string currVal = ip[i]; | ||
|
||
// If the left child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the left child for the current node | ||
currNode->left = newNode(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->left); | ||
} | ||
|
||
// For the right child | ||
i++; | ||
if (i >= ip.size()) break; | ||
currVal = ip[i]; | ||
|
||
// If the right child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the right child for the current node | ||
currNode->right = newNode(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->right); | ||
} | ||
i++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
|
||
// } Driver Code Ends | ||
/* A binary tree node structure | ||
struct Node | ||
{ | ||
int data; | ||
struct Node* left; | ||
struct Node* right; | ||
|
||
Node(int x){ | ||
data = x; | ||
left = right = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
class Solution{ | ||
public: | ||
//Function to check whether a binary tree is balanced or not. | ||
pair<bool, int> isBalancedFast(Node* root) { | ||
// base case | ||
if(root == NULL) | ||
{ | ||
pair<bool, int> p = make_pair(true, 0); | ||
return p; | ||
} | ||
|
||
pair<int,int> left = isBalancedFast(root->left); | ||
pair<int,int> right = isBalancedFast(root->right); | ||
|
||
|
||
bool leftAns = left.first; | ||
bool rightAns = right.first; | ||
|
||
bool diff = abs (left.second - right.second ) <=1; | ||
|
||
pair<bool,int> ans; | ||
ans.second = max(left.second, right.second) + 1; | ||
|
||
if(leftAns && rightAns && diff) { | ||
ans.first = true; | ||
} | ||
else | ||
{ | ||
ans.first = false; | ||
} | ||
return ans; | ||
} | ||
bool isBalanced(Node *root) | ||
{ | ||
return isBalancedFast(root).first; | ||
} | ||
|
||
}; | ||
|
||
|
||
// { Driver Code Starts. | ||
|
||
/* Driver program to test size function*/ | ||
|
||
|
||
|
||
int main() { | ||
|
||
|
||
int t; | ||
scanf("%d ", &t); | ||
while (t--) { | ||
string s, ch; | ||
getline(cin, s); | ||
|
||
Node* root = buildTree(s); | ||
Solution ob; | ||
cout << ob.isBalanced(root) << endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#include <bits/stdc++.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
struct Node *left; | ||
struct Node *right; | ||
|
||
Node(int x){ | ||
data = x; | ||
left = NULL; | ||
right = NULL; | ||
} | ||
}; | ||
|
||
|
||
// } Driver Code Ends | ||
/* A binary tree node | ||
struct Node | ||
{ | ||
int data; | ||
struct Node* left; | ||
struct Node* right; | ||
|
||
Node(int x){ | ||
data = x; | ||
left = right = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
public: | ||
//Function to check if two trees are identical. | ||
bool isIdentical(Node *r1, Node *r2) | ||
{ | ||
// base case | ||
if(r1 == NULL && r2 == NULL) { | ||
return true; | ||
} | ||
|
||
if(r1 == NULL && r2 != NULL) { | ||
return false; | ||
} | ||
|
||
if(r1 != NULL && r2 == NULL) { | ||
return false; | ||
} | ||
|
||
bool left = isIdentical(r1->left, r2->left); | ||
bool right = isIdentical(r1->right, r2->right); | ||
|
||
bool value = r1->data == r2->data; | ||
|
||
|
||
if(left && right && value) { | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
} | ||
|
||
}; | ||
|
||
// { Driver Code Starts. | ||
|
||
// Function to Build Tree | ||
Node *buildTree(string str) { | ||
// Corner Case | ||
if (str.length() == 0 || str[0] == 'N') | ||
return NULL; | ||
|
||
// Creating vector of strings from input | ||
// string after spliting by space | ||
vector<string> ip; | ||
|
||
istringstream iss(str); | ||
for (string str; iss >> str;) | ||
ip.push_back(str); | ||
|
||
// Create the root of the tree | ||
Node *root = new Node(stoi(ip[0])); | ||
|
||
// Push the root to the queue | ||
queue<Node *> queue; | ||
queue.push(root); | ||
|
||
// Starting from the second element | ||
int i = 1; | ||
while (!queue.empty() && i < ip.size()) { | ||
|
||
// Get and remove the front of the queue | ||
Node *currNode = queue.front(); | ||
queue.pop(); | ||
|
||
// Get the current node's value from the string | ||
string currVal = ip[i]; | ||
|
||
// If the left child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the left child for the current node | ||
currNode->left = new Node(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->left); | ||
} | ||
|
||
// For the right child | ||
i++; | ||
if (i >= ip.size()) | ||
break; | ||
currVal = ip[i]; | ||
|
||
// If the right child is not null | ||
if (currVal != "N") { | ||
|
||
// Create the right child for the current node | ||
currNode->right = new Node(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->right); | ||
} | ||
i++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
int main() { | ||
int tc; | ||
scanf("%d ", &tc); | ||
while (tc--) { | ||
string str, str1; | ||
getline(cin, str); | ||
Node *rootA = buildTree(str); | ||
getline(cin, str1); | ||
Node *rootB = buildTree(str1); | ||
Solution ob; | ||
if (ob.isIdentical(rootA, rootB)) { | ||
cout << "Yes\n"; | ||
} else { | ||
cout << "No\n"; | ||
} | ||
} | ||
return 0; | ||
} // } Driver Code Ends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Bhavyaatrivedi
At the start of every program, we need to write a comment about the program and what problem it is solving. It really helps to know about the program.