So I have created a Binary Search Tree (BST) by placing nodes into a vector. These nodes store 3 values, a user input int
ID, a user input int
age, and a user string
input name.
When inserting these nodes into the vector, they are stored going in ascending order.
Currently I'm working with two nodes.
104 10 Bob
102 11 Steve
When pushing back the first node, there are no problems; however, when attempting to push back the second node, I am getting an out_of_bounds error thrown by the vector class.
I believe something is wrong with my insert function when attempting to switch the positions of these two nodes, however I can't tell exactly where the issue lies.
#include "BinaryTree.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int index;
struct Node
{
int ID;
int age;
string name;
Node()
{
}
Node(int id, int Age, string nm)
{
this->ID = id;
this->age = Age;
this->name = nm;
}
};
vector<Node> binaryTree;
BST::BST()
{
}
void BST::start()
{
int choice;
cout << "What would you like to do?" << endl;
cout << "1. Add a node to the tree" << endl;
cout << "2. Delete a node from the tree" << endl;
cout << "3. Find a node in the tree" << endl;
cout << "4. Report the contents of the tree" << endl;
cout << "5. Exit program" << endl;
cin >> choice;
if (choice == 1)
{
insert();
}
if (choice == 3)
{
find();
}
if (choice == 4)
{
report();
}
}
void BST::insert()
{
int ID;
int AGE;
string NAME;
cout << "Please enter the ID number, age and name" << endl;
cin >> ID >> AGE >> NAME;
Node *tree = new Node(ID, AGE, NAME);
if (index == 0)
{
binaryTree.push_back(*tree);
index++;
}
if (index > 0)
{
if ((binaryTree.at(index - 1).ID) < ID)
{
binaryTree.push_back(*tree);
index++;
}
}
if (index > 0)
{
if ((binaryTree.at(index - 1).ID) > ID)
{
Node *temp = new Node();
*temp = binaryTree.at(index - 1);
binaryTree.at(index - 1) = *tree;
binaryTree.at(index) = *temp;
index++;
}
}
cout << "Added! Size: " << binaryTree.size() << endl;
cout << " " << endl;
start();
Would appreciate the help! Thanks!
Your vector doesn't resizes when you do this: binaryTree.at(index) = *tree;
Do push_back()
then try sort
binaryTree.push_back(*tree;)
std::sort(binaryTree.begin(),binaryTree.end(),[](const Node& n1, const Node& n2){//do your comparations});
Or simply use std::set
If you want to work with std::vector without crashing, then your insert() have to look like this:
void BST::insert()
{
int ID;
int AGE;
string NAME;
cout << "Please enter the ID number, age and name" << endl;
cin >> ID >> AGE >> NAME;
//Node *tree = new Node(ID, AGE, NAME); // Don't use new here, there is no need in this
Node tree(ID, AGE, NAME);
binaryTree.push_back(tree);
std::sort(binaryTree.begin(), binaryTree.end(), [](const Node& n1, const Node& n2)
{
//compare your nodes here
return (n1.ID > n2.ID);
});
cout << "Added! Size: " << binaryTree.size() << endl;
cout << " " << endl;
start();
}
But this won't be a binary tree. You need other data structure to create binary tree, std::vector
can't be binary tree.
There is a ready solution for you, look at std::set
, It inserts elements like you need, you will need to add your custom compare function to std::set
and everything will be fine.
Here is std::set
example for you:
class Node
{
public:
Node(int id):ID(id){}
int ID;
};
class NodeComparator
{
public:
bool operator()(const Node& n1,const Node& n2)
{
return n1.ID < n2.ID;
}
};
int main()
{
std::set<Node, NodeComparator> set1;
set1.insert(10);
set1.insert(8);
set1.insert(14);
set1.insert(2);
return 0;
}