Skip to content

Commit

Permalink
Added lab-07
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatkhanna1999 committed Jan 4, 2019
1 parent 5e32102 commit d8a1e35
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Lab-07/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Lab-07

## Question
Given a binary search tree, consisting of n keys, you have to process q queries.
In each query, given two keys that exist in the tree, print the maximum path sum between two nodes with those keys.
Path sum for two nodes is defined as the sum of the keys of all nodes in the unique path between two nodes.
Note that input and output format have already been taken care of in the code.

Input Format

The first line of input contains one integer n, the number of keys in the binary search tree.
The next line contains n spaced unique integers, which are the keys of the binary search tree.
The next line contains one integer q, the number of queries to be processed.
The next q lines contain two integers, which are keys that exist in the binary search tree.
Constraints

1 ≤ n ≤ 2 * 103
1 ≤ q ≤ 2 * 103
1 ≤ key ≤ 2 * 104
Subtask 1: All keys in the tree are unique

Output Format

Print one line per query, the path sum for the given two nodes, as described above. Do not print anything else.
Sample Input 0

6
4 2 3 1 7 6
1
1 7
Sample Output 0

14
Explanation 0

image

Since the unique path from 1 to 7 is 1 -> 2 -> 4 -> 7, therefore the pathsum becomes 1 + 2 + 4 + 7 = 14
189 changes: 189 additions & 0 deletions Lab-07/lab07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat

int arr[20005]={0};

class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};

class Solution {
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}

return root;
}
}

/*The tree node has data, left child and right child
class Node {
int data;
Node* left;
Node* right;
};
*/

int helper(Node*root , int v,int sum)
{
// cout<<"dffgghhj"<<endl;
// cout<<v<<" "<<"root"<<root->data<<endl;
if(v==root->data)
{
//cout<<" ddff"<<endl;
// cout<<root->data<<endl;
//sum+=root->data;
/*flag=true;
if(root->left!=NULL)
sum=root->data+helper(root->left,v,sum,flag);
else
sum=root->data;
return sum;*/
if(arr[v]>1)
{
arr[v]--;
sum=root->data+helper(root->left,v,sum);
return sum;
}
else if(arr[v]==1)
{
sum=root->data;
return sum;
}
}
else if(v<root->data)
{
// if(root->left!=NULL)
//cout<<"kkk"<<endl;
sum=root->data+helper(root->left,v,sum);
//else
// sum+=root->data;
return sum;
}
else
{
//if(root->right!=NULL)
//cout<<"jjj"<<endl;
sum=root->data+helper(root->right,v,sum);
// else
// sum+=root->data;
return sum;
}
return 0;
}


int pathSum(Node *root, int v1,int v2) {
// Write your code here.
//v1=min(v1,v2);
//v2=max(v1,v2);
if(v1==v2 && v1==root->data)
{
int s1=helper(root,v1,0);
return s1;
}
else if(v1<=root->data && v2>=root->data)
{
int temp=root->data;
//cout<<root->data<<endl;
int s1=helper(root,v1,0);
//cout<<s1<<endl;
//cout<<root->data<<endl;
//cout<<"h1"<<endl;
int s2=helper(root,v2,0);
//cout<<s2<<endl;
//cout<<"h2"<<endl;
int ans=s1+s2-temp;
// cout<<"h3"<<endl;
//cout<<ans<<endl;
return ans;
}

else if( v1<=root->data && v2 <=root->data)
{
int ans=pathSum(root->left,v1,v2);
return ans;
}

else if( v1>=root->data && v2>=root->data)
{
int ans=pathSum(root->right,v1,v2);
return ans;
}
return 0;
}

}; //End of Solution

int main() {
sync;
Solution myTree;
Node* root = NULL;

int t;
int data;

std::cin >> t;
while(t-- > 0) {
std::cin >> data;
arr[data]++;
root = myTree.insert(root, data);
}

int q;
cin >> q;

while( q>0 ){
int v1, v2;
std::cin >> v1 >> v2;
int t1=v1,t2=v2;
v1=min(t1,t2);
v2=max(t1,t2);
int ans = myTree.pathSum(root, v1, v2);
std:cout << ans << endl;
q--;
}

return 0;
}

0 comments on commit d8a1e35

Please sign in to comment.