Skip to content

BST#26

Open
sushi286 wants to merge 4 commits intonragland37:mainfrom
sushi286:main
Open

BST#26
sushi286 wants to merge 4 commits intonragland37:mainfrom
sushi286:main

Conversation

@sushi286
Copy link

@sushi286 sushi286 commented Mar 5, 2026

Added BST implementation and stock class files

Summary by CodeRabbit

  • Refactor
    • Binary Search Tree interface updated with streamlined methods for insertion, searching, and tree traversal operations
    • Simplified internal data structures for improved consistency and maintainability

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

📝 Walkthrough

Walkthrough

Refactored BST header files to improve API clarity and consistency. The BST class now features a public constructor, insert, search, and traversal methods that delegate to private recursive helpers, replacing underscore-prefixed utility methods. Node constructor simplified to a single inline version. Stock class member variables renamed for consistency and method signatures updated to use standard naming conventions.

Changes

Cohort / File(s) Summary
BST Core Refactoring
data_structures-II/06-BST/bst.h
Introduced public API with constructor, insert(), search(), inOrder(), preOrder(), postOrder(), and height() methods. Added private recursive helpers and Node<T>* root member. Removed legacy underscore-prefixed methods (_insert, _search, _max, _deleteTree, etc.).
Node Structure Simplification
data_structures-II/06-BST/node.h
Consolidated multiple constructors into a single inline constructor Node(const T& value). Renamed data member from value to data. Removed default and two-parameter constructors.
Stock Data Model Updates
data_structures-II/06-BST/stock.h
Renamed private members (companyNamename, stockSymbolsymbol, stockPriceprice). Simplified constructor to Stock(string n="", string s="", double p=0), removed copy constructor. Updated accessor return types to string and moved operator<< friend declaration inside class. Modified comparison operator signatures.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A BST stands taller, with cleaner design,
Recursive helpers nested, all in a line,
Nodes simplified, names aligned with grace,
Stock members refactored to a brighter place!
Old helpers retired, new API takes flight,
The rabbit hops onward—the code feels so right! 🌟

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'BST' is overly vague and does not clearly convey the scope or nature of the changes, using a non-descriptive abbreviation without context. Use a more descriptive title such as 'Refactor BST implementation with updated API and Node structure' or 'Implement BST with simplified public interface and helper methods'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
data_structures-II/06-BST/bst.h (1)

136-154: ⚠️ Potential issue | 🔴 Critical

Duplicate #endif will cause compilation error.

Line 136 properly closes the BST_H include guard, but line 154 contains another #endif with no matching #if/#ifdef. This will result in a preprocessor error.

Also, the commented-out _findMin code (lines 139-150) should be removed if it's no longer needed, or moved to a separate location if it's planned for future use.

🐛 Proposed fix: Remove duplicate `#endif` and dead code
 };
 
 `#endif`
-//*****************************************************************************************************
-
-// template <typename T>
-// Node<T> *_findMin(Node<T> *root) {
-//     Node<T> *result = nullptr;
-
-//     if (root) {
-//         if (root->left == nullptr)
-//             result = root;
-//         else
-//             result = _findMin(root->left);
-//     }
-//     return result;
-// }
-
-//*****************************************************************************************************
-
-#endif
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/bst.h` around lines 136 - 154, Remove the stray
duplicate preprocessor terminator and dead commented code: delete the extra
trailing "#endif" that follows the include guard close for BST_H and either
remove or relocate the commented-out helper function _findMin (the template
function named _findMin and its block) so there is exactly one matching `#endif`
for the BST_H guard; ensure the file ends immediately after the intended
include-guard closure with no unmatched preprocessor directives.
🧹 Nitpick comments (3)
data_structures-II/06-BST/node.h (1)

16-20: Consider using member initializer list for efficiency.

Using a member initializer list is the idiomatic C++ approach and avoids default-constructing members before assignment, which can matter for non-trivial types.

