Class Pointer Losing Members? - c++

I have a binary tree, that I am searching:
TreeNode<Vessel*>* node = this->tree_->search("PotatoFace");
string mystring = node->print();
when I run it, node contains the correct data, but when I go to print that data as soon as I enter:
string TreeNode<T>::print()
{
return data_->toString();
}
'this' (which should be the 'node' and has the same memory address as 'node') has all of its data members including the Vessel* set to null.
Any ideas?
Thank you!
Full Tree Node:
#pragma once
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
template <class T>
class TreeNode
{
private:
TreeNode<T>* greaterNode_;
TreeNode<T>* lessNode_;
TreeNode<T>* parentNode_;
TreeNode<T>* getLowest_();
T data_;
public:
TreeNode();
TreeNode(T data);
void add(T data);
bool operator==(const string &rhs);
TreeNode* search(T data);
void seqSearch(string data, TreeNode<T>* node);
void del(TreeNode<T>* root);
void toFile(ofstream& BSTFile);
TreeNode* compare(int sig[4]);
TreeNode* getRoot();
TreeNode* forward(TreeNode<T>* node);
string print();
};
template <class T>
TreeNode<T>::TreeNode(T data)
{
data_ = data;
greaterNode_ = lessNode_ = parentNode_= NULL;
}
template <class T>
TreeNode<T>::TreeNode()
{
}
template <class T>
void TreeNode<T>::seqSearch(string data, TreeNode<T>* node )
{
if(*data_ == data)
{
*node = this->data_;
}
if(this->lessNode_)
{
this->lessNode_->seqSearch(data, node);
}
if(this->greaterNode_)
{
this->greaterNode_->seqSearch(data, node);
}
}
template <class T>
string TreeNode<T>::print()
{
return data_->toString();
}
Still not entirely sure how to explain why it wasn't working, but it was a scope issue, outside the binary tree class tree nodes lost data. Taken out all tree functions that returned nodes and everything works now.

are you sure you wanted to write:
string mystring = node->print();
not
string mystring = hello->print();
If yes, it seems like 'this' of
string mystring = node->print();
is null (node is null). This may have several reasons:
node never gets initialized
node should be set by search("something") but search returns null
It would be really helpful if you paste more code.

Still not entirely sure how to explain why it wasn't working, but it was a scope issue, outside the binary tree class tree nodes lost data.
It was rectified by ensuring that the Binary Tree class doesn't return anything of type TreeNode*, and running whatever other function I wanted once I had the value of the node was done inside the binary tree class. This works now.
Thank you for the help!

Related

Why am I getting bad_alloc? Implementing a stack c++

I'm trying to implement my own Stack in C++ but I keep getting this error when I try to use the method pop() in which what I'm trying to do is:
Save element from the top in a variable called "res".
Get the reference to the next element from the node class and set it as the top.
size--
Return the variable "res".
If you could help me I'd appreciate it. Thank you!
Node class:
template<class T>
class Node {
private:
Node<T>* next;
T element;
public:
Node();
Node(const Node& orig);
~Node();
void setElement(T el);
T getElement();
Node<T>* getNext();
void setNext(Node<T>* ne);
};
Stack class:
#include "EmptyStackException.cpp"
#include "Node.cpp"
#include <iostream>
using namespace std;
template<class T>
class LinkedStack {
private:
int siz;
Node<T>* first;
public:
LinkedStack();
~LinkedStack();
int size();
bool isEmpty();
void push(T e);
T top();
T pop();
};
template<class T>
void LinkedStack<T>::push(T e) {
Node<T> node = Node<T>();
node.setNext(first);
node.setElement(e);
first = &node;
siz++;
}
template<class T>
T LinkedStack<T>::pop() {
T res = first->getElement();
first = *(first->getNext());
siz--;
}
template<class T>
void LinkedStack<T>::push(T e) {
Node<T> node = Node<T>();
node.setNext(first);
node.setElement(e);
first = &node;
siz++;
}
Since node is an object that is local to this function, as soon as this function ends, it is destroyed. However, first contains a pointer to it. So when this function returns, first contains a pointer to an object that no longer exists. You probably want this:
template<class T>
void LinkedStack<T>::push(T e) {
Node<T>* node = new Node<T>();
node->setNext(first);
node->setElement(e);
first = node;
siz++;
}
Now, node still ceases to exist when this function returns. But first doesn't contain a pointer to node, it contains the value of node -- a pointer to a dynamically allocated object.
Note that you will have to manage the lifetime of that object somehow. Ideally, you wouldn't use raw pointers so that you don't have that burden.

