-
Notifications
You must be signed in to change notification settings - Fork 82
BST #26
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
base: main
Are you sure you want to change the base?
BST #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,199 +12,128 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifndef BST_H | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define BST_H | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "node.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using namespace std; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class BST { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node<T> *root; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int _max(int a, int b) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _deleteTree(Node<T> *&root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _insert(Node<T> *&root, const T &item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _inOrder(Node<T> *root, std::ostream &out) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _preOrder(Node<T> *root, std::ostream &out) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _postOrder(Node<T> *root, std::ostream &out) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| T *_search(Node<T> *root, const T &item) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int _height(Node<T> *root) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Node<T> *_findMin(Node<T> *root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node<T>* root; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BST(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ~BST(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void destroy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void insert(const T &item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void inOrder(std::ostream &out = std::cout) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void preOrder(std::ostream &out = std::cout) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void postOrder(std::ostream &out = std::cout) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| T *search(const T &item) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int height() const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node<T>* insert(Node<T>* node, const T& value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node == nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Node<T>(value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BST<T>::BST() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| root = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(value < node->data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node->left = insert(node->left,value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BST<T>::~BST() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _deleteTree(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if(node->data < value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| node->right = insert(node->right,value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int BST<T>::_max(int a, int b) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (a > b) ? a : b; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Node<T>* searchNode(Node<T>* node,const T& key) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node == nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::destroy() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _deleteTree(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(key < node->data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return searchNode(node->left,key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node->data < key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return searchNode(node->right,key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::_deleteTree(Node<T> *&root) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _deleteTree(root->left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _deleteTree(root->right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete root; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| root = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void inOrder(Node<T>* node,ostream& out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::insert(const T &item) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _insert(root, item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node==nullptr) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inOrder(node->left,out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out<<node->data<<endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inOrder(node->right,out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::_insert(Node<T> *&root, const T &item) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root == nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| root = new Node<T>(item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (item < root->value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _insert(root->left, item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (item > root->value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _insert(root->right, item); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cerr << "\nError: duplicate value\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void preOrder(Node<T>* node) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node==nullptr) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::inOrder(std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _inOrder(root, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cout<<node->data<<endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preOrder(node->left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preOrder(node->right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void postOrder(Node<T>* node) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::_inOrder(Node<T> *root, std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _inOrder(root->left, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out << root->value << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _inOrder(root->right, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node==nullptr) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postOrder(node->left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postOrder(node->right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cout<<node->data<<endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int height(Node<T>* node) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::preOrder(std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _preOrder(root, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(node==nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int leftHeight = height(node->left); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int rightHeight = height(node->right); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::_preOrder(Node<T> *root, std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out << root->value << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _preOrder(root->left, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _preOrder(root->right, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(leftHeight > rightHeight) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return leftHeight+1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return rightHeight+1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::postOrder(std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _postOrder(root, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BST() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| root = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Missing destructor causes memory leak. The BST owns dynamically allocated 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void BST<T>::_postOrder(Node<T> *root, std::ostream &out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _postOrder(root->left, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _postOrder(root->right, out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out << root->value << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void insert(const T& value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| root = insert(root,value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+104
to
112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory leak risk: The Consider returning a pointer to the existing node's data (const if appropriate), or return 🔧 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 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void inOrder() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inOrder(root,cout); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int BST<T>::height() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _height(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void inOrder(ostream& out) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inOrder(root,out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void preOrder() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preOrder(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int BST<T>::_height(Node<T> *root) const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int result = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void postOrder() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postOrder(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (root != nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = 1 + _max(_height(root->left), _height(root->right)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int height() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return height(root); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -222,4 +151,4 @@ int BST<T>::_height(Node<T> *root) const { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //***************************************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
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