# Algorithms_in_C++: operations_on_datastructures/inorder_successor_of_bst.cpp File Reference

Loading...
Searching...
No Matches
inorder_successor_of_bst.cpp File Reference

An implementation for finding the Inorder successor of a binary search tree Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal. More...

`#include <cassert>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for inorder_successor_of_bst.cpp:

## Classes

class | operations_on_datastructures::inorder_traversal_of_bst::Node
| A Node structure representing a single node in BST. More...
class | TestCases
| class encapsulating the necessary test cases More...

## Namespaces

namespace | operations_on_datastructures
| for std::vector
namespace | inorder_successor_of_bst
| Functions for the Inorder successor of a binary search tree implementation.

## Functions

Node * | operations_on_datastructures::inorder_traversal_of_bst::makeNode (int64_t data)
| Allocates a new node in heap for given data and returns it's pointer. More...
Node * | operations_on_datastructures::inorder_traversal_of_bst::Insert (Node *root, int64_t data)
| Inserts the given data in BST while maintaining the properties of BST. More...
Node * | operations_on_datastructures::inorder_traversal_of_bst::getNode (Node *root, int64_t data)
| Searches the given data in BST and returns the pointer to the node containing that data. More...
Node * | operations_on_datastructures::inorder_traversal_of_bst::findMinNode (Node *root)
| Finds and return the minimum node in BST. More...
void | operations_on_datastructures::inorder_traversal_of_bst::printInorder (Node *root)
| Prints the BST in inorder traversal using recursion. More...
Node * | operations_on_datastructures::inorder_traversal_of_bst::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 coding it in code. For a given root, this will add all the nodes containing data passes in data vector. More...
Node * | operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor (Node *root, int64_t data)
| Inorder successor of a node is the next node in inorder traversal of the Binary Tree. This function takes the root node and the data of the node for which we have to find the inorder successor, and returns the inorder successor node. More...
void | operations_on_datastructures::inorder_traversal_of_bst::deallocate (Node *rootNode)
| This function clears the memory allocated to entire tree recursively. Its just for clean up the memory and not relevant to the actual topic. More...
static void | test ()
| Self-test implementations. More...
int | main (int argc, char *argv[])
| Main function. More...

## Detailed Description

An implementation for finding the Inorder successor of a binary search tree Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.

### Case 1: The given node has the right node/subtree

     * In this case, the left-most deepest node in the right subtree will

come just after the given node as we go to left deep in inorder.

- Go deep to left most node in right subtree. OR, we can also say in case if BST, find the minimum of the subtree for a given node.

### Case 2: The given node does not have a right node/subtree

#### Method 1: Use parent pointer (store the address of parent nodes)

- If a node does not have the right subtree, and we already visited the node itself, then the next node will be its parent node according to inorder traversal, and if we are going to parent from left, then the parent would be unvisited.
- In other words, go to the nearest ancestor for which given node would be in left subtree.

#### Method 2: Search from the root node

- In case if there is no link from a child node to the parent node, we need to walk down the tree starting from the root node to the given node, by doing so, we are visiting every ancestor of the given node.
- In order successor would be the deepest node in this path for which given node is in left subtree.

Author
  Nitin Sharma

## Function Documentation

## deallocate()

void operations_on_datastructures::inorder_traversal_of_bst::deallocate | ( | Node * | rootNode | ) |

This function clears the memory allocated to entire tree recursively. Its just for clean up the memory and not relevant to the actual topic.

Parameters

  root | Root node of the tree.

Returns
  void

{
if (rootNode == nullptr) {
return;
}
deallocate(rootNode->left);
deallocate(rootNode->right);
delete (rootNode);
}
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
Here is the call graph for this function:

## findMinNode()

Node * operations_on_datastructures::inorder_traversal_of_bst::findMinNode | ( | Node * | root | ) |

Finds and return the minimum node in BST.

Parameters

  root | A pointer to root node.

Returns
  Node* Pointer to the found node

{
if (root == nullptr) {
return root;
}
while (root->left != nullptr) {
root = root->left;
}
return root;
}
Here is the call graph for this function:

## getInorderSuccessor()

Node * operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor | ( | Node * | root,
|  | int64_t | data
| ) |  |

Inorder successor of a node is the next node in inorder traversal of the Binary Tree. This function takes the root node and the data of the node for which we have to find the inorder successor, and returns the inorder successor node.

Search from the root node as we need to walk the tree starting from the root node to the given node, by doing so, we are visiting every ancestor of the given node. In order successor would be the deepest node in this path for which given node is in left subtree. Time complexity O(h)

Parameters

  root | A pointer to the root node of the BST
  data | The data (or the data of node) for which we have to find inorder successor.

Returns
  Node pointer to the inorder successor node.

{
Node *current = getNode(root, data);
if (current == nullptr) {
return nullptr;
}
// Case - 1
if (current->right != nullptr) {
return findMinNode(current->right);
}
// case - 2
else {
Node *successor = nullptr;
Node *ancestor = root;
while (ancestor != current && ancestor != nullptr) {
// This means my current node is in left of the root node
if (current->data < ancestor->data) {
successor = ancestor;
ancestor = ancestor->left; // keep going left
} else {
ancestor = ancestor->right;
}
}
return successor; // Nodes with maximum vales will not have a successor
}
}
data
int data[MAX]
test data
Definition: hash_search.cpp:24
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::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
Node
Definition: linkedlist_implentation_usingarray.cpp:14
Here is the call graph for this function:

## getNode()

Node * operations_on_datastructures::inorder_traversal_of_bst::getNode | ( | Node * | root,
|  | int64_t | data
| ) |  |

Searches the given data in BST and returns the pointer to the node containing that data.

Parameters

  root | Pointer to the root node of the BST
  data | Data to be Searched.

Returns
  Node* pointer to the found node

Node found!

Traverse right subtree recursively as the given data is greater than the data in root node, data must be present in right subtree.

Traverse left subtree recursively as the given data is less than the data in root node, data must be present in left subtree.

{
if (root == nullptr) {
return nullptr;
} else if (root->data == data) {
return root; /// Node found!
} else if (data > root->data) {
/// Traverse right subtree recursively as the given data is greater than
/// the data in root node, data must be present in right subtree.
return getNode(root->right, data);
} else {
/// Traverse left subtree recursively as the given data is less than the
/// data in root node, data must be present in left subtree.
return getNode(root->left, data);
}
}
std::vector::data
T data(T... args)
Here is the call graph for this function:

## Insert()

Node * operations_on_datastructures::inorder_traversal_of_bst::Insert | ( | Node * | root,
|  | int64_t | data
| ) |  |

Inserts the given data in BST while maintaining the properties of BST.

Parameters

  root | Pointer to the root node of the BST
  data | Data to be inserted.

Returns
  Node* Pointer to the root node.

{
if (root == nullptr) {
root = makeNode(data);
} else if (data <= root->data) {
root->left = Insert(root->left, data);
} else {
root->right = Insert(root->right, data);
}
return root;
}
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
Here is the call graph for this function:

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Main function.

Parameters

  argc | commandline argument count (ignored)
  argv | commandline array of arguments (ignored)

Returns
  0 on exit

< root node of the bst

< Data to add nodes in BST

< An element to find inorder successor for.

< Making BST

memory cleanup!

{
test(); // run self-test implementations
operations_on_datastructures::inorder_traversal_of_bst::Node *root =
nullptr; ///< root node of the bst
std::vector<int64_t> node_data{3, 4, 5,
89, 1, 2}; ///< Data to add nodes in BST
int64_t targetElement = 4; ///< An element to find inorder successor for.
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(
root, node_data); ///< Making BST
operations_on_datastructures::inorder_traversal_of_bst::Node
*inorderSuccessor = operations_on_datastructures::
inorder_traversal_of_bst::getInorderSuccessor(root, targetElement);
std::cout << "In-order sequence is : ";
operations_on_datastructures::inorder_traversal_of_bst::printInorder(root);
std::cout << std::endl;
if (inorderSuccessor == nullptr) {
std::cout << "Inorder successor for last node is NULL" << std::endl;
} else {
std::cout << "Target element is : " << targetElement << std::endl;
std::cout << "Inorder successor for target element is : "
<< inorderSuccessor->data << std::endl;
}
deallocate(root); /// memory cleanup!
return 0;
}
std::cout
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::data
int64_t data
The key/value of the node.
Definition: inorder_successor_of_bst.cpp:58
std::endl
T endl(T... args)
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::printInorder
void printInorder(Node *root)
Prints the BST in inorder traversal using recursion.
Definition: inorder_successor_of_bst.cpp:136
test
static void test()
Self-test implementations.
Definition: inorder_successor_of_bst.cpp:387
std::vector
Here is the call graph for this function:

## makeBST()

Node * operations_on_datastructures::inorder_traversal_of_bst::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 coding it in code. For a given root, this will add all the nodes containing data passes in data vector.

Parameters

  root | Pointer to the root node.
  data | A vector containing integer values which are suppose to be inserted as nodes in BST.

Returns
  Node pointer to the root node.

{
for (int64_t values : data) {
root = Insert(root, values);
}
return root;
}
Here is the call graph for this function:

## makeNode()

Node * operations_on_datastructures::inorder_traversal_of_bst::makeNode | ( | int64_t | data | ) |

Allocates a new node in heap for given data and returns it's pointer.

Parameters

  data | Data for the node.

Returns
  A pointer to the newly allocated Node.

< setting data for node

< setting left child as null

< setting right child as null

{
Node *node = new Node();
node->data = data; ///< setting data for node
node->left = nullptr; ///< setting left child as null
node->right = nullptr; ///< setting right child as null
return node;
}
node
Definition: avltree.cpp:13
Here is the call graph for this function:

## printInorder()

void operations_on_datastructures::inorder_traversal_of_bst::printInorder | ( | Node * | root | ) |

Prints the BST in inorder traversal using recursion.

Parameters

  root | A pointer to the root node of the BST.

Returns
  void

recursive call to left subtree

recursive call to right subtree

{
if (root == nullptr) {
return;
}
printInorder(root->left); /// recursive call to left subtree
std::cout << root->data << " ";
printInorder(root->right); /// recursive call to right subtree
}
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
TestCases tc;
tc.runTests();
}
TestCases
class encapsulating the necessary test cases
Definition: inorder_successor_of_bst.cpp:225
TestCases::runTests
void runTests()
Executes test cases.
Definition: inorder_successor_of_bst.cpp:243
Here is the call graph for this function: