Skip to content

Commit 51b9919

Browse files
author
Prateek Narang
committed
updated trees
1 parent c480fbc commit 51b9919

File tree

62 files changed

+596
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+596
-27
lines changed

Arrays/1. Print Pattern

-8.87 KB
Binary file not shown.

Arrays/1. Print Pattern.o

-2.94 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
-9.04 KB
Binary file not shown.
-3.34 KB
Binary file not shown.
Binary file not shown.
-8.99 KB
Binary file not shown.
-3.03 KB
Binary file not shown.

Queue/4.Sort a Queue in place

-13.2 KB
Binary file not shown.

Queue/4.Sort a Queue in place.o

-5.12 KB
Binary file not shown.
File renamed without changes.

Recursion/15. Print all paths in grid

-13.4 KB
Binary file not shown.
-5.41 KB
Binary file not shown.
-13.5 KB
Binary file not shown.
-6.69 KB
Binary file not shown.

Recursion/20.Making a Maze solver

-13.4 KB
Binary file not shown.

Recursion/20.Making a Maze solver.o

-4.85 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
int main(){
6+
char str[100],regExp[100];
7+
cout<<"Enter String & Regular Expression :";
8+
cin>>str;
9+
cin>>regExp;
10+
11+
if(canAccept(regExp,str))
12+
cout<<"String Accepted ! ";
13+
else
14+
cout<<"Not Accepted ! ";
15+
16+
return 0;
17+
}
Binary file not shown.
Binary file not shown.

Trees/10. Print Mirror of Tree

-34.6 KB
Binary file not shown.

Trees/10. Print Mirror of Tree.o

-60.9 KB
Binary file not shown.

Trees/12. BST Insertion

-34.6 KB
Binary file not shown.

Trees/12. BST Insertion.o

-61.9 KB
Binary file not shown.

Trees/13. BST Deletion

-34.7 KB
Binary file not shown.

Trees/13. BST Deletion.o

-62.9 KB
Binary file not shown.

Trees/14. BST Check

-34.7 KB
Binary file not shown.

Trees/14. BST Check .o

-62.4 KB
Binary file not shown.

Trees/15. Print BST elements in range K1 and K2.cpp

+14-27
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ while(!q.empty()){
3636
}
3737
if(temp->right!=NULL){
3838
q.push(temp->right);
39+
}
3940
}
4041
}
4142
}
42-
}
4343

