forked from reachanihere/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Second_Largest.cpp
256 lines (210 loc) · 4.85 KB
/
Second_Largest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Given a generic tree, find and return the node with second largest value in given tree. Return NULL if no node with required value is present.
Input format :
Line 1 : Elements in level order form separated by space (as per done in class). Order is -
Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element
Sample Input 1 :
10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 1 :
40
Failing test case
*/
// Following is the given Tree node structure
/**************
class TreeNode {
TreeNode<T>** children;
int childCount;
public:
T data;
TreeNode(T data); // Constructor
int numChildren();
void addChild(TreeNode<T>* child);
TreeNode<T>* getChild(int index);
void setChild(int index, TreeNode<T>* child);
};
***************/
#include <iostream>
#include <climits>
using namespace std;
template <typename T>
class TreeNode {
TreeNode<T>** children;
int childCount;
int maxChildCount;
public:
T data;
TreeNode(T data) {
this->data = data;
childCount = 0;
children = new TreeNode<T>*[10];
maxChildCount = 10;
}
int numChildren() {
return childCount;
}
void addChild(TreeNode<T>* child) {
children[childCount] = child;
childCount++;
if(childCount == maxChildCount) {
TreeNode<T>** childrenNew = new TreeNode<T>*[2*maxChildCount];
for(int i = 0; i < maxChildCount; i++) {
childrenNew[i] = children[i];
}
maxChildCount *= 2;
children = childrenNew;
}
}
TreeNode<T>* getChild(int index) {
return children[index];
}
void setChild(int index, TreeNode<T>* child) {
children[index] = child;
}
};
template <typename T>
class Queue {
T* data;
int capacity;
int nextIndex;
int firstIndex;
int length;
public:
Queue() {
capacity = 10;
data = new T[capacity];
length = 0;
nextIndex = 0;
firstIndex = -1;
}
int size() {
return length;
}
bool isEmpty() {
return length == 0;
}
T front() {
if (length == 0) {
return 0;
}
return data[firstIndex];
}
T dequeue() {
if (length == 0) {
return 0;
}
T output = data[firstIndex];
length--;
firstIndex = (firstIndex + 1)%capacity;
if (length == 0) {
firstIndex = -1;
nextIndex = 0;
}
return output;
}
void enqueue(T element) {
if (length == capacity) {
T* temp = data;
capacity = 2* capacity;
data = new T[capacity];
int k = 0;
for (int i = firstIndex; i < length; i++) {
data[k] = temp[i];
k++;
}
for (int i = 0; i < firstIndex; i++) {
data[k] = temp[i];
k++;
}
delete [] temp;
firstIndex = 0;
nextIndex = length;
}
data[nextIndex] = element;
length++;
nextIndex = (nextIndex + 1) % capacity;
if (firstIndex == -1) {
firstIndex = 0;
}
}
~Queue() {
delete [] data;
}
};
TreeNode<int>* takeInputLevelWise() {
Queue<TreeNode<int>*> q;
int rootData;
cin >> rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);
q.enqueue(root);
while (!q.isEmpty()) {
TreeNode<int>* frontNode = q.dequeue();
int numChildren;
cin >> numChildren;
for (int i = 0; i < numChildren; i++) {
int childData;
cin >> childData;
TreeNode<int>* childNode = new TreeNode<int>(childData);
q.enqueue(childNode);
frontNode->addChild(childNode);
}
}
return root;
}
class Pair{
public:
TreeNode<int>* maxcurr;
TreeNode<int>* smaxcurr;
};
Pair help(TreeNode<int>* root){
if(root==NULL){
Pair p;
p.maxcurr=NULL;
p.smaxcurr=NULL;
return p;
}
if(root->numChildren()==0){
Pair p;
p.maxcurr=root;
p.smaxcurr=NULL;
return p;
}
TreeNode <int>* maxcurr=root;
TreeNode <int>* smaxcurr=NULL;
for(int i=0;i<root->numChildren();i++){
Pair ans=help(root->getChild(i));
//Will have to work out many cases for maxcurrent and second maxcurrent
if(maxcurr->data >ans.maxcurr->data){
if(smaxcurr==NULL)
smaxcurr=ans.maxcurr;
else if(smaxcurr->data<ans.maxcurr->data)
smaxcurr=ans.maxcurr;
}
else{
TreeNode<int>* x=maxcurr;
maxcurr=ans.maxcurr;
if(ans.smaxcurr==NULL)
smaxcurr=x;
else if (x->data>ans.smaxcurr->data)
smaxcurr=x;
else
smaxcurr=ans.smaxcurr;
}
}
Pair p;
p.maxcurr=maxcurr;
p.smaxcurr=smaxcurr;
return p;
}
TreeNode <int>* secondLargest(TreeNode<int> *root) {
// Write your code here
Pair p=help(root);
return p.smaxcurr;
}
int main() {
TreeNode<int>* root = takeInputLevelWise();
TreeNode<int>* ans = secondLargest(root);
if(ans == NULL)
cout << INT_MIN << endl;
else
cout << ans -> data << endl;
}