Data Lab 9
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;
return 0;
}
Q2
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left;
Node* right;
};
class BST
{
Node* root;
public:
BST() : root(nullptr) {}
return newNode;
}
~BST()
{
clear(root);
}
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;
}