♻️ Suggested refactor
-    Node(const T& value) {
-        data = value;
-        left = nullptr;
-        right = nullptr;
-    }
+    Node(const T& value) : data(value), left(nullptr), right(nullptr) {}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/node.h` around lines 16 - 20, The Node constructor
currently assigns members inside the body which can cause unnecessary default
construction; change the Node(const T& value) implementation to use a member
initializer list to initialize data, left, and right directly (i.e., initialize
data(value), left(nullptr), right(nullptr)) in the Node constructor of
class/struct Node<T> to improve efficiency and follow C++ idioms.
data_structures-II/06-BST/bst.h (1)

62-78: Inconsistent API: preOrder and postOrder hardcode cout.

The inOrder method provides an overload accepting std::ostream& (lines 118-120), allowing output to any stream. However, preOrder and postOrder hardcode std::cout, making them less flexible.

For consistency and testability, consider adding ostream& overloads for these methods as well.

♻️ Suggested refactor for consistent API
-    void preOrder(Node<T>* node) const {
+    void preOrder(Node<T>* node, std::ostream& out) const {
 
         if(node==nullptr) return;
 
-        cout<<node->data<<endl;
-        preOrder(node->left);
-        preOrder(node->right);
+        out << node->data << std::endl;
+        preOrder(node->left, out);
+        preOrder(node->right, out);
     }
 
-    void postOrder(Node<T>* node) const {
+    void postOrder(Node<T>* node, std::ostream& out) const {
 
         if(node==nullptr) return;
 
-        postOrder(node->left);
-        postOrder(node->right);
-        cout<<node->data<<endl;
+        postOrder(node->left, out);
+        postOrder(node->right, out);
+        out << node->data << std::endl;
     }

And update public methods:

     void preOrder() const {
-        preOrder(root);
+        preOrder(root, std::cout);
+    }
+
+    void preOrder(std::ostream& out) const {
+        preOrder(root, out);
     }
 
     void postOrder() const {
-        postOrder(root);
+        postOrder(root, std::cout);
+    }
+
+    void postOrder(std::ostream& out) const {
+        postOrder(root, out);
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/bst.h` around lines 62 - 78, preOrder and postOrder
currently write directly to std::cout which breaks the API consistency with
inOrder's ostream& overload; add overloads void preOrder(Node<T>* node,
std::ostream& out) const and void postOrder(Node<T>* node, std::ostream& out)
const that mirror the traversal logic but write to the provided out stream,
change the existing no-arg public preOrder/postOrder to forward to these
overloads with std::cout, and update any internal recursive calls inside the
overloads to pass the same out stream; reference the functions
preOrder(Node<T>*), postOrder(Node<T>*), and inOrder(Node<T>*)/inOrder(Node<T>*,
std::ostream&) when making the changes.
data_structures-II/06-BST/stock.h (1)

13-13: Avoid using namespace std; in headers.

Line 13 exports std into every translation unit including this header. Qualify types with std:: instead.

♻️ Suggested header change
-using namespace std;
@@
-    string name;
-    string symbol;
+    std::string name;
+    std::string symbol;
@@
-    Stock(string n="",string s="",double p=0);
+    Stock(std::string n="", std::string s="", double p=0);
@@
-    string getName() const;
-    string getSymbol() const;
+    std::string getName() const;
+    std::string getSymbol() const;
@@
-    friend ostream& operator<<(ostream& out,const Stock& s);
+    friend std::ostream& operator<<(std::ostream& out, const Stock& s);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/stock.h` at line 13, The header currently exposes
the global using directive "using namespace std;" which must be removed; delete
that line from stock.h and fully qualify all standard-library symbols used in
this header (e.g., replace string, vector, ostream, size_t, etc., with
std::string, std::vector, std::ostream, std::size_t) and update any function
signatures, member declarations, or typedefs in stock.h to use the std:: prefix
so the header no longer pollutes other translation units.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@data_structures-II/06-BST/bst.h`:
- Line 17: Remove the global "using namespace std;" declaration from the header
(the standalone using namespace std; line) and update all STD symbols declared
or referenced in this header to be fully qualified (e.g., change ostream,
string, cout, endl, etc. to std::ostream, std::string, std::cout, std::endl);
ensure any function signatures, class members, or friend operators in bst.h
(e.g., operator<< or any public APIs) use the std:: prefix so including this
header won't pollute the global namespace.
- Around line 94-98: BST currently lacks a destructor and Rule-of-Three/Five
safeguards, causing Node<T> heap leaks and potential shallow-copy issues;
implement a destructor ~BST that calls a recursive helper (e.g.,
deleteTree(Node<T>*)) to delete all nodes starting from root, and either define
or delete the copy constructor and copy-assignment operator (BST(const BST&),
BST& operator=(const BST&)) to prevent shallow copies; ensure deleteTree
traverses left and right children before deleting the node and set root to
nullptr after cleanup.
- Around line 104-112: The search method currently allocates a new T and returns
a raw pointer (search), causing potential leaks; change search's return to
return a pointer to the node's existing data instead of allocating (e.g., return
const T* or T* depending on mutability), directly return found->data (or
&found->data) and remove the new/delete requirement, and update the caller
usages and the declaration of search (and any overloads) accordingly; keep using
searchNode and Node<T> and still return nullptr when not found.

In `@data_structures-II/06-BST/stock.h`:
- Around line 19-25: The header's Stock class renamed members to name, symbol,
price and changed the constructor signature to Stock(string n="",string
s="",double p=0) but stock.cpp still references the old member names and
constructor signature; update stock.cpp to use the new member names (name,
symbol, price) and adjust any Stock(...) constructor calls/definition to match
the new three-parameter signature (or revert the header to the old
names/signature) so declarations and definitions match for class Stock, its
constructor, and any field accesses.

---

Outside diff comments:
In `@data_structures-II/06-BST/bst.h`:
- Around line 136-154: Remove the stray duplicate preprocessor terminator and
dead commented code: delete the extra trailing "#endif" that follows the include
guard close for BST_H and either remove or relocate the commented-out helper
function _findMin (the template function named _findMin and its block) so there
is exactly one matching `#endif` for the BST_H guard; ensure the file ends
immediately after the intended include-guard closure with no unmatched
preprocessor directives.

---

Nitpick comments:
In `@data_structures-II/06-BST/bst.h`:
- Around line 62-78: preOrder and postOrder currently write directly to
std::cout which breaks the API consistency with inOrder's ostream& overload; add
overloads void preOrder(Node<T>* node, std::ostream& out) const and void
postOrder(Node<T>* node, std::ostream& out) const that mirror the traversal
logic but write to the provided out stream, change the existing no-arg public
preOrder/postOrder to forward to these overloads with std::cout, and update any
internal recursive calls inside the overloads to pass the same out stream;
reference the functions preOrder(Node<T>*), postOrder(Node<T>*), and
inOrder(Node<T>*)/inOrder(Node<T>*, std::ostream&) when making the changes.

In `@data_structures-II/06-BST/node.h`:
- Around line 16-20: The Node constructor currently assigns members inside the
body which can cause unnecessary default construction; change the Node(const T&
value) implementation to use a member initializer list to initialize data, left,
and right directly (i.e., initialize data(value), left(nullptr), right(nullptr))
in the Node constructor of class/struct Node<T> to improve efficiency and follow
C++ idioms.

In `@data_structures-II/06-BST/stock.h`:
- Line 13: The header currently exposes the global using directive "using
namespace std;" which must be removed; delete that line from stock.h and fully
qualify all standard-library symbols used in this header (e.g., replace string,
vector, ostream, size_t, etc., with std::string, std::vector, std::ostream,
std::size_t) and update any function signatures, member declarations, or
typedefs in stock.h to use the std:: prefix so the header no longer pollutes
other translation units.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7edce57a-ca4d-417e-82bf-b481d9371946

📥 Commits

Reviewing files that changed from the base of the PR and between 52f6e37 and eb86f4c.

📒 Files selected for processing (3)
  • data_structures-II/06-BST/bst.h
  • data_structures-II/06-BST/node.h
  • data_structures-II/06-BST/stock.h

#include <iostream>

//*****************************************************************************************************
using namespace std;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Avoid using namespace std; in header files.

