-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Prateek Narang
committed
Dec 28, 2014
1 parent
c480fbc
commit 51b9919
Showing
62 changed files
with
596 additions
and
27 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file removed
BIN
-9.03 KB
Arrays/13. Longest substring string that contains only 2 unique chars
Binary file not shown.
Binary file removed
BIN
-3.49 KB
Arrays/13. Longest substring string that contains only 2 unique chars.o
Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
|
||
int main(){ | ||
char str[100],regExp[100]; | ||
cout<<"Enter String & Regular Expression :"; | ||
cin>>str; | ||
cin>>regExp; | ||
|
||
if(canAccept(regExp,str)) | ||
cout<<"String Accepted ! "; | ||
else | ||
cout<<"Not Accepted ! "; | ||
|
||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,89 @@ | ||
#include<iostream> | ||
using namespace std; | ||
// Note: take a sorted linked list | ||
// Lil Buggy Code | ||
struct node{ | ||
int data; | ||
struct node*next; | ||
struct node*prev; | ||
}; | ||
struct node*newNode(int data){ | ||
struct node*node = new struct node; | ||
node->data = data; | ||
node->next = NULL; | ||
node->prev = NULL; | ||
return node; | ||
} | ||
|
||
void insertDLL(struct node**head,int data){ | ||
if(*head==NULL){ | ||
*head = newNode(data); | ||
} | ||
else { | ||
struct node*temp = newNode(data); | ||
temp->next = (*head); | ||
(*head)->prev = temp; | ||
*head = temp; | ||
} | ||
return; | ||
} | ||
void printDLL(struct node*head){ | ||
while(head!=NULL){ | ||
cout<<head->data<<" <=> "; | ||
head = head->next; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
struct node* linkedListToBST(struct node**root){ | ||
|
||
if(*root==NULL){ | ||
return NULL; | ||
} | ||
|
||
struct node*fast = *root; | ||
struct node*slow = *root; | ||
struct node*prev; | ||
|
||
|
||
|
||
while(fast!=NULL){ | ||
fast = fast->next; | ||
if(fast!=NULL) | ||
{ | ||
fast = fast->next; | ||
prev = slow; | ||
slow = slow->next; | ||
} | ||
} | ||
prev->next = NULL; //Break The links | ||
slow->prev = NULL ; | ||
|
||
struct node* prevPrev = prev->prev; | ||
prev->prev = NULL; | ||
prevPrev->next = NULL; | ||
|
||
cout<<prev->data<<endl; | ||
printDLL(*root); | ||
printDLL(slow); | ||
|
||
prev->prev = linkedListToBST(root); | ||
prev->next = linkedListToBST(&slow); | ||
return prev; | ||
} | ||
|
||
|
||
int main(){ | ||
struct node*root = NULL; | ||
insertDLL(&root,8); | ||
insertDLL(&root,7); | ||
insertDLL(&root,6); | ||
/*insertDLL(&root,5); | ||
insertDLL(&root,4); | ||
insertDLL(&root,3); | ||
insertDLL(&root,2); | ||
insertDLL(&root,1);*/ | ||
printDLL(root); | ||
linkedListToBST(&root); | ||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-34.8 KB
Trees/20. Generic Tree finding a node with max sum of children and itself
Binary file not shown.
Binary file removed
BIN
-62.8 KB
Trees/20. Generic Tree finding a node with max sum of children and itself.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
63 changes: 63 additions & 0 deletions
63
Trees/23. Contruct Tree from Preorder and Inorder Traversals.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,64 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
|
||
//---------------------Binary Search | ||
int BinarySearch(int *a,int key,int low,int high){ | ||
while(low<=high){ | ||
int mid = (low+high)/2; | ||
if(a[mid]==key) | ||
return mid; | ||
else if(a[mid]>key) | ||
high = mid-1; | ||
else | ||
low = mid+1; | ||
} | ||
return -1; //Not found | ||
} | ||
|
||
struct node{ | ||
int data; | ||
struct node*left; | ||
struct node*right; | ||
}; | ||
|
||
struct node*constructTree(int *pre,int *in,int low,int high,int &pre_index,int n){ | ||
|
||
if(pre_index>=n) | ||
return NULL; | ||
|
||
if(low>high){ | ||
return NULL; | ||
} | ||
|
||
else if(low<=high){ | ||
|
||
struct node* node = new struct node; | ||
node->data = pre[pre_index]; | ||
int index_in = BinarySearch(in,pre[pre_index],low,high); | ||
pre_index++; | ||
node->left = constructTree(pre,in,low,index_in-1,pre_index,n); | ||
node->right = constructTree(pre,in,index_in+1,high,pre_index,n); | ||
return node; | ||
} | ||
} | ||
|
||
|
||
void printInorder(struct node*root){ | ||
if(root==NULL) | ||
return; | ||
|
||
printInorder(root->left); | ||
cout<<root->data<<" "; | ||
printInorder(root->right); | ||
|
||
} | ||
|
||
int main(){ | ||
int preorder[] = {6,3,1,4,7,8,9}; | ||
int inorder[] ={1,3,4,6,7,8,9}; | ||
int n = sizeof(inorder)/sizeof(int); | ||
int pre_index = 0; | ||
struct node*root = constructTree(preorder,inorder,0,n-1,pre_index,n); | ||
printInorder(root); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include<iostream> | ||
#include<queue> | ||
using namespace std; | ||
|
||
struct node{ | ||
int data; | ||
struct node*left; | ||
struct node*right; | ||
struct node*next; //Next will point to inorder successor | ||
}; | ||
|
||
//---------------------------------------Build Level Order | ||
struct node*build(){ | ||
struct node*root = new node; | ||
cout<<"Enter Root's Data"; | ||
cin>>root->data; | ||
|
||
queue<node*> q; | ||
q.push(root); | ||
|
||
while(!q.empty()){ | ||
|
||
struct node*temp = q.front(); | ||
q.pop(); | ||
cout<<"Enter the children (or -1) for node with data as "<<temp->data<<" : "; | ||
int data1; | ||
int data2; | ||
cin>>data1>>data2; | ||
|
||
if(data1!=-1){ | ||
temp->left = new node; | ||
temp->left->data = data1; | ||
temp->left->next = NULL; | ||
q.push(temp->left); | ||
} | ||
else{ | ||
temp->left = NULL; | ||
} | ||
|
||
if(data2!=-1){ | ||
temp->right = new node; | ||
temp->right->data = data2; | ||
temp->right->next = NULL; | ||
q.push(temp->right); | ||
} | ||
else{ | ||
temp->right = NULL; | ||
} | ||
} | ||
return root; | ||
} | ||
//--------------------------------------------------Print Level Order | ||
void printLevelOrder(node*root){ | ||
|
||
queue<node*> q; | ||
q.push(root); | ||
q.push(NULL); | ||
|
||
while(!q.empty()){ | ||
node*temp = q.front(); | ||
q.pop(); | ||
|
||
if(q.empty()) | ||
{ | ||
cout<<endl; | ||
break; | ||
} | ||
|
||
else if(temp==NULL) | ||
{ | ||
q.push(NULL); | ||
cout<<endl; | ||
} | ||
else{ | ||
cout<<temp->data<<" "; | ||
if(temp->left!=NULL){ | ||
q.push(temp->left); | ||
} | ||
if(temp->right!=NULL){ | ||
q.push(temp->right); | ||
} | ||
} | ||
} | ||
} | ||
//---------------------------------------------------Set Next pointers | ||
void setNextPointers(struct node*root){ | ||
if(root==NULL) | ||
{ | ||
return; | ||
} | ||
struct node*temp = root ; | ||
|
||
|
||
if(root->left!=NULL){ | ||
root->left->next = root; | ||
} | ||
setNextPointers(root->left); | ||
|
||
if(root->right!=NULL){ | ||
root->right->next = root->next; | ||
root->next = root->right; | ||
} | ||
setNextPointers(root->right); | ||
} | ||
|
||
//---------------------------------------------------Print Inorder | ||
void printInorder(struct node*node){ | ||
if(node==NULL) | ||
return; | ||
printInorder(node->left); | ||
if(node->next!=NULL) | ||
cout<<node->data<<" -> "<<node->next->data<<endl; | ||
else | ||
cout<<node->data<<" -> NULL "; | ||
printInorder(node->right); | ||
|
||
} | ||
|
||
void printInorderUsingNextPointers(struct node*root){ | ||
while(root!=NULL) | ||
{ | ||
cout<<root->data<<" "; | ||
root = root->next; | ||
|
||
} | ||
} | ||
|
||
int main(){ | ||
struct node*root1 = build(); | ||
root1->next = NULL; | ||
setNextPointers(root1); | ||
|
||
cout<<"Inorder Recursive with next fields :"<<endl; | ||
printInorder(root1); | ||
cout<<endl<<"Inorder Using Next Pointers (Efficient):"<<endl; | ||
printInorderUsingNextPointers(root1); // Not it will start from root and it will print only the next larger elements | ||
cout<<endl; | ||
} |
Oops, something went wrong.