Skip to content
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

Add binary search tree and algorithms #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions binary_search_tree/build_bst.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node *left,*right;
//Constructor:
node(int data)
{
this->data=data;
left=right=NULL;
}
};
//Build Binary Search Tree:
node *buildBinarySearchTree(node *root , int data)
{
if(root==NULL)
{
node *temp=new node(data);
return temp;
}
if(data < root->data)
{
root->left=buildBinarySearchTree(root->left , data);
}
else if(data > root->data)
{
root->right=buildBinarySearchTree(root->right , data);
}
return root;
}
//Inorder Traversal:
void inOrderTraversal(node * root)
{
if(root==NULL)
{
return;
}
inOrderTraversal(root->left);
cout<<root->data<<" ";
inOrderTraversal(root->right);
}
//Main Function:
int main(int argc, char const *argv[])
{
node *root = buildBinarySearchTree(root , 8);
root = buildBinarySearchTree(root , 6);
root = buildBinarySearchTree(root , 10);
root = buildBinarySearchTree(root , 5);
root = buildBinarySearchTree(root , 7);
root = buildBinarySearchTree(root , 9);
root = buildBinarySearchTree(root , 11);

inOrderTraversal(root);
cout<<endl;
return 0;
}
114 changes: 114 additions & 0 deletions binary_search_tree/deletion_in_bst.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include<bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node *left,*right;
//Constructor:
node(int data)
{
this->data=data;
left=right=NULL;
}
};
//Find the inorder successor:
int inorderSuccessor(node *root)
{
node *temp=root;
while(temp->left!=NULL)
{
temp=temp->left;
}
return temp->data;
}
//Searching in BST:
node *deletion(node *root, int data)
{
if(root==NULL)
{
cout<<"Element has not been found "<<endl;
return NULL;
}
if(root->data==data)
{

if((root->left != NULL) and (root->right != NULL))
{
int successor=inorderSuccessor(root->right);
root->data= successor;
root->right = deletion(root->right , successor);
}
else if(root->left !=NULL)
{
root=root->left;
}
else if(root->right !=NULL)
{
root=root->right;
}
else{
root=NULL;
}
return root;
}
else if(root->data > data)
{
root->left = deletion(root->left , data);
}
else if(root->data < data)
{
root->right = deletion(root->right , data);
}
return root;
}
//Build Binary Search Tree:
node *buildBinarySearchTree(node *root , int data)
{
if(root==NULL)
{
node *temp=new node(data);
return temp;
}
if(data < root->data)
{
root->left=buildBinarySearchTree(root->left , data);
}
else if(data > root->data)
{
root->right=buildBinarySearchTree(root->right , data);
}
return root;
}
//Inorder Traversal:
void inOrderTraversal(node * root)
{
if(root==NULL)
{
return;
}
inOrderTraversal(root->left);
cout<<root->data<<" ";
inOrderTraversal(root->right);
}
//Main Function:
int main(int argc, char const *argv[])
{
node *root = buildBinarySearchTree(root , 8);
root = buildBinarySearchTree(root , 3);
root = buildBinarySearchTree(root , 10);
root = buildBinarySearchTree(root , 1);
root = buildBinarySearchTree(root , 6);
root = buildBinarySearchTree(root , 14);
root = buildBinarySearchTree(root , 4);
root = buildBinarySearchTree(root , 7);
root = buildBinarySearchTree(root , 13);

inOrderTraversal(root);
cout<<endl;
root=deletion(root,6);
inOrderTraversal(root);

return 0;
}
106 changes: 106 additions & 0 deletions binary_search_tree/print_all_path_from_leaf_to_root.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

#include<iostream>
#include<vector>
using namespace std;
// vector<int> vctr;
class Node
{
public:
int data;
Node *left;
Node *right;
//Constructor:
Node(int data)
{
this->data=data;
left=right=NULL;
}
};
//Print:
void printing(vector<int> vctr)
{
for(auto x : vctr)
{
cout<<x<<"-->";
}
cout<<endl;
}
void printAllPath(Node *root , vector<int> &vctr)
{
if(root==NULL)
{
return ;
}
if(root->left ==NULL and root->right==NULL)
{
vctr.push_back(root->data);
printing(vctr);
vctr.pop_back();
return;
}
vctr.push_back(root->data);
printAllPath(root->left , vctr);
printAllPath(root->right , vctr);
vctr.pop_back();
return;
}
//Build Binary Search Tree:
Node *buildBinarySearchTree(Node *root , int data)
{
if(root==NULL)
{
Node *temp=new Node(data);
return temp;
}
if(data < root->data)
{
root->left=buildBinarySearchTree(root->left , data);
}
else if(data > root->data)
{
root->right=buildBinarySearchTree(root->right , data);
}
return root;
}
//Inorder Traversal:
void inOrderTraversal(Node * root)
{
if(root==NULL)
{
return;
}
inOrderTraversal(root->left);
cout<<root->data<<" ";
inOrderTraversal(root->right);
}
//Preorder Traversal:
void preOrderTraversal(Node * root)
{
if(root==NULL)
{
return;
}
cout<<root->data<<" ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
//Main Function:
int main()
{

Node * root = buildBinarySearchTree(root , 8);
root = buildBinarySearchTree(root , 6);
root = buildBinarySearchTree(root , 10);
root = buildBinarySearchTree(root , 5);
root = buildBinarySearchTree(root , 7);
root = buildBinarySearchTree(root , 9);
root = buildBinarySearchTree(root , 11);


inOrderTraversal(root);
cout<<endl;

vector<int> path;
printAllPath(root,path);
return 0;
}
99 changes: 99 additions & 0 deletions binary_search_tree/print_in_range.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include<bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node *left,*right;
//Constructor:
node(int data)
{
this->data=data;
left=right=NULL;
}
};
//Print in Range -->elf:
void printInRange(node *root , int a, int b)
{
if(root==NULL)
{
return;
}
printInRange(root->left , a , b);
if(a<=root->data and root->data<=b){
cout<<root->data<<" ";
}
printInRange(root->right , a, b);
}
// Second -Way
void printRange(node *root , int a, int b)
{
if(root==NULL)
{
return;
}
else if(a<=root->data and root->data<=b)
{
printRange(root->left ,a ,b);
cout<<root->data<<" ";
printRange(root->right ,a ,b);
}
else if(root->data > b)
{
printRange(root->left ,a ,b);
}
else if(root->data < a)
{
printRange(root->right ,a ,b);
}
}
//Build Binary Search Tree:
node *buildBinarySearchTree(node *root , int data)
{
if(root==NULL)
{
node *temp=new node(data);
return temp;
}
if(data < root->data)
{
root->left=buildBinarySearchTree(root->left , data);
}
else if(data > root->data)
{
root->right=buildBinarySearchTree(root->right , data);
}
return root;
}
//Inorder Traversal:
void inOrderTraversal(node * root)
{
if(root==NULL)
{
return;
}
inOrderTraversal(root->left);
cout<<root->data<<" ";
inOrderTraversal(root->right);
}
//Main Function:
int main(int argc, char const *argv[])
{
node *root = buildBinarySearchTree(root , 8);
root = buildBinarySearchTree(root , 3);
root = buildBinarySearchTree(root , 10);
root = buildBinarySearchTree(root , 1);
root = buildBinarySearchTree(root , 6);
root = buildBinarySearchTree(root , 14);
root = buildBinarySearchTree(root , 4);
root = buildBinarySearchTree(root , 7);
root = buildBinarySearchTree(root , 13);

inOrderTraversal(root);
cout<<endl;
printInRange(root, 5,12);
cout<<endl;
printRange(root , 5 ,12);
return 0;
}
Loading