0% found this document useful (0 votes)
5 views4 pages

C++ Retailer Product Management Code

The document contains C++ code for a Retailer class that manages a binary search tree of products, allowing for adding new products, searching for products, updating quantities, and finding products with the highest quantity. It includes methods for inserting nodes, searching for a product by ID, updating product quantities, and printing products with the maximum quantity. The main function demonstrates the functionality by adding products and displaying the maximum quantity product details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

C++ Retailer Product Management Code

The document contains C++ code for a Retailer class that manages a binary search tree of products, allowing for adding new products, searching for products, updating quantities, and finding products with the highest quantity. It includes methods for inserting nodes, searching for a product by ID, updating product quantities, and printing products with the maximum quantity. The main function demonstrates the functionality by adding products and displaying the maximum quantity product details.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 3 .

Code :

#include <iostream>
using namespace std ;
class Node {
public :
int product_id ;
int quantity ;
Node* left = nullptr ;
Node* right = nullptr ;

Node(int n , int b ){
product_id = n ;
quantity = b ;
}
};

class Retailer {

public :
Node* root = nullptr ;
bool add_a_new_product(int n , int b ){
Node* new_node = new Node(n , b ) ;
if (root == nullptr ){
root = new_node ;
}
else {
Node* curr = root ;
while (curr!= nullptr) {
if (curr->product_id == new_node->product_id){
return false ;
}
else if (new_node->product_id > curr->product_id &&
curr->right == nullptr){
curr->right = new_node ;
return true ;
}
else if (new_node->product_id > curr->product_id){
curr = curr->right ;
}
else if (new_node->product_id < curr->product_id &&
curr->left == nullptr){
curr->left = new_node ;
return true ;
}
else if (new_node->product_id < curr->product_id){
curr = curr->left ;
}
}
}
return false;
}

Node* search_product(int p){


Node* curr = root ;
while(!(curr->left != nullptr && curr->right != nullptr)){
if (curr->product_id == p){
return curr ;
}
else if (curr->product_id > p){
curr = curr->left ;
}

else if (curr->product_id < p){


curr = curr->right ;
}
}
return nullptr ;
}

bool update (int p , int q ){


Node* curr = root ;
while(!(curr->left == nullptr && curr->right != nullptr)){
if (curr->product_id == p){
curr->quantity = q ;
return true ;
}
else if (curr->product_id > p){
curr = curr->left ;
}

else if (curr->product_id < p){


curr = curr->right ;
}
}
return false ;
}
int find_highest_quantity(Node* curr , int& maximum){
if (curr->quantity > maximum ){
maximum = curr->quantity ;
}
if (curr->right != nullptr){
find_highest_quantity(curr->right,maximum) ;
}
if (curr->left != nullptr){
find_highest_quantity(curr->left,maximum) ;
}
return maximum ;
}
int n;
void Print_highest_quantity_products(Node* curr){
if (curr->quantity == n){
cout <<"Product ID : "<< curr->product_id << " Product
Quantity : " << curr->quantity<<endl;
}
if (curr->right != nullptr) {
Print_highest_quantity_products(curr->right) ;
}
if (curr->left != nullptr) {
Print_highest_quantity_products(curr->left) ;
}
}

} ;
int main(){
Retailer R;
R.add_a_new_product(1001,10);
R.add_a_new_product(2002,20);
R.add_a_new_product(3003,30);
R.add_a_new_product(4003,30);
R.add_a_new_product(5003,30);
R.search_product(2002);
R.find_highest_quantity([Link],R.n);
cout<<"Max is "<<R.n<<endl;
R.Print_highest_quantity_products([Link]);

}
Output :

You might also like