Skip to content

Commit f8658fe

Browse files
committed
clang-tidy errors fixed, reference docs link added
1 parent a527532 commit f8658fe

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

operations_on_datastructures/inorder_successor_of_bst.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* @brief An implementation for finding [Inorder successor of binary search tree](link)
3+
* @brief An implementation for finding [Inorder successor of binary search tree](https://www.youtube.com/watch?v=5cPbNCrdotA&t=904s)
44
* 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.
55
*
66
* ### Case 1 : The given node has right node/subtree
@@ -112,13 +112,13 @@ namespace binary_search_tree {
112112
* @brief Search from the root node as we need to walk the tree starting from the root node to the given node,
113113
* by doing so, we are visiting every ancestor of the given node.
114114
* In order successor would be the deepest node in this path for which given node is in left subtree.
115+
* Time complexity O(h)
115116
*
116117
* @param root A pointer to the root node of the BST
117118
* @param data The data (or the data of node) for which we have to find inorder successor.
118119
* @returns Node pointer to the inorder successor node.
119120
* */
120121
Node *getInorderSuccessor(Node *root, int64_t data) {
121-
// O(h)
122122

123123
Node *current = getNode(root, data);
124124
if (current == nullptr) return nullptr;
@@ -132,13 +132,14 @@ namespace binary_search_tree {
132132
Node *successor = nullptr;
133133
Node *ancestor = root;
134134

135-
while (ancestor != current) {
135+
while (ancestor != current && ancestor != nullptr) {
136136

137+
// This means my current node is in left of the root node
137138
if (current->data < ancestor->data) {
138-
// This means my current node is in left of the root node
139139
successor = ancestor;
140140
ancestor = ancestor->left; // keep going left
141-
} else {
141+
}
142+
else {
142143
ancestor = ancestor->right;
143144
}
144145
}
@@ -173,7 +174,7 @@ namespace binary_search_tree {
173174
}
174175
return root;
175176
}
176-
} // namespace binary_search_tree
177+
} // namespace binary_search_tree
177178

178179
/**
179180
* @brief class encapsulating the necessary test cases
@@ -235,6 +236,9 @@ class TestCases {
235236
assert(inorderSuccessor == expectedOutput);
236237
log("Assertion check passed!");
237238

239+
delete(inorderSuccessor);
240+
delete(root);
241+
238242
log("[PASS] : TEST CASE 1 PASS!");
239243
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
240244
}
@@ -266,6 +270,9 @@ class TestCases {
266270
assert(inorderSuccessor->data == expectedOutput);
267271
log("Assertion check passed!");
268272

273+
delete(inorderSuccessor);
274+
delete(root);
275+
269276
log("[PASS] : TEST CASE 2 PASS!");
270277
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
271278
}
@@ -297,6 +304,9 @@ class TestCases {
297304
assert(inorderSuccessor->data == expectedOutput);
298305
log("Assertion check passed!");
299306

307+
delete(inorderSuccessor);
308+
delete(root);
309+
300310
log("[PASS] : TEST CASE 3 PASS!");
301311
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
302312
}
@@ -341,5 +351,8 @@ int main(int argc, char *argv[]) {
341351
std::cout<<"Inorder successor for target element is : "<<inorderSuccessor->data;
342352
}
343353

354+
delete(inorderSuccessor);
355+
delete(root);
356+
344357
return 0;
345-
}
358+
}

0 commit comments

Comments
 (0)