TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
inorder_successor_of_bst.cpp
Go to the documentation of this file.
1
35
36
#include <cassert>
37
#include <iostream>
38
#include <vector>
39
44
namespace
operations_on_datastructures
{
45
51
namespace
inorder_traversal_of_bst {
52
56
class
Node
{
57
public
:
58
int64_t
data
;
59
Node
*
left
;
60
Node
*
right
;
61
};
62
68
Node
*
makeNode
(int64_t
data
) {
69
Node
*
node
=
new
Node
();
70
node
->data =
data
;
71
node
->left =
nullptr
;
72
node
->right =
nullptr
;
73
return
node
;
74
}
75
82
Node
*
Insert
(
Node
*root, int64_t
data
) {
83
if
(root ==
nullptr
) {
84
root =
makeNode
(
data
);
85
}
else
if
(
data <= root->
data
) {
86
root->left =
Insert
(root->left,
data
);
87
}
else
{
88
root->right =
Insert
(root->right,
data
);
89
}
90
return
root;
91
}
92
100
Node
*
getNode
(
Node
*root, int64_t
data
) {
101
if
(root ==
nullptr
) {
102
return
nullptr
;
103
}
else
if
(root->data ==
data
) {
104
return
root;
105
}
else
if
(
data
> root->data) {
108
return
getNode
(root->right,
data
);
109
}
else
{
112
return
getNode
(root->left,
data
);
113
}
114
}
115
121
Node
*
findMinNode
(
Node
*root) {
122
if
(root ==
nullptr
) {
123
return
root;
124
}
125
while
(root->left !=
nullptr
) {
126
root = root->left;
127
}
128
return
root;
129
}
130
136
void
printInorder
(
Node
*root) {
137
if
(root ==
nullptr
) {
138
return
;
139
}
140
141
printInorder
(root->left);
142
std::cout << root->data <<
" "
;
143
printInorder
(root->right);
144
}
145
155
Node
*
makeBST
(
Node
*root,
const
std::vector<int64_t> &
data
) {
156
for
(int64_t values :
data
) {
157
root =
Insert
(root, values);
158
}
159
return
root;
160
}
161
176
Node
*
getInorderSuccessor
(
Node
*root, int64_t
data
) {
177
Node
*current =
getNode
(root,
data
);
178
if
(current ==
nullptr
) {
179
return
nullptr
;
180
}
181
182
// Case - 1
183
if
(current->
right
!=
nullptr
) {
184
return
findMinNode
(current->
right
);
185
}
186
// case - 2
187
else
{
188
Node
*successor =
nullptr
;
189
Node
*ancestor = root;
190
191
while
(ancestor != current && ancestor !=
nullptr
) {
192
// This means my current node is in left of the root node
193
if
(current->
data
< ancestor->
data
) {
194
successor = ancestor;
195
ancestor = ancestor->
left
;
// keep going left
196
}
else
{
197
ancestor = ancestor->
right
;
198
}
199
}
200
return
successor;
// Nodes with maximum vales will not have a successor
201
}
202
}
203
210
void
deallocate
(
Node
*rootNode) {
211
if
(rootNode ==
nullptr
) {
212
return
;
213
}
214
deallocate
(rootNode->
left
);
215
deallocate
(rootNode->
right
);
216
delete
(rootNode);
217
}
218
219
}
// namespace inorder_traversal_of_bst
220
}
// namespace operations_on_datastructures
221
225
class
TestCases
{
226
private
:
232
template
<
typename
T>
233
void
log
(T msg) {
234
// It's just to avoid writing cout and endl
235
std::cout <<
"[TESTS] : ---> "
<< msg << std::endl;
236
}
237
238
public
:
243
void
runTests
() {
244
log
(
"Running Tests..."
);
245
246
testCase_1
();
247
testCase_2
();
248
testCase_3
();
249
250
log
(
"Test Cases over!"
);
251
std::cout << std::endl;
252
}
253
259
void
testCase_1
() {
260
const
operations_on_datastructures::inorder_traversal_of_bst::Node
261
*expectedOutput =
nullptr
;
262
263
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
264
log
(
"This is test case 1 : "
);
265
log
(
"Description:"
);
266
log
(
" EDGE CASE : Printing inorder successor for last node in the "
267
"BST, Output will be nullptr."
);
268
269
operations_on_datastructures::inorder_traversal_of_bst::Node
*root =
270
nullptr
;
271
std::vector<int64_t> node_data{
272
20, 3, 5, 6, 2, 23, 45, 78, 21};
273
274
root =
operations_on_datastructures::inorder_traversal_of_bst::makeBST
(
275
root,
276
node_data);
277
278
std::cout <<
"Inorder sequence is : "
;
279
operations_on_datastructures::inorder_traversal_of_bst::printInorder
(
280
root);
281
std::cout << std::endl;
282
283
operations_on_datastructures::inorder_traversal_of_bst::Node
284
*inorderSuccessor = operations_on_datastructures::
285
inorder_traversal_of_bst::getInorderSuccessor(
286
root, 78);
287
288
log
(
"Checking assert expression..."
);
289
assert(inorderSuccessor == expectedOutput);
290
log
(
"Assertion check passed!"
);
291
292
operations_on_datastructures::inorder_traversal_of_bst::deallocate
(
293
root);
294
295
log
(
"[PASS] : TEST CASE 1 PASS!"
);
296
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
297
}
298
304
void
testCase_2
() {
305
const
int
expectedOutput = 21;
306
307
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
308
log
(
"This is test case 2 : "
);
309
310
operations_on_datastructures::inorder_traversal_of_bst::Node
*root =
311
nullptr
;
312
std::vector<int64_t> node_data{
313
20, 3, 5, 6, 2, 23, 45, 78, 21};
314
315
root =
operations_on_datastructures::inorder_traversal_of_bst::makeBST
(
316
root,
317
node_data);
318
319
std::cout <<
"Inorder sequence is : "
;
320
operations_on_datastructures::inorder_traversal_of_bst::printInorder
(
321
root);
322
std::cout << std::endl;
323
324
operations_on_datastructures::inorder_traversal_of_bst::Node
325
*inorderSuccessor = operations_on_datastructures::
326
inorder_traversal_of_bst::getInorderSuccessor(
327
root, 20);
328
329
log
(
"Checking assert expression..."
);
330
assert(inorderSuccessor->
data
== expectedOutput);
331
log
(
"Assertion check passed!"
);
332
333
operations_on_datastructures::inorder_traversal_of_bst::deallocate
(
334
root);
335
336
log
(
"[PASS] : TEST CASE 2 PASS!"
);
337
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
338
}
339
345
void
testCase_3
() {
346
const
int
expectedOutput = 110;
347
348
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
349
log
(
"This is test case 3 : "
);
350
351
operations_on_datastructures::inorder_traversal_of_bst::Node
*root =
352
nullptr
;
353
std::vector<int64_t> node_data{
354
89, 67, 32, 56, 90, 123, 120,
355
110, 115, 6, 78, 7, 10};
356
357
root =
operations_on_datastructures::inorder_traversal_of_bst::makeBST
(
358
root,
359
node_data);
360
361
std::cout <<
"Inorder sequence is : "
;
362
operations_on_datastructures::inorder_traversal_of_bst::printInorder
(
363
root);
364
std::cout << std::endl;
365
366
operations_on_datastructures::inorder_traversal_of_bst::Node
367
*inorderSuccessor = operations_on_datastructures::
368
inorder_traversal_of_bst::getInorderSuccessor(
369
root, 90);
370
371
log
(
"Checking assert expression..."
);
372
assert(inorderSuccessor->
data
== expectedOutput);
373
log
(
"Assertion check passed!"
);
374
375
operations_on_datastructures::inorder_traversal_of_bst::deallocate
(
376
root);
377
378
log
(
"[PASS] : TEST CASE 3 PASS!"
);
379
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
380
}
381
};
382
387
static
void
test
() {
388
TestCases
tc;
389
tc.
runTests
();
390
}
391
396
int
main
() {
397
test
();
// run self-test implementations
398
399
operations_on_datastructures::inorder_traversal_of_bst::Node
*root =
400
nullptr
;
401
std::vector<int64_t> node_data{3, 4, 5,
402
89, 1, 2};
403
404
int64_t targetElement = 4;
405
root =
operations_on_datastructures::inorder_traversal_of_bst::makeBST
(
406
root, node_data);
407
408
operations_on_datastructures::inorder_traversal_of_bst::Node
409
*inorderSuccessor = operations_on_datastructures::
410
inorder_traversal_of_bst::getInorderSuccessor(root, targetElement);
411
412
std::cout <<
"In-order sequence is : "
;
413
operations_on_datastructures::inorder_traversal_of_bst::printInorder
(root);
414
std::cout << std::endl;
415
416
if
(inorderSuccessor ==
nullptr
) {
417
std::cout <<
"Inorder successor for last node is NULL"
<< std::endl;
418
}
else
{
419
std::cout <<
"Target element is : "
<< targetElement << std::endl;
420
std::cout <<
"Inorder successor for target element is : "
421
<< inorderSuccessor->
data
<< std::endl;
422
}
423
424
deallocate(root);
425
426
return
0;
427
}
test
void test()
Definition
caesar_cipher.cpp:100
TestCases
class encapsulating the necessary test cases
Definition
inorder_successor_of_bst.cpp:225
TestCases::log
void log(T msg)
A function to print given message on console.
Definition
inorder_successor_of_bst.cpp:233
TestCases::testCase_2
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
Definition
inorder_successor_of_bst.cpp:304
TestCases::testCase_1
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
Definition
inorder_successor_of_bst.cpp:259
TestCases::testCase_3
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
Definition
inorder_successor_of_bst.cpp:345
TestCases::runTests
void runTests()
Executes test cases.
Definition
inorder_successor_of_bst.cpp:243
operations_on_datastructures::inorder_traversal_of_bst::Node
A Node structure representing a single node in BST.
Definition
inorder_successor_of_bst.cpp:56
operations_on_datastructures::inorder_traversal_of_bst::Node::right
Node * right
Pointer to right child.
Definition
inorder_successor_of_bst.cpp:60
operations_on_datastructures::inorder_traversal_of_bst::Node::left
Node * left
Pointer to Left child.
Definition
inorder_successor_of_bst.cpp:59
operations_on_datastructures::inorder_traversal_of_bst::Node::data
int64_t data
The key/value of the node.
Definition
inorder_successor_of_bst.cpp:58
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data
int data[MAX]
test data
Definition
hash_search.cpp:24
operations_on_datastructures::inorder_traversal_of_bst::makeBST
Node * makeBST(Node *root, const std::vector< int64_t > &data)
This function is used in test cases to quickly create BST containing large data instead of hard codin...
Definition
inorder_successor_of_bst.cpp:155
operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor
Node * getInorderSuccessor(Node *root, int64_t data)
Inorder successor of a node is the next node in inorder traversal of the Binary Tree....
Definition
inorder_successor_of_bst.cpp:176
operations_on_datastructures::inorder_traversal_of_bst::Insert
Node * Insert(Node *root, int64_t data)
Inserts the given data in BST while maintaining the properties of BST.
Definition
inorder_successor_of_bst.cpp:82
operations_on_datastructures::inorder_traversal_of_bst::printInorder
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
Definition
inorder_successor_of_bst.cpp:136
operations_on_datastructures::inorder_traversal_of_bst::findMinNode
Node * findMinNode(Node *root)
Finds and return the minimum node in BST.
Definition
inorder_successor_of_bst.cpp:121
operations_on_datastructures::inorder_traversal_of_bst::deallocate
void deallocate(Node *rootNode)
This function clears the memory allocated to entire tree recursively. Its just for clean up the memor...
Definition
inorder_successor_of_bst.cpp:210
operations_on_datastructures::inorder_traversal_of_bst::makeNode
Node * makeNode(int64_t data)
Allocates a new node in heap for given data and returns it's pointer.
Definition
inorder_successor_of_bst.cpp:68
operations_on_datastructures::inorder_traversal_of_bst::getNode
Node * getNode(Node *root, int64_t data)
Searches the given data in BST and returns the pointer to the node containing that data.
Definition
inorder_successor_of_bst.cpp:100
operations_on_datastructures
for std::vector
node
Definition
binary_search_tree.cpp:11
operations_on_datastructures
inorder_successor_of_bst.cpp
Generated by
1.18.0