Skip to content

Commit

Permalink
bst
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan2309 committed Jul 8, 2021
1 parent 6dfc234 commit 6e6fb56
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
Binary file modified BST/a.exe
Binary file not shown.
67 changes: 67 additions & 0 deletions BST/klarge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
k-th largest element in BST
Given a Binary search tree. Your task is to complete the function which will return the Kth largest element without doing any modification in Binary Search Tree.
*/
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left; //pointer pointing towards left child
struct Node *right; //pointer for right child

Node(int val) //constructor taking data
{
data = val;
left = NULL; //initially
right = NULL;
}
};

int ans; //global variable

void solve(Node *root, int k, int &idx)
{
if (!root)
{ //base cond.
return;
}
solve(root->right, k, idx); //right rec for rev inorder traversal
if (k == idx) //if index matches k
{
ans = root->data;
idx++;
return;
}
else
{
idx++;
}
solve(root->left, k, idx); //left rec for rev inorder traversal
}

int KthSmallestElement(Node *root, int K)
{
//add code here.
int idx = 1; //counter
ans = -1; //initially
solve(root, K, idx);
return ans;
}

int main()
{

struct Node *root = new Node(4); //node pointer called root -> it will point towards a new node 1
root->left = new Node(2); //root left will point towards a new node 2
root->right = new Node(6);
root->left->left = new Node(1);
root->left->right = new Node(3);
root->right->left = new Node(5);
root->right->right = new Node(7);
int k = 3; //to find kth smallest element
cout << KthSmallestElement(root, k) << endl;
return 0;
}
67 changes: 67 additions & 0 deletions BST/ksmall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
k-th smallest element in BST
Given a BST and an integer K. Find the Kth Smallest element in the BST.
*/
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left; //pointer pointing towards left child
struct Node *right; //pointer for right child

Node(int val) //constructor taking data
{
data = val;
left = NULL; //initially
right = NULL;
}
};

int ans; //global variable

void solve(Node *root, int k, int &idx)
{
if (!root)
{ //base cond.
return;
}
solve(root->left, k, idx); //left rec for inorder traversal
if (k == idx) //if index matches k
{
ans = root->data;
idx++;
return;
}
else
{
idx++;
}
solve(root->right, k, idx); //right rec for inorder traversal
}

int KthSmallestElement(Node *root, int K)
{
//add code here.
int idx = 1; //counter
ans = -1; //initially
solve(root, K, idx);
return ans;
}

int main()
{

struct Node *root = new Node(4); //node pointer called root -> it will point towards a new node 1
root->left = new Node(2); //root left will point towards a new node 2
root->right = new Node(6);
root->left->left = new Node(1);
root->left->right = new Node(3);
root->right->left = new Node(5);
root->right->right = new Node(7);
int k = 3; //to find kth smallest element
cout << KthSmallestElement(root, k) << endl;
return 0;
}
87 changes: 87 additions & 0 deletions BST/merge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Merge two BST
*/
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left; //pointer pointing towards left child
struct Node *right; //pointer for right child

Node(int val) //constructor taking data
{
data = val;
left = NULL; //initially
right = NULL;
}
};

vector<Node *> inOrder; //vector storing inorder traversal of tree 1
int start = 0; //start index
int n = inOrder.size(); //end index

vector<Node *> inOrder1; //vector storing inorder traversal of tree 2
int start1 = 0; //start index
int n1 = inOrder.size(); //end index

vector<Node *> merged;
void inorder(struct Node *root) //func for taking out inorder traversal of tree 1
{
if (root == NULL)
{
return;
}
inorder(root->left);
inOrder.push_back(root);
inorder(root->right);
}
void inorder1(struct Node *root) //func for taking out inorder traversal of tree 2
{
if (root == NULL)
{
return;
}
inorder1(root->left);
inOrder1.push_back(root);
inorder1(root->right);
}

Node *solve(int start, int end) //func for making tree balanced
{
if (start > end)
{ //base case
return NULL;
}
int mid = (start + end) / 2; //middle element find out from vector storing inorder trav
Node *root = merged[mid]; //middle element is root node
root->left = solve(start, mid - 1); //left rec
root->right = solve(mid + 1, end); //right rec
return root;
}

int main()
{
//tree1
struct Node *root = new Node(3); //node pointer called root -> it will point towards a new node 1
root->left = new Node(2); //root left will point towards a new node 2
root->right = new Node(4);
root->left->left = new Node(1);
//tree2
struct Node *root1 = new Node(6); //node pointer called root -> it will point towards a new node 1
root1->left = new Node(5); //root left will point towards a new node 2
root1->right = new Node(7);
inorder(root); //inorder of tree1
inorder1(root1); //inorder of tree 2

//merge two sorted vectors

merge(inOrder.begin(), inOrder.end(), inOrder1.begin(),
inOrder1.end(), merged.begin());
int st = 0;
int en = merged.size();
//making balanced tree from vect
solve(start, en);
return 0;
}

0 comments on commit 6e6fb56

Please sign in to comment.