TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
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>
Go to the source code of this file.
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. | |
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. | |
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. | |
Node * | operations_on_datastructures::inorder_traversal_of_bst::findMinNode (Node *root) |
Finds and return the minimum node in BST. | |
void | operations_on_datastructures::inorder_traversal_of_bst::printInorder (Node *root) |
Prints the BST in inorder traversal using recursion. | |
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. | |
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. | |
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. | |
static void | test () |
Self-test implementations. | |
int | main (int argc, char *argv[]) |
Main function. | |
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.
* 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.
Definition in file inorder_successor_of_bst.cpp.
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.
root | Root node of the tree. |
Definition at line 210 of file inorder_successor_of_bst.cpp.
Finds and return the minimum node in BST.
root | A pointer to root node. |
Definition at line 121 of file inorder_successor_of_bst.cpp.
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)
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. |
Definition at line 176 of file inorder_successor_of_bst.cpp.
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.
root | Pointer to the root node of the BST |
data | Data to be Searched. |
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.
Definition at line 100 of file inorder_successor_of_bst.cpp.
Inserts the given data in BST while maintaining the properties of BST.
root | Pointer to the root node of the BST |
data | Data to be inserted. |
Definition at line 82 of file inorder_successor_of_bst.cpp.
int main | ( | int | argc, |
char * | argv[] ) |
Main function.
argc | commandline argument count (ignored) |
argv | commandline array of arguments (ignored) |
< root node of the bst
< Data to add nodes in BST
< An element to find inorder successor for.
< Making BST
memory cleanup!
Definition at line 398 of file inorder_successor_of_bst.cpp.
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.
root | Pointer to the root node. |
data | A vector containing integer values which are suppose to be inserted as nodes in BST. |
Definition at line 155 of file inorder_successor_of_bst.cpp.
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.
data | Data for the node. |
< setting data for node
< setting left child as null
< setting right child as null
Definition at line 68 of file inorder_successor_of_bst.cpp.
void operations_on_datastructures::inorder_traversal_of_bst::printInorder | ( | Node * | root | ) |
Prints the BST in inorder traversal using recursion.
root | A pointer to the root node of the BST. |
recursive call to left subtree
recursive call to right subtree
Definition at line 136 of file inorder_successor_of_bst.cpp.
|
static |
Self-test implementations.
Definition at line 387 of file inorder_successor_of_bst.cpp.