This directive pollutes the global namespace for every file that includes this header, potentially causing name collisions and making code harder to maintain.

🔧 Proposed fix
-using namespace std;

Then qualify standard library types explicitly (e.g., std::ostream, std::cout, std::endl).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/bst.h` at line 17, Remove the global "using
namespace std;" declaration from the header (the standalone using namespace std;
line) and update all STD symbols declared or referenced in this header to be
fully qualified (e.g., change ostream, string, cout, endl, etc. to std::ostream,
std::string, std::cout, std::endl); ensure any function signatures, class
members, or friend operators in bst.h (e.g., operator<< or any public APIs) use
the std:: prefix so including this header won't pollute the global namespace.

Comment on lines +94 to +98
public:

//*****************************************************************************************************
BST() {
root = nullptr;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Missing destructor causes memory leak.

The BST owns dynamically allocated Node<T> objects but has no destructor to free them. When a BST object goes out of scope (as shown in main.cpp at lines 32-51), all nodes remain allocated, causing a memory leak.

Additionally, this violates the Rule of Three/Five: if a class manages resources requiring custom destruction, it should also define (or delete) the copy constructor and copy assignment operator to prevent double-free or shallow-copy issues.

🐛 Proposed fix: Add destructor and delete tree helper
 private:
     Node<T>* root;
 
+    void deleteTree(Node<T>* node) {
+        if (node != nullptr) {
+            deleteTree(node->left);
+            deleteTree(node->right);
+            delete node;
+        }
+    }
+
     Node<T>* insert(Node<T>* node, const T& value) {
 public:
 
     BST() {
         root = nullptr;
     }
+
+    ~BST() {
+        deleteTree(root);
+    }
+
+    // Prevent shallow copies (or implement deep copy if needed)
+    BST(const BST&) = delete;
+    BST& operator=(const BST&) = delete;
 
     void insert(const T& value) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public:
//*****************************************************************************************************
BST() {
root = nullptr;
}
private:
Node<T>* root;
void deleteTree(Node<T>* node) {
if (node != nullptr) {
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
}
Node<T>* insert(Node<T>* node, const T& value) {
// ... rest of implementation
public:
BST() {
root = nullptr;
}
~BST() {
deleteTree(root);
}
// Prevent shallow copies (or implement deep copy if needed)
BST(const BST&) = delete;
BST& operator=(const BST&) = delete;
void insert(const T& value) {
// ... rest of implementation
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/bst.h` around lines 94 - 98, BST currently lacks a
destructor and Rule-of-Three/Five safeguards, causing Node<T> heap leaks and
potential shallow-copy issues; implement a destructor ~BST that calls a
recursive helper (e.g., deleteTree(Node<T>*)) to delete all nodes starting from
root, and either define or delete the copy constructor and copy-assignment
operator (BST(const BST&), BST& operator=(const BST&)) to prevent shallow
copies; ensure deleteTree traverses left and right children before deleting the
node and set root to nullptr after cleanup.

