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

Implemented Red Black Tree in C++ #3557

Open
wants to merge 3 commits into
base: master
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
17 changes: 17 additions & 0 deletions Logical Problems/reverse-number/reverse_big_int.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<bits/stdc++.h>
using namespace std;

string reverse_number(string s){
reverse(s.begin(),s.end());
for(auto it:s){
if(it<'0'||it>'9') return "Error";
}
return s;
}

int main(){
string s;
cin>>s;
cout<<reverse_number(s)<<endl;
return 0;
}
44 changes: 24 additions & 20 deletions data_structures/Arrays/Java/CountNegativeNumbersIn2DArray.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
class Solution {
public int countNegatives(int[][] grid) {
int rows = grid.length;
int columns = grid[0].length;
int rp = 0;
int cp = columns -1;
int count=0;
while(cp>=0 && rp<rows)
{
if(grid[rp][cp]<0)
{
count+=(rows-rp);
cp--;
import java.util.Scanner;
import java.util.*;

public class countNegativeNumbersIn2dArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows:");
int n = sc.nextInt();
System.out.println("Enter the number of columns:");
int m = sc.nextInt();
int[][] arr = new int[n][m];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
else
{
rp++;
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] < 0) {
count++;
}
}

}
return count;

System.out.println("The number of negative numbers in the array is: " + count);
}
}
}
205 changes: 205 additions & 0 deletions data_structures/RB_tree/C++.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include<bits/stdc++.h>
using namespace std;

struct node{
int val;
node *left=NULL,*right=NULL,*par=NULL;
bool red;
node(int x=0,bool c=true):val(x),red(c){}
};

class RB_tree{
private:
node* r=NULL;

private:
node* bst_insert(node* root,int key){
if(root==NULL) return new node(key);
if(key<root->val) root->left=bst_insert(root->left,key);
else root->right=bst_insert(root->right,key);
return root;
}

void left_rotate(node* root){
node* y=root->right;
root->right=y->left;
if(y->left!=NULL) y->left->par=root;
y->par=root->par;
if(root->par==NULL) r=y;
else if(root==root->par->left) root->par->left=y;
else root->par->right=y;
y->left=root;
root->par=y;
}

void right_rotate(node* root){
node* y=root->left;
root->left=y->right;
if(y->right!=NULL) y->right->par=root;
y->par=root->par;
if(root->par==NULL) r=y;
else if(root==root->par->left) root->par->left=y;
else root->par->right=y;
y->right=root;
root->par=y;
}

bool find(node* root,int key){
if(root==NULL) return false;
if(root->val==key) return true;
if(key>root->val) return find(root->right,key);
return find(root->left,key);
}

node* bst_del(node* root,int key){
if(root==NULL) return root;
if(key==root->val){
node* curr=root;
if(root->left==NULL) curr=root->right;
else if(root->right==NULL) curr=root->left;
else{
node* succ=root->right;
while(succ->left!=NULL){
curr=succ;
succ=succ->left;
}
if(curr!=root) curr->left=succ->right;
else root->right=succ->right;
root->val=succ->val;
curr=succ;
}
delete curr;
return root;

}
else if(key<root->val) root->left=bst_del(root->left,key);
else root->right=bst_del(root->right,key);
return root;
}

void inorder(node* root){
if(root==NULL) return;
inorder(root->left);
cout<<root->val<<' ';
inorder(root->right);
}

public:
void rb_insert(int key){
r=bst_insert(r,key);
node* z=r;
while(z->par!=NULL&&z->par->par!=NULL&&z->par->red){
if(z->par==z->par->par->left){
node* y=z->par->par->right;
if(y!=NULL&&y->red){
z->par->red=false;
y->red=false;
z->par->par->red=true;
z=z->par->par;
}
else{
if(z==z->par->right){
z=z->par;
left_rotate(z);
}
z->par->red=false;
z->par->par->red=true;
right_rotate(z->par->par);
}
}
else{
node* y=z->par->par->left;
if(y!=NULL&&y->red){
z->par->red=false;
y->red=false;
z->par->par->red=true;
z=z->par->par;
}
else{
if(z==z->par->left){
z=z->par;
right_rotate(z);
}
z->par->red=false;
z->par->par->red=true;
left_rotate(z->par->par);
}
}
}
r->red=false;
}

void rb_del(int key){
r=bst_del(r,key);
node* x=r;
while(x!=NULL&&x->par!=NULL&&!x->red){
if(x==x->par->left){
node* w=x->par->right;
if(w->red){
w->red=false;
x->par->red=true;
left_rotate(x->par);
w=x->par->right;
}
if((w->left==NULL||!w->left->red)&&(w->right==NULL||!w->right->red)){
w->red=true;
x=x->par;
}
else{
if(w->right==NULL||!w->right->red){
w->left->red=false;
w->red=true;
right_rotate(w);
w=x->par->right;
}
w->red=x->par->red;
x->par->red=false;
w->right->red=false;
left_rotate(x->par);
x=r;
}
}
else{
node* w=x->par->left;
if(w->red){
w->red=false;
x->par->red=true;
right_rotate(x->par);
w=x->par->left;
}
if((w->left==NULL||!w->left->red)&&(w->right==NULL||!w->right->red)){
w->red=true;
x=x->par;
}
else{
if(w->left==NULL||!w->left->red){
w->right->red=false;
w->red=true;
left_rotate(w);
w=x->par->left;
}
w->red=x->par->red;
x->par->red=false;
w->left->red=false;
right_rotate(x->par);
x=r;
}
}
}
if(x!=NULL) x->red=false;
}

bool find(int key){
return find(r,key);
}

void print(){
inorder(r);
cout<<'\n';
}
};

int main() {

return 0;
}
51 changes: 28 additions & 23 deletions search/binary_search/C/binarySearch.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

int binarySearch(int arr[], int start, int end, int x) {
printf("Enter number of elements\n");
scanf("%d", &n);

if (start <= end) {
printf("Enter %d integers\n", n);

int mid = start + (end - start) / 2;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

if (arr[mid] == x)
return mid;
printf("Enter value to find\n");
scanf("%d", &search);

if (arr[mid] > x)
return binarySearch(arr, start, mid - 1, x);
first = 0;
last = n - 1;
middle = (first+last)/2;

return binarySearch(arr, mid + 1, end, x);
}
return -1;
}
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

int main()
{
int arr[] = { 2, 3, 7, 8, 78, 99, 102, 5555 };
int x = 9;
int result = binarySearch(arr, 0, sizeof(arr) / sizeof(arr[0]), x);

if (result == -1) {
printf("Element not present");
}
else {
printf("Element found at index %d", result);
}
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
Loading
Loading