4444
//----------------------------------------------------Create from Array
4545
struct node*createBST(int *a,int low,int high){
@@ -87,45 +87,32 @@ else
8787
root->right = insert(root->right,data);
8888
return root;
8989
}
90-
//---------------------------------------------------------CHECK IF A BST OR NOT
91-
void PrintElementsInRange(struct node*root ,int k1,int k2){
92-
if(root==NULL)
93-
return NULL;
94-
95-
96-
if(root->data>=k1)
97-
{ PrintElementsInRange(root->left,k1,k2);}
90+
//---------------------------------------Print elements in the range
91+
void printRange(struct node*root,int k1,int k2){
92+
if(root==NULL)
93+
return;
9894

99-
else if(root->data>=k2)
100-
{
95+
printRange(root->left,k1,k2);
10196

10297

103-
}
104-
98+
if(root->data >= k1&&root->data<=k2)
10599

100+
cout<<root->data<<" ";
106101

102+
if(root->data>=k2)
103+
{ return ;}
107104

105+
printRange(root->right,k1,k2);
108106
}
109-
110-
//---------------------------------------Main
111-
112107
int main(){
113-
int a[8]={1,2,6,4,5,6,7,9};
108+
int a[8]={1,1,3,4,5,6,7,9};
114109
struct node*root = NULL;
115110
root = createBST(a,0,7);
116111
cout<<"Originial "<<endl;
117112
insert(root,8);
118-
printLevelOrder(root);
119-
cout<<endl<<"Inorder : "<<endl;
120-
printPreorder(root);
121-
cout<<endl;
122-
if(isBST(root,-1000,1000))
123-
cout<<"Dekh Bhai , Ye to BST Hi Hai ... !"<<endl;
124-
125-
else{
126-
cout<<"Na Bhai , BST nahin hai ... ! "<<endl;
127-
}
113+
printRange(root,2,8); .
128114
return 0;
115+
129116
}
130117

131118

-34.8 KB
Binary file not shown.
-62.3 KB
Binary file not shown.
-34.7 KB
Binary file not shown.
-63.3 KB
Binary file not shown.
+88
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
1+
#include<iostream>
2+
using namespace std;
3+
// Note: take a sorted linked list
4+
// Lil Buggy Code
5+
struct node{
6+
int data;
7+
struct node*next;
8+
struct node*prev;
9+
};
10+
struct node*newNode(int data){
11+
struct node*node = new struct node;
12+
node->data = data;
13+
node->next = NULL;
14+
node->prev = NULL;
15+
return node;
16+
}
117

18+
void insertDLL(struct node**head,int data){
19+
if(*head==NULL){
20+
*head = newNode(data);
21+
}
22+
else {
23+
struct node*temp = newNode(data);
24+
temp->next = (*head);
25+
(*head)->prev = temp;
26+
*head = temp;
27+
}
28+
return;
29+
}
30+
void printDLL(struct node*head){
31+
while(head!=NULL){
32+
cout<<head->data<<" <=> ";
33+
head = head->next;
34+
}
35+
cout<<endl;
36+
}
37+
38+
struct node* linkedListToBST(struct node**root){
39+
40+
if(*root==NULL){
41+
return NULL;
42+
}
43+
44+
struct node*fast = *root;
45+
struct node*slow = *root;
46+
struct node*prev;
47+
48+
49+
50+
while(fast!=NULL){
51+
fast = fast->next;
52+
if(fast!=NULL)
53+
{
54+
fast = fast->next;
55+
prev = slow;
56+
slow = slow->next;
57+
}
58+
}
59+
prev->next = NULL; //Break The links
60+
slow->prev = NULL ;
61+
62+
struct node* prevPrev = prev->prev;
63+
prev->prev = NULL;
64+
prevPrev->next = NULL;
65+
66+
cout<<prev->data<<endl;
67+
printDLL(*root);
68+
printDLL(slow);
69+
70+
prev->prev = linkedListToBST(root);
71+
prev->next = linkedListToBST(&slow);
72+
return prev;
73+
}
74+
75+
76+
int main(){
77+
struct node*root = NULL;
78+
insertDLL(&root,8);
79+
insertDLL(&root,7);
80+
insertDLL(&root,6);
81+
/*insertDLL(&root,5);
82+
insertDLL(&root,4);
83+
insertDLL(&root,3);
84+
insertDLL(&root,2);
85+
insertDLL(&root,1);*/
86+
printDLL(root);
87+
linkedListToBST(&root);
88+
return 0;
89+
}
Binary file not shown.
Binary file not shown.

Trees/2. Generic Tree Class

-34.9 KB
Binary file not shown.

Trees/2. Generic Tree Class.o

-64.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

Trees/21. Binary Tree Class

-35.1 KB
Binary file not shown.

Trees/21. Binary Tree Class.o

-66.5 KB
Binary file not shown.
-34.7 KB
Binary file not shown.
-63.3 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1+
#include<iostream>
2+
using namespace std;
13

4+
5+
//---------------------Binary Search
6+
int BinarySearch(int *a,int key,int low,int high){
7+
while(low<=high){
8+
int mid = (low+high)/2;
9+
if(a[mid]==key)
10+
return mid;
11+
else if(a[mid]>key)
12+
high = mid-1;
13+
else
14+
low = mid+1;
15+
}
16+
return -1; //Not found
17+
}
18+
19+
struct node{
20+
int data;
21+
struct node*left;
22+
struct node*right;
23+
};
24+
25+
struct node*constructTree(int *pre,int *in,int low,int high,int &pre_index,int n){
26+
27+
if(pre_index>=n)
28+
return NULL;
29+
30+
if(low>high){
31+
return NULL;
32+
}
33+
34+
else if(low<=high){
35+
36+
struct node* node = new struct node;
37+
node->data = pre[pre_index];
38+
int index_in = BinarySearch(in,pre[pre_index],low,high);
39+
pre_index++;
40+
node->left = constructTree(pre,in,low,index_in-1,pre_index,n);
41+
node->right = constructTree(pre,in,index_in+1,high,pre_index,n);
42+
return node;
43+
}
44+
}
45+
46+
47+
void printInorder(struct node*root){
48+
if(root==NULL)
49+
return;
50+
51+
printInorder(root->left);
52+
cout<<root->data<<" ";
53+
printInorder(root->right);
54+
55+
}
56+
57+
int main(){
58+
int preorder[] = {6,3,1,4,7,8,9};
59+
int inorder[] ={1,3,4,6,7,8,9};
60+
int n = sizeof(inorder)/sizeof(int);
61+
int pre_index = 0;
62+
struct node*root = constructTree(preorder,inorder,0,n-1,pre_index,n);
63+
printInorder(root);
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
5+
struct node{
6+
int data;
7+
struct node*left;
8+
struct node*right;
9+
struct node*next; //Next will point to inorder successor
10+
};
11+
12+
//---------------------------------------Build Level Order
13+
struct node*build(){
14+
struct node*root = new node;
15+
cout<<"Enter Root's Data";
16+
cin>>root->data;
17+
18+
queue<node*> q;
19+
q.push(root);
20+
21+
while(!q.empty()){
22+
23+
struct node*temp = q.front();
24+
q.pop();
25+
cout<<"Enter the children (or -1) for node with data as "<<temp->data<<" : ";
26+
int data1;
27+
int data2;
28+
cin>>data1>>data2;
29+
30+
if(data1!=-1){
31+
temp->left = new node;
32+
temp->left->data = data1;
33+
temp->left->next = NULL;
34+
q.push(temp->left);
35+
}
36+
else{
37+
temp->left = NULL;
38+
}
39+
40+
if(data2!=-1){
41+
temp->right = new node;
42+
temp->right->data = data2;
43+
temp->right->next = NULL;
44+
q.push(temp->right);
45+
}
46+
else{
47+
temp->right = NULL;
48+
}
49+
}
50+
return root;
51+
}
52+
//--------------------------------------------------Print Level Order
53+
void printLevelOrder(node*root){
54+
55+
queue<node*> q;
56+
q.push(root);
57+
q.push(NULL);
58+
59+
while(!q.empty()){
60+
node*temp = q.front();
61+
q.pop();
62+
63+
if(q.empty())
64+
{
65+
cout<<endl;
66+
break;
67+
}
68+
69+
else if(temp==NULL)
70+
{
71+
q.push(NULL);
72+
cout<<endl;
73+
}
74+
else{
75+
cout<<temp->data<<" ";
76+
if(temp->left!=NULL){
77+
q.push(temp->left);
78+
}
79+
if(temp->right!=NULL){
80+
q.push(temp->right);
81+
}
82+
}
83+
}
84+
}
85+
//---------------------------------------------------Set Next pointers
86+
void setNextPointers(struct node*root){
87+
if(root==NULL)
88+
{
89+
return;
90+
}
91+
struct node*temp = root ;
92+
93+
94+
if(root->left!=NULL){
95+
root->left->next = root;
96+
}
97+
setNextPointers(root->left);
98+
99+
if(root->right!=NULL){
100+
root->right->next = root->next;
101+
root->next = root->right;
102+
}
103+
setNextPointers(root->right);
104+
}
105+
106+
//---------------------------------------------------Print Inorder
107+
void printInorder(struct node*node){
108+
if(node==NULL)
109+
return;
110+
printInorder(node->left);
111+
if(node->next!=NULL)
112+
cout<<node->data<<" -> "<<node->next->data<<endl;
113+
else
114+
cout<<node->data<<" -> NULL ";
115+
printInorder(node->right);
116+
117+
}
118+
119+
void printInorderUsingNextPointers(struct node*root){
120+
while(root!=NULL)
121+
{
122+
cout<<root->data<<" ";
123+
root = root->next;
124+
125+
}
126+
}
127+
128+
int main(){
129+
struct node*root1 = build();
130+
root1->next = NULL;
131+
setNextPointers(root1);
132+
133+
cout<<"Inorder Recursive with next fields :"<<endl;
134+
printInorder(root1);
135+
cout<<endl<<"Inorder Using Next Pointers (Efficient):"<<endl;
136+
printInorderUsingNextPointers(root1); // Not it will start from root and it will print only the next larger elements
137+
cout<<endl;
138+
}

0 commit comments

Comments
 (0)