forked from reachanihere/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextLargestNode.cpp
224 lines (180 loc) · 4.06 KB
/
NextLargestNode.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
/*
Given a generic tree and an integer n. Find and return the node with next larger element in the Tree i.e. find a node with value just greater than n.
Return NULL if no node is present with value greater than n.
Input format :
Line 1 : Integer n
Line 2 : 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
Output format : Node with value just greater than n.
Sample Input 1 :
18
10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 1 :
20
Sample Input 2 :
21
10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 2:
30
*/
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;
}
};
TreeNode<int>* nextLargerElement(TreeNode<int> *root, int n) {
// Write your code here
if(root==NULL)
return NULL;
if(root->numChildren()==0){
if(root->data>n)
return root;
else
return NULL;
}
TreeNode<int>* curr=NULL;
if(root->data>n)
curr=root;
for(int i=0;i<root->numChildren();i++){
TreeNode<int>* desc=nextLargerElement(root->getChild(i),n);
if(desc!=NULL){
if(curr==NULL)
curr=desc;
if(curr->data>desc->data)
curr=desc;
}
}
return curr;
}
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;
}
};
#include <iostream>
#include <climits>
using namespace std;
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;
}
int main() {
int n;
cin >> n;
TreeNode<int>* root = takeInputLevelWise();
TreeNode<int>* ans = nextLargerElement(root, n);
if(ans == NULL)
cout << INT_MIN << endl;
else
cout << ans -> data << endl;
}