Comment on lines +104 to 112
T* search(const T& key) const {

template <typename T>
T *BST<T>::search(const T &item) const {
return _search(root, item);
}
Node<T>* found = searchNode(root,key);

//*****************************************************************************************************
if(found==nullptr)
return nullptr;

template <typename T>
T *BST<T>::_search(Node<T> *root, const T &item) const {
T *result = nullptr;

if (root != nullptr) {
if (item > root->value)
result = _search(root->right, item);
else if (item < root->value)
result = _search(root->left, item);
else
result = new T(root->value);
return new T(found->data);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Memory leak risk: search() returns heap-allocated copy.

The search method allocates a new T object on the heap (line 111) and returns a raw pointer. The caller must manually delete the result, which is undocumented and error-prone. If the caller forgets to delete, memory leaks occur.

Consider returning a pointer to the existing node's data (const if appropriate), or return nullptr when not found:

🔧 Proposed fix: Return pointer to existing data
     T* search(const T& key) const {
 
         Node<T>* found = searchNode(root,key);
 
         if(found==nullptr)
             return nullptr;
 
-        return new T(found->data);
+        return &(found->data);
     }

Note: If mutability is a concern, consider returning const T* instead.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
T* search(const T& key) const {
template <typename T>
T *BST<T>::search(const T &item) const {
return _search(root, item);
}
Node<T>* found = searchNode(root,key);
//*****************************************************************************************************
if(found==nullptr)
return nullptr;
template <typename T>
T *BST<T>::_search(Node<T> *root, const T &item) const {
T *result = nullptr;
if (root != nullptr) {
if (item > root->value)
result = _search(root->right, item);
else if (item < root->value)
result = _search(root->left, item);
else
result = new T(root->value);
return new T(found->data);
}
const T* search(const T& key) const {
Node<T>* found = searchNode(root,key);
if(found==nullptr)
return nullptr;
return &(found->data);
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/bst.h` around lines 104 - 112, The search method
currently allocates a new T and returns a raw pointer (search), causing
potential leaks; change search's return to return a pointer to the node's
existing data instead of allocating (e.g., return const T* or T* depending on
mutability), directly return found->data (or &found->data) and remove the
new/delete requirement, and update the caller usages and the declaration of
search (and any overloads) accordingly; keep using searchNode and Node<T> and
still return nullptr when not found.

Comment on lines +19 to +25
string name;
string symbol;
double price;

public:
Stock(const std::string &name = "", const std::string &symbol = "", double price = 0);
Stock(const Stock &s);
std::string getName() const;
std::string getSymbol() const;
double getPrice() const;
bool operator==(const Stock &rhs) const;
bool operator!=(const Stock &rhs) const;
bool operator>(const Stock &rhs) const;
bool operator<(const Stock &rhs) const;
};

//*****************************************************************************************************

inline std::string Stock::getName() const {
return companyName;
}

//*****************************************************************************************************

inline std::string Stock::getSymbol() const {
return stockSymbol;
}

//*****************************************************************************************************

inline double Stock::getPrice() const {
return stockPrice;
}

//*****************************************************************************************************

inline bool Stock::operator==(const Stock &rhs) const {
return (stockSymbol == rhs.stockSymbol);
}

//*****************************************************************************************************

inline bool Stock::operator!=(const Stock &rhs) const {
return (stockSymbol != rhs.stockSymbol);
}
Stock(string n="",string s="",double p=0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Header changes are out of sync with stock.cpp and will break compilation.

Line 19-Line 21 renamed members and Line 25 changed constructor declaration, but data_structures-II/06-BST/stock.cpp still uses old fields/signature (Line 15-Line 27 and Line 31-Line 37). This is a hard compile failure.

💡 Proposed sync fix
--- a/data_structures-II/06-BST/stock.cpp
+++ b/data_structures-II/06-BST/stock.cpp
@@
-Stock::Stock(const std::string &name, const std::string &symbol, double price) {
-    companyName = name;
-    stockSymbol = symbol;
-    stockPrice = price;
-}
+Stock::Stock(std::string n, std::string s, double p) {
+    name = n;
+    symbol = s;
+    price = p;
+}
@@
-Stock::Stock(const Stock &s) {
-    companyName = s.companyName;
-    stockSymbol = s.stockSymbol;
-    stockPrice = s.stockPrice;
-}
+// Remove manual copy-constructor block if relying on compiler-generated copy ctor.
@@
 std::ostream &operator<<(std::ostream &out, const Stock &stock) {
-    out << stock.companyName << std::endl
-        << stock.stockSymbol << std::endl
-        << stock.stockPrice;
+    out << stock.name << std::endl
+        << stock.symbol << std::endl
+        << stock.price;
 
     return out;
 }

Also applies to: 34-34

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@data_structures-II/06-BST/stock.h` around lines 19 - 25, The header's Stock
class renamed members to name, symbol, price and changed the constructor
signature to Stock(string n="",string s="",double p=0) but stock.cpp still
references the old member names and constructor signature; update stock.cpp to
use the new member names (name, symbol, price) and adjust any Stock(...)
constructor calls/definition to match the new three-parameter signature (or
revert the header to the old names/signature) so declarations and definitions
match for class Stock, its constructor, and any field accesses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant