-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: use smart pointer in binary search tree #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1. Memory Management: Used std::unique_ptr for automatic memory management, reducing the risk of memory leaks. 2. STL Utilization: Replaced the custom queue implementation with std::queue, simplifying breadth-first traversal. 3. Code Structure: Organized the code into a BinaryTree class, encapsulating the tree functionality and providing clearer interfaces for operations. 4. Redundancy Reduction: Combined logic for finding and removing nodes to reduce redundancy, improving readability and maintainability. 5. Error Handling: Added checks to handle cases where nodes may not be present during removal and traversal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contributions. There are several issues to be addressed please fix them.
#include <memory> | ||
#include <vector> | ||
#include <queue> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be inside the datastructures namespace
*/ | ||
#include <iostream> | ||
#include <memory> | ||
#include <vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
headers should be documented
|
||
private: | ||
// Helper function for insertion | ||
void insertHelper(Node* node, int x) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be inlined
root = std::make_unique<Node>(x); // Create root if it doesn't exist | ||
} else { | ||
Insert(n->left, x); | ||
insertHelper(root.get(), x); // Helper function for recursive insertion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably try to avoid recursion
|
||
// Remove a value from the tree | ||
void remove(int x) { | ||
removeHelper(root, x); // Helper function to manage removal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of removeHelper why not _remove()?
} | ||
|
||
// Postorder traversal | ||
void postOrderTraversal() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use snake_case
int main() { | ||
queue.front = 0; | ||
queue.rear = 0; | ||
BinaryTree tree; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests should be in a separate function called tests
Your code doesn't compile. The above review is about the minor issues. The major issue is the improper use of templated functions |
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions! |
Memory Management: Used std::unique_ptr for automatic memory management, reducing the risk of memory leaks.
STL Utilization: Replaced the custom queue implementation with std::queue, simplifying breadth-first traversal.
Code Structure: Organized the code into a BinaryTree class, encapsulating the tree functionality and providing clearer interfaces for operations.
Redundancy Reduction: Combined logic for finding and removing nodes to reduce redundancy, improving readability and maintainability.
Error Handling: Added checks to handle cases where nodes may not be present during removal and traversal.
Description of Change
Checklist
Notes: