I'm trying to insert a Packet object
into this binary search tree. But the problem is, I don't really know of a good-enough way of doing this or how to go about doing it. I'm looking for some pointers in the right direction and to be shown what to do to tackle this problem.
Please:
- Ignore my usage of namespace std; because this is for educational purposes and I'm not rly (as of now) looking to go further than that!
- Help me with my specific question and if possible, show me how I could fix this problem.
<< Take a look at my code >>
Main.cpp:
#include <iostream>
#include "BST.h"
#include "Packet.h"
// IGNORE the USAGE of namespace std. as this is purely a testing program for educational purposes.
// It is NOT implementation for a real program.
using namespace std;
int main() {
cout << "-------------------------------------------------------" << endl;
cout << "Testing BST" << endl;
cout << "-------------------------------------------------------" << endl;
BST test1;
Packet packetTest(123, "This is a packet of cheese.", 12.95, 10);
// test1.insert(How should I choose to insert Packet? That's the question.);
system("pause");
}
BST.h:
#pragma once
#include "Packet.h"
using namespace std;
class BST {
struct Node {
Node() : rlink(nullptr), llink(nullptr) {};
~Node() {};
// Store packet here (for instance Packet *data or something)...
Node *rlink, *llink;
};
public:
BST();
// void insert(How should I choose to insert Packet? That's the question.);
void insert(Node *&p, Node *newNode);
void preorderTraversal() const;
void destroyTree();
~BST();
private:
Node * root;
void destroyTree(Node *&p);
void preorderTraversal(const Node *p) const;
};
BST.cpp (need guidance here, see below code to see what I mean):
#include "BST.h"
#include <iostream>
BST::BST() : root(nullptr) {}
// Need guidance here. What should I do for this function? How can I insert this object called Packet into the BST?
/*void BST::insert(How should I choose to insert Packet? That's the question.) {
Node *newNode = new Node;
...
insert(root, newNode);
}*/
void BST::insert(Node *&p, Node *newNode) {
if (p == nullptr) {
p = newNode;
}/*else if (p's data's getPartId() > newNode's data's getPartId()){
insert(p->llink, newNode);
}*/else {
insert(p->rlink, newNode);
}
}
void BST::preorderTraversal() const {
if (root == nullptr) {
cerr << "There is no tree.";
}
else {
preorderTraversal(root);
}
}
void BST::preorderTraversal(const Node *p) const {
if (p != nullptr) {
// cout << p->data->getPartId() << " "; Need to handle Packet's data here. But we need to implement Packet insection first!
preorderTraversal(p->llink);
preorderTraversal(p->rlink);
}
}
void BST::destroyTree(Node *&p) {
if (p != nullptr) {
destroyTree(p->llink);
destroyTree(p->rlink);
delete p;
p = nullptr;
}
}
void BST::destroyTree() {
destroyTree(root);
}
BST::~BST() {
destroyTree(root);
}
Packet.h:
#pragma once
#include <string>
using namespace std;
class Packet {
public:
Packet(int partId, string description, double price, int partCount) :
partId(partId), description(description), price(price), partCount(partCount) {}
int getPartId() const { return partId; }
private:
int partId;
string description;
double price;
int partCount;
};
This was my previous implementation of insert in BST.cpp:
void BST::insert(Packet &data) {
Node *newNode = new Node;
newNode->data = &data;
insert(root, newNode);
}
As you can see, I don't believe that this is ideal. I mean I had to use & reference twice. Is there a more elegant solution and may I get guidance in regards to that?