I hope this isn't a duplicate but I couldn't find any similar posts.
The build on this may seem overly convoluted but I am trying to learn binary sorting trees and apply it to my graph knowledge. I understand it may be easier to use an adjacency list.
Currently the build utilizes two structs and a class, one struct for Edges, one for Vectors, and the "Graph" class functions as a binary search tree.
// Vertex node stores name and neighbors
struct Vertex {
string name;
vector<Edge *> *neighbors;
};
class Graph {
// Functions like BST and stores a Vertex object with each node
int value;
Graph *left, *right;
struct Vertex *node;
The functions that interact with this portion of the program are my default and parameterized constructors:
Graph::Graph() : value(0), left(NULL), right(NULL), node(){};
Graph::Graph(int data, Vertex* node) {
value = data;
left = right = NULL;
Vertex* newNode = new Vertex;
newNode = node;
};
and my addVertex
function:
Graph* Graph::addVertex(Graph* root, string name, int data){
if (!root) {
Vertex* newVertex = new Vertex;
newVertex->name = name;
newVertex->neighbors = new vector<Edge *>();
node = newVertex;
return new Graph(data, node);
}
if (value > root->value){
root->right = addVertex(root->right, name, data);
}
else if (value < root->value){
root->left = addVertex(root->left, name, data);
}
return root;
};
I have tried removing each one of the constructors separately, tried creating a new Vertex within the constructors. Changing the variable order within the class and structs. It seems like the program is only creating over vertex and is overwriting the vertex repeatedly.
What I expect to happen is the program would create a new Vertex and store its name for later comparison, but my output is this:
Vertex name: g Graph Node Value: 3
vertex name: g Neighbors:
Vertex name: g Graph Node Value: 2
vertex name: g Neighbors:
Vertex name: g Graph Node Value: 1
vertex name: g Neighbors:
So I am getting the value that is assigned to the node in the BST but I am not getting the name stored in the Vertex.