51namespace inorder_traversal_of_bst {
72 node->right =
nullptr;
83 if (root ==
nullptr) {
86 root->left = Insert(root->left,
data);
88 root->right = Insert(root->right,
data);
101 if (root ==
nullptr) {
103 }
else if (root->data ==
data) {
105 }
else if (
data > root->data) {
122 if (root ==
nullptr) {
125 while (root->left !=
nullptr) {
137 if (root ==
nullptr) {
142 std::cout << root->data <<
" ";
156 for (int64_t values :
data) {
157 root = Insert(root, values);
178 if (current ==
nullptr) {
183 if (current->
right !=
nullptr) {
188 Node *successor =
nullptr;
189 Node *ancestor = root;
191 while (ancestor != current && ancestor !=
nullptr) {
193 if (current->
data < ancestor->
data) {
194 successor = ancestor;
195 ancestor = ancestor->
left;
197 ancestor = ancestor->
right;
211 if (rootNode ==
nullptr) {
232 template <
typename T>
235 std::cout <<
"[TESTS] : ---> " << msg << std::endl;
244 log(
"Running Tests...");
250 log(
"Test Cases over!");
251 std::cout << std::endl;
261 *expectedOutput =
nullptr;
263 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
264 log(
"This is test case 1 : ");
266 log(
" EDGE CASE : Printing inorder successor for last node in the "
267 "BST, Output will be nullptr.");
271 std::vector<int64_t> node_data{
272 20, 3, 5, 6, 2, 23, 45, 78, 21};
274 root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
278 std::cout <<
"Inorder sequence is : ";
279 operations_on_datastructures::inorder_traversal_of_bst::printInorder(
281 std::cout << std::endl;
284 *inorderSuccessor = operations_on_datastructures::
285 inorder_traversal_of_bst::getInorderSuccessor(
288 log(
"Checking assert expression...");
289 assert(inorderSuccessor == expectedOutput);
290 log(
"Assertion check passed!");
292 operations_on_datastructures::inorder_traversal_of_bst::deallocate(
295 log(
"[PASS] : TEST CASE 1 PASS!");
296 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
305 const int expectedOutput = 21;
307 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
308 log(
"This is test case 2 : ");
312 std::vector<int64_t> node_data{
313 20, 3, 5, 6, 2, 23, 45, 78, 21};
315 root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
319 std::cout <<
"Inorder sequence is : ";
320 operations_on_datastructures::inorder_traversal_of_bst::printInorder(
322 std::cout << std::endl;
325 *inorderSuccessor = operations_on_datastructures::
326 inorder_traversal_of_bst::getInorderSuccessor(
329 log(
"Checking assert expression...");
330 assert(inorderSuccessor->
data == expectedOutput);
331 log(
"Assertion check passed!");
333 operations_on_datastructures::inorder_traversal_of_bst::deallocate(
336 log(
"[PASS] : TEST CASE 2 PASS!");
337 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
346 const int expectedOutput = 110;
348 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
349 log(
"This is test case 3 : ");
353 std::vector<int64_t> node_data{
354 89, 67, 32, 56, 90, 123, 120,
355 110, 115, 6, 78, 7, 10};
357 root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
361 std::cout <<
"Inorder sequence is : ";
362 operations_on_datastructures::inorder_traversal_of_bst::printInorder(
364 std::cout << std::endl;
367 *inorderSuccessor = operations_on_datastructures::
368 inorder_traversal_of_bst::getInorderSuccessor(
371 log(
"Checking assert expression...");
372 assert(inorderSuccessor->
data == expectedOutput);
373 log(
"Assertion check passed!");
375 operations_on_datastructures::inorder_traversal_of_bst::deallocate(
378 log(
"[PASS] : TEST CASE 3 PASS!");
379 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
398int main(
int argc,
char *argv[]) {
403 std::vector<int64_t> node_data{3, 4, 5,
406 int64_t targetElement = 4;
407 root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
411 *inorderSuccessor = operations_on_datastructures::
412 inorder_traversal_of_bst::getInorderSuccessor(root, targetElement);
414 std::cout <<
"In-order sequence is : ";
415 operations_on_datastructures::inorder_traversal_of_bst::printInorder(root);
416 std::cout << std::endl;
418 if (inorderSuccessor ==
nullptr) {
419 std::cout <<
"Inorder successor for last node is NULL" << std::endl;
421 std::cout <<
"Target element is : " << targetElement << std::endl;
422 std::cout <<
"Inorder successor for target element is : "
423 << inorderSuccessor->
data << std::endl;
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
class encapsulating the necessary test cases
void log(T msg)
A function to print given message on console.
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
void runTests()
Executes test cases.
A Node structure representing a single node in BST.
Node * right
Pointer to right child.
Node * left
Pointer to Left child.
int64_t data
The key/value of the node.
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...
Node * getInorderSuccessor(Node *root, int64_t data)
Inorder successor of a node is the next node in inorder traversal of the Binary Tree....
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
Node * findMinNode(Node *root)
Finds and return the minimum node in BST.
void deallocate(Node *rootNode)
This function clears the memory allocated to entire tree recursively. Its just for clean up the memor...
Node * makeNode(int64_t data)
Allocates a new node in heap for given data and returns it's pointer.
Node * getNode(Node *root, int64_t data)
Searches the given data in BST and returns the pointer to the node containing that data.
static void test()
Self-test implementations.