Skip to content

Commit c1b99d3

Browse files
author
Prateek Narang
committed
2 parents 2a3945c + 4114eb8 commit c1b99d3

6 files changed

+416
-101
lines changed

Queue/4.Sort a Queue in place.cpp

+38-93
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,58 @@
11
#include<iostream>
2+
#include<queue>
23
using namespace std;
34

4-
//INCOMPLETE
5-
struct Queue{
6-
int arr[100];
7-
int front;
8-
int rear;
9-
int currentSize;
10-
};
11-
12-
13-
void enQueue(Queue &Q,int x){
14-
15-
Q.rear += 1; //Bhaga Lo Pehle
16-
17-
if(Q.currentSize==100){
18-
cout<<"Q is Full "<<endl;
19-
return ;
20-
}
21-
22-
if(Q.rear == 100)
23-
Q.rear =0;
24-
25-
Q.arr[Q.rear]=x;
26-
Q.currentSize++;
27-
}
28-
29-
void deQueue(Queue &Q){
30-
if(Q.currentSize==0){
31-
cout<<"Empty "<<endl;
32-
return ;
33-
}
34-
Q.front++;
35-
if(Q.front==100){
36-
Q.front = 0;
5+
queue<int> q;
6+
//This function just returns the minimum in the window 0 to k
7+
int findMinimum(int k,int n)
8+
{ int min = 100000;
9+
for(int i=0;i<n;i++){
10+
int current = q.front();
11+
q.pop();
12+
if(current <min && i < k){
13+
min = current;
3714
}
38-
Q.currentSize--;
39-
if(Q.currentSize==0){
40-
Q.front = 0;
41-
Q.rear = -1;
15+
q.push(current);
4216
}
17+
return min;
4318
}
44-
45-
46-
void print(Queue Q){
47-
48-
while(Q.currentSize){
49-
cout<<Q.arr[Q.front]<<" ";
50-
deQueue(Q);
51-
}
52-
cout<<endl;
53-
}
54-
55-
//------------------------------Sorting a Queue in Place involves two steps.
56-
// Finding a Min element
57-
// Reordering the queue
58-
59-
60-
/*This is just returns the min element without affecting queue state*/
61-
int findMin(Queue &Q,int k,int n){
62-
int Min = 1000000000;
63-
19+
//This places the minimum value at correct position
20+
void reorder(int min,int n){
6421
for(int i=0;i<n;i++){
65-
int current = Q.arr[Q.front];
66-
67-
deQueue(Q);
68-
if(current<Min && i<k)
69-
{ Min = current;}
70-
71-
enQueue(Q,current);
22+
int current = q.front();
23+
q.pop();
24+
if(current!=min){
25+
q.push(current);
7226
}
73-
print(Q); //This Prints the Queue , print function takes Q by copy and doesnt modify actual Queue.
74-
return Min;
27+
}
28+
q.push(min);
7529
}
7630

77-
void reorder(Queue &Q,int Min,int n){
78-
for(int i=0;i<n;i++){
79-
int curr = Q.arr[Q.front];
80-
deQueue(Q);
81-
if(curr!=Min)
82-
{ enQueue(Q,curr) ; }
83-
84-
}
85-
enQueue(Q,Min); //EnQueue the min element at last
8631

87-
}
8832

89-
void Sort(Queue &Q){
90-
int n = Q.currentSize;
33+
void sort(){
34+
int n = q.size();
9135

9236
for(int i=0;i<n;i++){
93-
int min_no = findMin(Q,n-i,n);
94-
cout<<"Min : "<<min_no<<endl;
95-
reorder(Q,min_no,n);
37+
int min = findMinimum(n-i,n);
38+
//cout<<min<<endl;
39+
reorder(min,n);
9640
}
97-
9841
}
9942

10043

10144
int main(){
102-
Queue Q ;
103-
Q.front = 0;
104-
Q.rear = -1;
105-
enQueue(Q,9);
106-
enQueue(Q,6);
107-
enQueue(Q,1);
108-
enQueue(Q,3);
109-
enQueue(Q,5);
110-
Sort(Q);
111-
print(Q);
112-
cout<<"Finally Sorted... :D";
45+
q.push(5);
46+
q.push(4);
47+
q.push(8);
48+
q.push(2);
49+
q.push(1);
50+
q.push(9);
51+
sort();
52+
while(!q.empty()){
53+
cout<<q.front()<<" ";
54+
q.pop();
55+
}
56+
return 0;
11357
}
58+

Queue/Queue Class using Array.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
class Queue{
5+
int currentSize;
6+
int maxSize;
7+
int front;
8+
int rear ;
9+
int *q;
10+
11+
public:
12+
Queue(int);
13+
void enqueue(int no);
14+
int dequeue();
15+
bool isEmpty()
16+
{
17+
return currentSize==0?true:false;
18+
};
19+
};
20+
Queue::Queue(int maxSize){
21+
q = new int[maxSize];
22+
this->maxSize = maxSize;
23+
front = 0;
24+
rear = -1;
25+
currentSize=0;
26+
}
27+
28+
void Queue::enqueue(int x){
29+
30+
if(currentSize==maxSize){
31+
cout<<"Queue is Full"<<endl;
32+
return;
33+
}
34+
if(rear ==maxSize)
35+
rear=0;
36+
37+
rear++;
38+
q[rear]=x;
39+
currentSize++;
40+
return;
41+
}
42+
43+
int Queue::dequeue(){
44+
45+
if(currentSize==0)
46+
{
47+
cout<<"Empty Queue";
48+
return -1;
49+
}
50+
51+
int ans = q[front];
52+
front++;
53+
currentSize--;
54+
55+
if(front==maxSize)
56+
front=0;
57+
58+
if(currentSize==0){
59+
front=0;
60+
rear = -1;
61+
}
62+
63+
return ans;
64+
}
65+
66+
67+
void print(Queue q){
68+
while(!q.isEmpty()){
69+
cout<<q.dequeue()<<" ";
70+
}
71+
72+
return ;
73+
}
74+
75+
int main(){
76+
Queue *q = new Queue(5);
77+
q->enqueue(1);
78+
q->enqueue(2);
79+
q->enqueue(3);
80+
q->enqueue(4);
81+
q->enqueue(5);
82+
q->enqueue(6);
83+
q->enqueue(7);
84+
print(*q);
85+
return 0;
86+
}
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
class Queue{
5+
int currentSize;
6+
int maxSize;
7+
int front;
8+
int rear ;
9+
int *q;
10+
11+
public:
12+
Queue(int);
13+
void enqueue(int no);
14+
int dequeue();
15+
16+
void recursiveReverse(){
17+
if(currentSize==0)
18+
return;
19+
int x = dequeue();
20+
recursiveReverse();
21+
enqueue(x);
22+
}
23+
24+
bool isEmpty()
25+
{
26+
return currentSize==0?true:false;
27+
};
28+
};
29+
Queue::Queue(int maxSize){
30+
q = new int[maxSize];
31+
this->maxSize = maxSize;
32+
front = 0;
33+
rear = -1;
34+
currentSize=0;
35+
}
36+
37+
void Queue::enqueue(int x){
38+
39+
if(currentSize==maxSize){
40+
cout<<"Queue is Full"<<endl;
41+
return;
42+
}
43+
if(rear ==maxSize)
44+
rear=0;
45+
46+
rear++;
47+
q[rear]=x;
48+
currentSize++;
49+
return;
50+
}
51+
52+
int Queue::dequeue(){
53+
54+
if(currentSize==0)
55+
{
56+
cout<<"Empty Queue";
57+
return -1;
58+
}
59+
60+
int ans = q[front];
61+
front++;
62+
currentSize--;
63+
64+
if(front==maxSize)
65+
front=0;
66+
67+
if(currentSize==0){
68+
front=0;
69+
rear = -1;
70+
}
71+
72+
return ans;
73+
}
74+
75+
76+
void print(Queue q){
77+
while(!q.isEmpty()){
78+
cout<<q.dequeue()<<" ";
79+
}
80+
81+
return ;
82+
}
83+
84+
85+
int main(){
86+
Queue *q = new Queue(5);
87+
q->enqueue(1);
88+
q->enqueue(2);
89+
q->enqueue(3);
90+
q->enqueue(4);
91+
q->enqueue(5);
92+
q->enqueue(6);
93+
q->enqueue(7);
94+
q->recursiveReverse();
95+
print(*q);
96+
return 0;
97+
}

Trees/6.Null .cpp

-1
This file was deleted.

Trees/8. Max Sum of all nodes by taking either parent or children.cpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,26 @@ printInorder(node->right);
9696
//--------------------------------------------------Find Max Sum
9797

9898

99+
struct output{
100+
int inc;
101+
int exc;
102+
};
103+
104+
output findMaxSumChildOrParent( struct node*root ){
105+
output ans ;
106+
if(root ==NULL)
107+
{
108+
ans.inc =0;
109+
ans.exc =0;
110+
return ans;
111+
}
112+
output left = findMaxSumChildOrParent(root->left);
113+
output right = findMaxSumChildOrParent(root->right);
99114

115+
ans.inc = root->data + left.exc + right.exc ;
116+
ans.exc = max(left.exc,left.inc) + max(right.exc,right.inc);
117+
return ans;
118+
}
100119

101120

102121

@@ -108,13 +127,10 @@ printInorder(node->right);
108127
int main(){
109128
struct node*root = build();
110129
cout<<"Level Order :"<<endl;
111-
printLevelOrder(root);
130+
//printLevelOrder(root);
112131
cout<<"Inorder : "<<endl;
113-
printInorder(root);
114-
cout<<endl<<"Vertical Order : "<<endl;
115-
printVertical(root);
132+
//printInorder(root);
133+
output ans = findMaxSumChildOrParent(root);
134+
cout<<endl<<"Max Possible Sum is : "<<max(ans.inc,ans.exc);
116135
return 0;
117136
}
118-
119-
120-

0 commit comments

Comments
 (0)