Objective:
I'm trying to create a BST in order to insert Packet objects for search purposes. Packet holds information like partId
, description
, price
, and partCount
.
This is a follow up question of this post. I have since revised it so that:
- No or minimal memory leaks
- Includes minimized
- Namespace usage is minimized or removed altogether
Now I want to ask some more questions:
As you can see here,
void BST::insert(Packet &data) {
I have to do this within the function,
newNode->data = &data;
Is there a way to make that a more elegant solution or improve it? Because as you can see I'm using reference twice and I'd like to fix that or improve it. Could I get an example or be shown how this could be fixed?
In an answer to the other post, it was said:
Node isn't part of the public interface. If we make it a private struct within BST, then BST gets full access (not needing a friend declaration), but no other code does. That's what we really want.
I have never done this before. Could I be shown an example or directly?
Main.cpp:
#include <iostream>
#include "BST.h"
#include "Packet.h"
using namespace std;
int main() {
BST test1;
cout << "-------------------------------------------------------" << endl;
cout << "Testing BST" << endl;
cout << "-------------------------------------------------------" << endl;
Packet packetTest(123, "This is a packet of cheese.", 12.95, 10);
Packet packetTest1(321, "This is a packet of cheese.", 12.95, 10);
Packet packetTest2(456, "This is a packet of cheese.", 12.95, 10);
Packet packetTest3(7, "This is a packet of cheese.", 12.95, 10);
Packet packetTest4(119, "This is a packet of cheese.", 12.95, 10);
Packet packetTest5(456, "This is a packet of cheese.", 12.95, 10);
test1.insert(packetTest);
test1.insert(packetTest1);
test1.insert(packetTest2);
test1.insert(packetTest3);
test1.insert(packetTest4);
test1.insert(packetTest5);
test1.preorderTraversal();
system("pause");
}
BST.h:
#pragma once
#include "Packet.h"
class Node
{
friend class BST;
public:
Node() : data(nullptr), rlink(nullptr), llink(nullptr) {}
~Node() {}
private:
Packet *data;
Node *rlink, *llink;
};
class BST {
public:
BST();
void BST::insert(Packet &data);
void BST::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:
#include "BST.h"
#include <iostream>
BST::BST() : root(nullptr) {}
void BST::insert(Packet &data) {
Node *newNode = new Node;
newNode->data = &data;
insert(root, newNode);
}
void BST::insert(Node *&p, Node *newNode)
{
if (p == nullptr)
p = newNode;
else if (p->data->getPartId() > newNode->data->getPartId())
insert(p->llink, newNode);
else
insert(p->rlink, newNode);
}
void BST::preorderTraversal() const {
if (root == nullptr)
std::cerr << "There is no tree.";
else
{
preorderTraversal(root);
}
}
void BST::preorderTraversal(const Node *p) const {
if (p != nullptr) {
std::cout << p->data->getPartId() << " ";
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>
class Packet {
public:
Packet(int partId, std::string description, double price, int partCount) :
partId(partId), description(description), price(price), partCount(partCount) {}
int getPartId() const {return partId;}
private:
int partId;
std::string description;
double price;
int partCount;
};
using namespace std;
which both answers strictly advised to avoid. Apart from that, the code as presented here still won't compile because of the extra qualifierBST::
oninsert
inBST.h
. AlsoBST.cpp
does not know where to look forcout
andcerr
since you droppedusing namespace std;
but did not change their usage tostd::cout
or addedusing std::cout;
at the beginning. \$\endgroup\$