Node Class to represent a binary tree C++

I have created a node.h class, defining a class called node for representing a binary tree(any type). It seems to be that the constructor isn't working. The errors are below.Ive only started writing constructors within classes like these, and this is the first ive encountered binary trees. Can anyone point me in the right direction on how to fix these errors and make my code work? Thanks.
Node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
//an object of type node holds 3 things
// - an item (oftype t)
// - a left subtree
// - a right subtree
template<typename T>
class Node {
public:
Node(T item); //constructor to create a leaf node
Node(T item, Node *lft, Node *rht); //constructor which creates an internal node
~Node(); //Destructor
//public data member functions:
bool searchTree(T key);
void printTree();
private:
//private data member functions:
//..
};
//constructor
template<typename T>
Node<T>::Node(T i, Node<T> *l, Node<T> *r) {
item = i;
lft = NULL;
rht = NULL;
}
//constructor //is this correct?
template <typename T>
Node<T>::Node(T i) { //should i be a parameter here?
item = i; //is this right?
}
//destructor
template <typename T>
Node<T>::~Node() {
delete left;
delete right;
//delete;
}
//print tree method
template <typename T>
void Node<T>::printTree() {
if (lft != NULL) {
lft->printTree();
cout << item << endl;//alphabetical order
}
if (rht != NULL) {
rht->printTree();
//cout << item << endl; //post order
}
}
//search Tree method
template <typename T>
bool Node<T>::searchTree(T key) {
bool found = false;
if (item == key) {
return true;
}
if (left != NULL) {
found = left->searchTree(key);
if (found) return true;
}
if (right != NULL) {
return right->searchTree(key);
}
return false; //if left and right are both null & key is not the search item, then not found == not in the tree.
}
#endif
Main.cpp
#include "Node.h"
#include <iostream>
using namespace std;
//set up tree method
Node<string> *setUpTree() {
Node<string> *s_tree =
new Node<string>("Sunday",
new Node<string>("monday",
new Node<string>("Friday"),
new Node<string>("Saturday")),
new Node<string>("Tuesday",
new Node<string>("Thursday"),
new Node<string>("Wednesday")));
}
int main() {
Node<string> *s_tree;
s_tree = setUpTree();
cout << "Part 2 :Priting tree vals " << endl << endl;
s_tree->printTree();
cout << endl;
//search for range of tree values
//searchTree(s_tree, "Sunday");
//searchTree(s_tree, "Monday");
return 0;
}
There is no declarations of members you use in your constructor and other methods. The compiler does not know what rht or right means. Judging from your code the class should look more like this:
template<typename T>
class Node {
public:
Node(T item); //constructor to create a leaf node
Node(T item, Node *lft, Node *rht); //constructor which creates an internal node
~Node(); //Destructor
//public data member functions:
bool searchTree(T key);
void printTree();
private:
Node* left;
Node* right;
T item;
//private data member functions:
//..
};
So now the compiler knows what left, right and item mean. Now you can use these identifiers inside member functions of that class. Note that compiler still does not know what rht or lft are, so you should replace them with right and left.
Hope this helps

Binary Search Tree: Issue with Insert Function

I've been researching how to create binary search trees and i have run into a problem when trying to create my own. I have to use the following private structure to create the tree. Every example that i have looked at uses left and right pointers pointing to the structure and i have to use left and right pointers pointing to my template class. I have been trying to figure out how to write the insert function for adding a new node into my tree but i keep running into problems because of the way these two pointers are setup. Does anyone have a clue on how to make it work with these two pointers below?
private:
struct BinaryNode
{
Comparable element;
BinarySearchTree<Comparable> *left;
BinarySearchTree<Comparable> *right;
};
BinaryNode *root;
};
This is my constructor
BinarySearchTree<Comparable>::BinarySearchTree() {
BinaryNode *temp;
temp = new BinaryNode;
temp->left= NULL;
temp->right= NULL;
root = temp;
}
Try the following:
public:
template <typename Comparable>
void insert(const Comparable& key)
{
root = insert(root, key);
}
private:
template <typename Comparable>
BinaryNode* insert(BinaryNode*& current_node, const Comparable& key)
{
if (current_node == nullptr)
{
// Create a leaf node and return it, thus attaching
// it to the node we last called the function with
return new BinaryNode{key, nullptr, nullptr};
}
if (key == current_node->element)
// duplicate element, take some action
else if (key < current_node->element)
current_node->left = insert(current_node->left, key);
else
current_node->right = insert(current_node->right, key);
return current_node;
}

