I'm trying to create a binary search tree that would be used to insert my Packet objects'. It holds information like partId
, description
, price
, and partCount
. It's a basic binary search tree. Could you take a look and tell me if there's nothing odd in the code? Because I'm trying to brush up on my BST. Please give me your comments and reviews.
I'm not doing anything complicated, just simple BST. I haven't done C++ in months, trying to review everything again.
This is my testing program:
#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");
}
Here's my BST.h:
#pragma once
#include <string>
#include <iostream>
#include <stack>
#include "Packet.h"
using namespace std;
class Node
{
friend class BST;
public:
Node() : packet(packet), rlink(nullptr), llink(nullptr) {}
~Node() {}
private:
Packet *packet;
Node *rlink, *llink;
};
class BST {
public:
BST();
void BST::insert(Packet& packet);
void BST::insert(Node* &p, Node *newNode);
void preorderTraversal() const;
private:
Node *root;
void preorderTraversal(const Node *p) const;
};
The BST.cpp Careful with this part, I might have done some things here, can you take an extra look here?:
#include "BST.h"
BST::BST() : root(nullptr) {}
void BST::insert(Packet& thisPacket) {
Node *newNode = new Node;
newNode->packet = &thisPacket;
insert(root, newNode);
}
void BST::insert(Node *&p, Node *newNode)
{
if (p == nullptr)
p = newNode;
else if (p->packet->getPartId() < newNode->packet->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->packet->getPartId() << " ";
preorderTraversal(p->llink);
preorderTraversal(p->rlink);
}
}
And finally 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;
};