Skip to content

Commit

Permalink
updated trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Prateek Narang committed Dec 28, 2014
1 parent c480fbc commit 51b9919
Show file tree
Hide file tree
Showing 62 changed files with 596 additions and 27 deletions.
Binary file removed Arrays/1. Print Pattern
Binary file not shown.
Binary file removed Arrays/1. Print Pattern.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed Arrays/14. Print a 2D array in zigzag manner
Binary file not shown.
Binary file removed Arrays/14. Print a 2D array in zigzag manner.o
Binary file not shown.
Binary file not shown.
Binary file removed Miscellanous/Program to test structure array
Binary file not shown.
Binary file removed Miscellanous/Program to test structure array.o
Binary file not shown.
Binary file removed Queue/4.Sort a Queue in place
Binary file not shown.
Binary file removed Queue/4.Sort a Queue in place.o
Binary file not shown.
File renamed without changes.
Binary file removed Recursion/15. Print all paths in grid
Binary file not shown.
Binary file removed Recursion/15. Print all paths in grid.o
Binary file not shown.
Binary file removed Recursion/19. Sudoku Solver and checker
Binary file not shown.
Binary file removed Recursion/19. Sudoku Solver and checker.o
Binary file not shown.
Binary file removed Recursion/20.Making a Maze solver
Binary file not shown.
Binary file removed Recursion/20.Making a Maze solver.o
Binary file not shown.
17 changes: 17 additions & 0 deletions Recursion/22. Regex and String Matching.cpp
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 removed Trees/10. Print Mirror of Tree
Binary file not shown.
Binary file removed Trees/10. Print Mirror of Tree.o
Binary file not shown.
Binary file removed Trees/12. BST Insertion
Binary file not shown.
Binary file removed Trees/12. BST Insertion.o
Binary file not shown.
Binary file removed Trees/13. BST Deletion
Binary file not shown.
Binary file removed Trees/13. BST Deletion.o
Binary file not shown.
Binary file removed Trees/14. BST Check
Binary file not shown.
Binary file removed Trees/14. BST Check .o
Binary file not shown.
41 changes: 14 additions & 27 deletions Trees/15. Print BST elements in range K1 and K2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ while(!q.empty()){
}
if(temp->right!=NULL){
q.push(temp->right);
}
}
}
}
}

//----------------------------------------------------Create from Array
struct node*createBST(int *a,int low,int high){
Expand Down Expand Up @@ -87,45 +87,32 @@ else
root->right = insert(root->right,data);
return root;
}
//---------------------------------------------------------CHECK IF A BST OR NOT
void PrintElementsInRange(struct node*root ,int k1,int k2){
if(root==NULL)
return NULL;


if(root->data>=k1)
{ PrintElementsInRange(root->left,k1,k2);}
//---------------------------------------Print elements in the range
void printRange(struct node*root,int k1,int k2){
if(root==NULL)
return;

else if(root->data>=k2)
{
printRange(root->left,k1,k2);


}

if(root->data >= k1&&root->data<=k2)

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

if(root->data>=k2)
{ return ;}

printRange(root->right,k1,k2);
}

//---------------------------------------Main

int main(){
int a[8]={1,2,6,4,5,6,7,9};
int a[8]={1,1,3,4,5,6,7,9};
struct node*root = NULL;
root = createBST(a,0,7);
cout<<"Originial "<<endl;
insert(root,8);
printLevelOrder(root);
cout<<endl<<"Inorder : "<<endl;
printPreorder(root);
cout<<endl;
if(isBST(root,-1000,1000))
cout<<"Dekh Bhai , Ye to BST Hi Hai ... !"<<endl;

else{
cout<<"Na Bhai , BST nahin hai ... ! "<<endl;
}
printRange(root,2,8); .
return 0;

}


Binary file removed Trees/16. Convert A BST into sorted Linked List
Binary file not shown.
Binary file removed Trees/16. Convert A BST into sorted Linked List.o
Binary file not shown.
Binary file removed Trees/17. Finding Largest BST in a Binary Tree
Binary file not shown.
Binary file removed Trees/17. Finding Largest BST in a Binary Tree.o
Binary file not shown.
88 changes: 88 additions & 0 deletions Trees/18. Linked List to Balanced BST.cpp
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 removed Trees/19. Generic Tree Build and Print Recursively.o
Binary file not shown.
Binary file removed Trees/2. Generic Tree Class
Binary file not shown.
Binary file removed Trees/2. Generic Tree Class.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed Trees/21. Binary Tree Class
Binary file not shown.
Binary file removed Trees/21. Binary Tree Class.o
Binary file not shown.
Binary file removed Trees/22. Check if Trees are Identical
Binary file not shown.
Binary file removed Trees/22. Check if Trees are Identical .o
Binary file not shown.
63 changes: 63 additions & 0 deletions Trees/23. Contruct Tree from Preorder and Inorder Traversals.cpp
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);
}
138 changes: 138 additions & 0 deletions Trees/24. Convert tree into inorder threaded tree.cpp
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;
}
Loading

0 comments on commit 51b9919

Please sign in to comment.