From 920d150bdd52e674fd123cc30872334387fb7df7 Mon Sep 17 00:00:00 2001 From: Khushii <155296248+sushi286@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:12:22 +0530 Subject: [PATCH 1/4] Update node.h --- data_structures-II/06-BST/node.h | 39 ++++++++------------------------ 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/data_structures-II/06-BST/node.h b/data_structures-II/06-BST/node.h index 754e481..568c583 100644 --- a/data_structures-II/06-BST/node.h +++ b/data_structures-II/06-BST/node.h @@ -7,36 +7,17 @@ #ifndef NODE_H #define NODE_H -//***************************************************************************************************** - template struct Node { - T value; - Node *left; - Node *right; - - Node(); - Node(const T &v, Node *l = nullptr, Node *r = nullptr); + T data; + Node* left; + Node* right; + + Node(const T& value) { + data = value; + left = nullptr; + right = nullptr; + } }; -//***************************************************************************************************** - -template -Node::Node() { - value = T(); // T() - default initialization (0 for numbers, empty string for strings, etc.) - left = nullptr; - right = nullptr; -} - -//***************************************************************************************************** - -template -Node::Node(const T &v, Node *l, Node *r) { - value = v; - left = l; - right = r; -} - -//***************************************************************************************************** - -#endif \ No newline at end of file +#endif From 9b562e5196954ba8640b9c02c05c9dfea529f570 Mon Sep 17 00:00:00 2001 From: Khushii <155296248+sushi286@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:13:21 +0530 Subject: [PATCH 2/4] Update bst.h --- data_structures-II/06-BST/bst.h | 223 +++++++++++--------------------- 1 file changed, 76 insertions(+), 147 deletions(-) diff --git a/data_structures-II/06-BST/bst.h b/data_structures-II/06-BST/bst.h index 2bc7f57..141d560 100644 --- a/data_structures-II/06-BST/bst.h +++ b/data_structures-II/06-BST/bst.h @@ -12,199 +12,128 @@ #ifndef BST_H #define BST_H -//***************************************************************************************************** - #include "node.h" #include - -//***************************************************************************************************** +using namespace std; template class BST { + private: - Node *root; - - int _max(int a, int b) const; - void _deleteTree(Node *&root); - void _insert(Node *&root, const T &item); - void _inOrder(Node *root, std::ostream &out) const; - void _preOrder(Node *root, std::ostream &out) const; - void _postOrder(Node *root, std::ostream &out) const; - T *_search(Node *root, const T &item) const; - int _height(Node *root) const; - // Node *_findMin(Node *root); + Node* 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* insert(Node* node, const T& value) { -//***************************************************************************************************** + if(node == nullptr) + return new Node(value); -template -BST::BST() { - root = nullptr; -} - -//***************************************************************************************************** + if(value < node->data) + node->left = insert(node->left,value); -template -BST::~BST() { - _deleteTree(root); -} + else if(node->data < value) + node->right = insert(node->right,value); -//***************************************************************************************************** + return node; + } -template -int BST::_max(int a, int b) const { - return (a > b) ? a : b; -} + Node* searchNode(Node* node,const T& key) const { -//***************************************************************************************************** + if(node == nullptr) + return nullptr; -template -void BST::destroy() { - _deleteTree(root); -} + if(key < node->data) + return searchNode(node->left,key); -//***************************************************************************************************** + if(node->data < key) + return searchNode(node->right,key); -template -void BST::_deleteTree(Node *&root) { - if (root != nullptr) { - _deleteTree(root->left); - _deleteTree(root->right); - delete root; - root = nullptr; + return node; } -} -//***************************************************************************************************** + void inOrder(Node* node,ostream& out) const { -template -void BST::insert(const T &item) { - _insert(root, item); -} + if(node==nullptr) return; -//***************************************************************************************************** + inOrder(node->left,out); + out<data<right,out); + } -template -void BST::_insert(Node *&root, const T &item) { - if (root == nullptr) - root = new Node(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* node) const { -//***************************************************************************************************** + if(node==nullptr) return; -template -void BST::inOrder(std::ostream &out) const { - _inOrder(root, out); -} + cout<data<left); + preOrder(node->right); + } -//***************************************************************************************************** + void postOrder(Node* node) const { -template -void BST::_inOrder(Node *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<data<* node) const { -template -void BST::preOrder(std::ostream &out) const { - _preOrder(root, out); -} + if(node==nullptr) + return 0; -//***************************************************************************************************** + int leftHeight = height(node->left); + int rightHeight = height(node->right); -template -void BST::_preOrder(Node *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 -void BST::postOrder(std::ostream &out) const { - _postOrder(root, out); -} +public: -//***************************************************************************************************** + BST() { + root = nullptr; + } -template -void BST::_postOrder(Node *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 -T *BST::search(const T &item) const { - return _search(root, item); -} + Node* found = searchNode(root,key); -//***************************************************************************************************** + if(found==nullptr) + return nullptr; -template -T *BST::_search(Node *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); } - return result; -} - -//***************************************************************************************************** + void inOrder() const { + inOrder(root,cout); + } -template -int BST::height() const { - return _height(root); -} + void inOrder(ostream& out) const { + inOrder(root,out); + } -//***************************************************************************************************** + void preOrder() const { + preOrder(root); + } -template -int BST::_height(Node *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 @@ -222,4 +151,4 @@ int BST::_height(Node *root) const { //***************************************************************************************************** -#endif \ No newline at end of file +#endif From 51f568715ee02f79c7d1fe5af14c754aa26d8a8c Mon Sep 17 00:00:00 2001 From: Khushii <155296248+sushi286@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:13:58 +0530 Subject: [PATCH 3/4] Update stock.h --- data_structures-II/06-BST/stock.h | 74 +++++++------------------------ 1 file changed, 15 insertions(+), 59 deletions(-) diff --git a/data_structures-II/06-BST/stock.h b/data_structures-II/06-BST/stock.h index 3915d73..f7355e7 100755 --- a/data_structures-II/06-BST/stock.h +++ b/data_structures-II/06-BST/stock.h @@ -8,74 +8,30 @@ #ifndef STOCK_H #define STOCK_H -//***************************************************************************************************** - +#include #include - -//***************************************************************************************************** +using namespace std; class Stock { + private: - std::string companyName; - std::string stockSymbol; - double stockPrice; - friend std::ostream &operator<<(std::ostream &out, const Stock &stock); + 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); -//***************************************************************************************************** - -inline bool Stock::operator>(const Stock &rhs) const { - return (stockSymbol > rhs.stockSymbol); -} - -//***************************************************************************************************** + string getName() const; + string getSymbol() const; + double getPrice() const; -inline bool Stock::operator<(const Stock &rhs) const { - return (stockSymbol < rhs.stockSymbol); -} + bool operator<(const Stock& other) const; + bool operator==(const Stock& other) const; -//***************************************************************************************************** + friend ostream& operator<<(ostream& out,const Stock& s); +}; -#endif \ No newline at end of file +#endif From eb86f4cf66f5624e4c1b742e49f34edafb1695c8 Mon Sep 17 00:00:00 2001 From: Khushii <155296248+sushi286@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:15:07 +0530 Subject: [PATCH 4/4] Update Stock.txt