0% found this document useful (0 votes)
11 views8 pages

Data Lab 9

The document discusses binary search trees (BSTs) in C++. It includes struct definitions for nodes and products, a BST class with methods for insertion, searching, traversal and more. Main tests the BST by inserting nodes and calling methods to print traversals, search for nodes, and get tree properties.

Uploaded by

Rafay Naveed
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)
11 views8 pages

Data Lab 9

The document discusses binary search trees (BSTs) in C++. It includes struct definitions for nodes and products, a BST class with methods for insertion, searching, traversal and more. Main tests the BST by inserting nodes and calling methods to print traversals, search for nodes, and get tree properties.

Uploaded by

Rafay Naveed
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
You are on page 1/ 8

data lab 9

Abdur Rafay
[Company name] [Company address]
Q1
#include <iostream>
#include <string>
using namespace std;

struct Product
{
int id;
string name;
string category;
float price;
string person;
string cnic;

Product(int id, string name, string category, float price, string person, string cnic)
: id(id), name(name), category(category), price(price), person(person), cnic(cnic) {}
};
struct Node
{
Product product;
Node* left;
Node* right;

Node(Product product) : product(product), left(nullptr), right(nullptr) {}


};
class BST
{
Node* root;

Node* insert(Node* node, Product product)


{
if (node == nullptr)
{
return new Node(product);
}
if (product.price < node->product.price)
{
node->left = insert(node->left, product);
}
else
{
node->right = insert(node->right, product);
}
return node;
}
void displayBill(Node* node)
{
if (node != nullptr)
{
displayBill(node->left);
cout << "Product ID: " << node->product.id << endl;
cout << "Name: " << node->product.name << endl;
cout << "Category: " << node->product.category << endl;
cout << "Price: " << node->product.price << endl;
cout << "Person Name: " << node->product.person << endl;
cout << "CNIC: " << node->product.cnic << endl;
cout << "-------------------------" << endl;
displayBill(node->right);
}
}
void printTree(Node* node, int space)
{
if (node == nullptr)
{
return;
}
space += 10;
printTree(node->right, space);
cout << endl;
for (int i = 10; i < space; i++)
{
cout << " ";
}
cout << node->product.id << "(" << node->product.price << ")" << endl;
printTree(node->left, space);
}
Node* search(Node* node, int id)
{
if (node == nullptr || node->product.id == id)
{
return node;
}
if (node->product.id > id)
{
return search(node->left, id);
}
return search(node->right, id);
}
public:
BST() : root(nullptr) {}

void insert(Product product)


{
root = insert(root, product);
}
void displayBill()
{
displayBill(root);
}
void printTree()
{
if (root == nullptr)
{
cout << "Tree is empty" << endl;
return;
}
cout << "Tree:" << endl;
cout << "----" << endl;
printTree(root, 0);
}
Node* search(int id)
{
return search(root, id);
}
};
int main()
{
BST electronics, vehicles;

electronics.insert(Product(1, "Laptop", "Electronics", 1200.00, "rafay", "6383734939"));


electronics.insert(Product(2, "Smartphone", "Electronics", 800.00, "abdullah", "93749473937"));
electronics.insert(Product(3, "TV", "Electronics", 2000.00, "mohid", "938773938463"));

vehicles.insert(Product(4, "Car", "Vehicles", 20000.00, "emaan", "73635363538"));


vehicles.insert(Product(5, "Motorbike", "Vehicles", 3000.00, "anas", "8273539363"));
cout << "Electronics:" << endl;
electronics.displayBill();
electronics.printTree();

cout << endl << "Vehicles:" << endl;


vehicles.displayBill();
vehicles.printTree();

return 0;
}

Q2
#include <iostream>
using namespace std;

struct Node
{
int data;
Node* left;
Node* right;
};

class BST
{
Node* root;

public:
BST() : root(nullptr) {}

bool insert(int data)


{
if (root == nullptr)
{
root = new Node{ data, nullptr, nullptr };
return true;
}

Node* current = root;


while (true)
{
if (data == current->data)
return false;

if (data < current->data)


{
if (current->left == nullptr)
{
current->left = new Node{ data, nullptr, nullptr };
return true;
}
current = current->left;
}
else
{
if (current->right == nullptr)
{
current->right = new Node{ data, nullptr, nullptr };
return true;
}
current = current->right;
}
}
}

BST(const BST& other)


{
root = copy(other.root);
}

Node* copy(Node* node) const


{
if (node == nullptr)
return nullptr;

Node* newNode = new Node{ node->data, nullptr, nullptr };


newNode->left = copy(node->left);
newNode->right = copy(node->right);

return newNode;
}

void preorderPrint() const


{
preorderPrint(root);
cout << endl;
}

void preorderPrint(Node* node) const


{
if (node != nullptr)
{
cout << node->data << " ";
preorderPrint(node->left);
preorderPrint(node->right);
}
}

void inorderPrint() const


{
inorderPrint(root);
cout << endl;
}

void inorderPrint(Node* node) const


{
if (node != nullptr)
{
inorderPrint(node->left);
cout << node->data << " ";
inorderPrint(node->right);
}
}

void postorderPrint() const


{
postorderPrint(root);
cout << endl;
}

void postorderPrint(Node* node) const


{
if (node != nullptr)
{
postorderPrint(node->left);
postorderPrint(node->right);
cout << node->data << " ";
}
}

Node* search(int key) const


{
return search(root, key);
}

Node* search(Node* node, int key) const


{
if (node == nullptr || node->data == key)
return node;
if (key < node->data)
return search(node->left, key);

return search(node->right, key);


}

int length() const


{
return length(root);
}

int length(Node* node) const


{
if (node == nullptr)
return 0;

return 1 + length(node->left) + length(node->right);


}

int leafCount() const


{
return leafCount(root);
}

int leafCount(Node* node) const


{
if (node == nullptr)
return 0;

if (node->left == nullptr && node->right == nullptr)


return 1;

return leafCount(node->left) + leafCount(node->right);


}

~BST()
{
clear(root);
}

void clear(Node* node)


{
if (node == nullptr)
return;

clear(node->left);
clear(node->right);
delete node;
}
};

int main()
{
BST tree;
tree.insert(500);
tree.insert(1000);
tree.insert(1);
tree.insert(600);
tree.insert(700);
tree.insert(10);
tree.insert(30);
tree.insert(9000);
tree.insert(50000);
tree.insert(20);
cout << "Printing data using recursive inorder traversal: ";
tree.inorderPrint();
cout << "\nCOPY CONSTRUCTOR\n";
BST tree1(tree);
cout << "\n Preorder Traversal \n";
tree1.preorderPrint();
cout << "\n Inorder Traversal \n";
tree1.inorderPrint();
cout << "\n Postorder Traversal \n";
tree1.postorderPrint();
cout << "\n SEARCH: ";
Node* found = tree.search(1);
if (found != nullptr)
cout << "Found: " << found->data << endl;
else
cout << "Not found" << endl;
found = tree.search(30);
if (found != nullptr)
cout << "Found: " << found->data << endl;
else
cout << "Not found" << endl;
found = tree.search(50);
if (found != nullptr)
cout << "Found: " << found->data << endl;
else
cout << "Not found" << endl;
cout << "\n Tree Length: " << tree.length() << endl;
cout << "Tree Leaf Nodes: " << tree.leafCount() << endl;

return 0;
}

You might also like