C++ Binary Tree, modifying root node, pointer to pointer to root node

I realize the title isn't too descriptive so here are the details. I'm implementing my own Binary Tree class in C++. I have written a template Node class and template Binary Tree class already, for the most part, and am stuck on something. I created an empty binary tree (root node is null) and when I try to set that node it fails miserably. here is the code and more explanation:
template<class T> class Node
{
T _key;
Node<T> *_leftChild;
Node<T> *_rightChild;
public:
Node();
Node(T key);
Node(T key, Node<T> *leftChild, Node<T> *rightChild);
~Node();
bool hasLeftChild();
bool hasRightChild();
void setKey(T key);
void setLeftChild(Node<T> *node);
void setRightChild(Node<T> *node);
T getKey();
Node<T>* getLeftChild();
Node<T>* getRightChild();
bool compare(Node<T> *compareNode); // return true if this.Node < compareNode
};
Node implementation not really necessary.. ( I dont think ) it's quite long.
#include "Node.cpp"
#include <iostream>
using namespace std;
template<class T> class BinaryTree
{
Node<T> *_root;
public:
BinaryTree();
BinaryTree(Node<T> *root);
~BinaryTree();
Node<T>* getRoot();
void insert(Node<T> **root, Node<T> *node);
};
template<class T>
BinaryTree<T>::BinaryTree()
{
this->_root = NULL;
}
template<class T>
BinaryTree<T>::BinaryTree(Node<T> *root)
{
this->_root = root;
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
// delete stuff
}
template<class T>
Node<T>* BinaryTree<T>::getRoot()
{
return this->_root;
}
template<class T>
void BinaryTree<T>::insert(Node<T> **root, Node<T> *node)
{
if(!*root)
{
*root = node;
}
}
Main:
BinaryTree<int> *tree = new BinaryTree<int>();
Node<int> *root = tree->getRoot();
Node<int> **root1 = &root;
cout << tree->getRoot() << endl;
Node<int> *noChildrenNode = new Node<int>(2);
tree->insert(&root1, noChildrenNode);
cout << tree->getRoot() << endl;
Inserts current functionality is just supposed to replace the NULL root pointer to the node pointer passed in as a parameter. The failing miserably part is since the pointer is a copy it isn't actually setting the root node.. but I can't seem to figure out how to set up a pointer to a pointer to the root node so it can be altered.. I've got to be close and any help will be MUCH appreciated.
Thanks
First, you've got to include the exact text of any error messages. "fails miserably" is not adequate.
I think you want
root = node;
Not
*root = node;
Because if root is null, using *root is a null pointer exception.

C++ Custom List template

I am trying to create a custom List template in C++ but haven't gotten very far! I have the skeleton of the coding done and am now trying to fill it in. Basically, I would like it to be possible to create a new list where the two nodes (first, last) will be NULL until values are added into the list.
However, there is a huge error message which seems to be a i/o one. It is too long to paste here. I'm sure I am doing something silly that will be spotted by those more experienced then me.
#include <iostream>
using namespace std;
template <class T>
class node {
T value;
node<T> *next;
node<T> *previous;
};
template <class T>
class my_list {
public:
node<T> first;
node<T> last;
my_list(){
first = NULL;
last = NULL;
}
~my_list(){
}
void push_back(T val);
};
template <class T>
void my_list<T>::push_back(T val){
if (this->first == NULL) {
cout << "Hello";
}
}
int main() {
my_list<int> newlist;
newlist.push_back(5);
}
You need to fix your declaration of first and last:
node<T> *first;
node<T> *last;