Skip to content

Commit

Permalink
Added dsa c++ codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryansood2512 authored Oct 30, 2022
1 parent a245b70 commit ca29332
Show file tree
Hide file tree
Showing 87 changed files with 7,838 additions and 0 deletions.
177 changes: 177 additions & 0 deletions BST/BST class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
BST Class
Implement the BST class which includes following functions -
1. search
Given an element, find if that is present in BST or not. Return true or false.
2. insert -
Given an element, insert that element in the BST at the correct position. If element is equal to the data of the node, insert it in the left subtree.
3. delete -
Given an element, remove that element from the BST. If the element which is to be deleted has both children, replace that with the minimum element
from right sub-tree.
4. printTree (recursive) -
Print the BST in ithe following format -
For printing a node with data N, you need to follow the exact format -
N:L:x,R:y
where, N is data of any node present in the binary tree. x and y are the values of left and right child of node N. Print the children only if it is not null.
There is no space in between.
You need to print all nodes in the recursive format in different lines.
*/

/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/

class BST {
// Define the data members
BinaryTreeNode<int> *root;

public:
BST() {
// Implement the Constructor
root = NULL;
}

~BST() {
delete root;
}

/*----------------- Public Functions of BST -----------------*/
// Remove function
private:
BinaryTreeNode<int>* remove(int data, BinaryTreeNode<int>* node) {
if (node == NULL) {
return NULL;
}

if (data > node -> data) {
node -> right = remove(data, node -> right);
return node;
} else if (data < node -> data) {
node -> left = remove(data, node -> left);
return node;
} else {
if (node -> left == NULL and node -> right == NULL) {
delete node;
return NULL;
} else if (node -> left == NULL) {
BinaryTreeNode<int>* temp = node -> right;
node -> right = NULL;
delete node;
return temp;
} else if (node -> right == NULL) {
BinaryTreeNode<int>* temp = node -> left;
node -> left = NULL;
delete node;
return temp;
} else {
BinaryTreeNode<int>* minNode = node -> right;
while (minNode -> left) {
minNode = minNode -> left;
}
int rightMin = minNode -> data;
node -> data = rightMin;
node -> right = remove(rightMin, node -> right);
return node;
}
}
}

public:
void remove(int data) {
// Implement the remove() function
root = remove(data, root);
}

// Print Function
private:
void print(BinaryTreeNode<int> *root) {
// Implement the print() function
if (root == NULL) {
return;
}

cout << root -> data << ":";

if (root -> left) {
cout << "L:" << root -> left -> data <<",";
}

if (root -> right) {
cout << "R:" << root -> right -> data;
}
cout << endl;
print(root -> left);
print(root -> right);
}

public:
void print() {
return print(root);
}

// Insert Function
private:
BinaryTreeNode<int>* insert(int data, BinaryTreeNode<int>* node) {
if (node == NULL) {
BinaryTreeNode<int>* newNode = new BinaryTreeNode<int>(data);
return newNode;
}

if (data <= node -> data) {
node -> left = insert(data, node -> left);
} else {
node -> right = insert(data, node -> right);
}
return node;
}

public:
void insert(int data) {
this -> root = insert(data, this -> root);
}

// Search Function
private:
bool search(int data, BinaryTreeNode<int> *node) {
if (node == NULL) {
return false;
}

if (node -> data == data) {
return true;
} else if (data < node -> data) {
return search(data, node -> left);
} else {
return search(data, node -> right);
}

}

public:
bool search(int data) {
// Implement the search() function
return search(data, root);
}
};
87 changes: 87 additions & 0 deletions BST/BST to sorted linked list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
BST to Sorted LL
Given a BST, convert it into a sorted linked list. You have to return the head of LL.
Input format:
The first and only line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space.
If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node.
Output Format:
The first and only line of output prints the elements of sorted linked list.
Constraints:
Time Limit: 1 second
Sample Input 1:
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
Sample Output 1:
2 5 6 7 8 10
*/

/**********************************************************
Following are the Binary Tree Node class structure and the
Node class structure
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
template <typename T>
class Node{
public:
T data;
Node<T> *next;
Node(T data) {
this->data = data;
this->next = NULL;
}
};
***********************************************************/
#include<utility>

pair<Node<int>*, Node<int>*> helper(BinaryTreeNode<int>* root) {
if(root == NULL) {
return {NULL,NULL};
}

Node<int> *node = new Node<int>(root -> data);

pair<Node<int>*, Node<int>*> leftPart = helper(root -> left);
pair<Node<int>*, Node<int>*> rightPart = helper(root -> right);

if(leftPart.first == NULL) {
leftPart.first = node;
leftPart.second = node;
} else {
leftPart.second -> next = node;
leftPart.second = node;
}

if(rightPart.first == NULL) {
rightPart.first = node;
rightPart.second = node;
} else {
node -> next = rightPart.first;
}

return {leftPart.first,rightPart.second};
}

Node<int>* constructLinkedList(BinaryTreeNode<int>* root) {
//Write your code here
return helper(root).first;
}
69 changes: 69 additions & 0 deletions BST/Check if binary tree is BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Check if a Binary Tree is BST
Given a binary tree with N number of nodes, check if that input tree is BST (Binary Search Tree). If yes, return true, return false otherwise.
Note: Duplicate elements should be kept in the right subtree.
Input format :
The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space.
If any node does not have a left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist,
therefore, it will not be a part of the data of any node.
Output format :
The first and only line of output contains either true or false.
Constraints :
Time Limit: 1 second
Sample Input 1 :
3 1 5 -1 2 -1 -1 -1 -1
Sample Output 1 :
true
Sample Input 2 :
5 2 10 0 1 -1 15 -1 -1 -1 -1 -1 -1
Sample Output 2 :
false
*/

/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
#include<climits>
bool helper(BinaryTreeNode<int> *root, int min, int max){
//corner case
if(root == NULL){
return true;
}

if(root -> data < min or root -> data > max){
return false;
}

bool left = helper(root -> left, min, root -> data - 1); // left subtree
bool right = helper(root -> right, root -> data, max); // right subtree

return left and right;
}

bool isBST(BinaryTreeNode<int> *root){
return helper(root, INT_MIN, INT_MAX);
}
63 changes: 63 additions & 0 deletions BST/Construct BST from sorted array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Construct BST from a Sorted Array
Given a sorted integer array A of size n, which contains all unique elements. You need to construct a balanced BST from this input array.
Return the root of constructed BST.
Note: If array size is even, take first mid as root.
Input format:
The first line of input contains an integer, which denotes the value of n. The following line contains n space separated integers,
that denote the values of array.
Output Format:
The first and only line of output contains values of BST nodes, printed in pre order traversal.
Constraints:
Time Limit: 1 second
Sample Input 1:
7
1 2 3 4 5 6 7
Sample Output 1:
4 2 1 3 6 5 7
*/

/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
BinaryTreeNode<int>* helper(int *input, int startIndex, int endIndex) {
//base case
if(startIndex > endIndex) {
return NULL;
}

int mid = startIndex + (endIndex - startIndex) / 2;

BinaryTreeNode<int> *root = new BinaryTreeNode<int>(input[mid]);
root -> left = helper(input, startIndex, mid - 1);
root -> right = helper(input,mid + 1, endIndex);

return root;
}

BinaryTreeNode<int>* constructTree(int *input, int n) {
// Write your code here
return helper(input, 0, n - 1);
}
Loading

0 comments on commit ca29332

Please sign in to comment.