inorder_traversal_of_bst {
71 node->left =
nullptr;
72 node->right =
nullptr;
83 if(root ==
nullptr) {
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) {
142std::cout << root->data <<
" ";
156 for(int64_t values :
data) {
157root =
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) {
194successor = ancestor;
195ancestor = ancestor->
left;
197ancestor = ancestor->
right;
211 if(rootNode ==
nullptr) {
232 template<
typenameT>
235std::cout <<
"[TESTS] : ---> "<< msg << std::endl;
244 log(
"Running Tests...");
250 log(
"Test Cases over!");
251std::cout << std::endl;
261*expectedOutput =
nullptr;
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.");
271std::vector<int64_t> node_data{
27220, 3, 5, 6, 2, 23, 45, 78, 21};
278std::cout <<
"Inorder sequence is : ";
281std::cout << std::endl;
284*inorderSuccessor = operations_on_datastructures::
285inorder_traversal_of_bst::getInorderSuccessor(
288 log(
"Checking assert expression...");
289assert(inorderSuccessor == expectedOutput);
290 log(
"Assertion check passed!");
295 log(
"[PASS] : TEST CASE 1 PASS!");
296 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
305 const intexpectedOutput = 21;
307 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
308 log(
"This is test case 2 : ");
312std::vector<int64_t> node_data{
31320, 3, 5, 6, 2, 23, 45, 78, 21};
319std::cout <<
"Inorder sequence is : ";
322std::cout << std::endl;
325*inorderSuccessor = operations_on_datastructures::
326inorder_traversal_of_bst::getInorderSuccessor(
329 log(
"Checking assert expression...");
330assert(inorderSuccessor->
data== expectedOutput);
331 log(
"Assertion check passed!");
336 log(
"[PASS] : TEST CASE 2 PASS!");
337 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
346 const intexpectedOutput = 110;
348 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
349 log(
"This is test case 3 : ");
353std::vector<int64_t> node_data{
35489, 67, 32, 56, 90, 123, 120,
355110, 115, 6, 78, 7, 10};
361std::cout <<
"Inorder sequence is : ";
364std::cout << std::endl;
367*inorderSuccessor = operations_on_datastructures::
368inorder_traversal_of_bst::getInorderSuccessor(
371 log(
"Checking assert expression...");
372assert(inorderSuccessor->
data== expectedOutput);
373 log(
"Assertion check passed!");
378 log(
"[PASS] : TEST CASE 3 PASS!");
379 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
401std::vector<int64_t> node_data{3, 4, 5,
404int64_t targetElement = 4;
409*inorderSuccessor = operations_on_datastructures::
410inorder_traversal_of_bst::getInorderSuccessor(root, targetElement);
412std::cout <<
"In-order sequence is : ";
414std::cout << std::endl;
416 if(inorderSuccessor ==
nullptr) {
417std::cout <<
"Inorder successor for last node is NULL"<< std::endl;
419std::cout <<
"Target element is : "<< targetElement << std::endl;
420std::cout <<
"Inorder successor for target element is : " 421<< inorderSuccessor->
data<< std::endl;
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....
Node * Insert(Node *root, int64_t data)
Inserts the given data in BST while maintaining the properties of BST.
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.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4