Why doesn't a member function change my objects data members? - c++

So I was working on a Project for a computer science class in which we need to create a binary search tree and a corresponding index. We needed to use recursion for this project.
Here is my class implementation:
class Leaf;
struct indexEntries;
class BinarySearchTree{
public:
BinarySearchTree();
~BinarySearchTree();
//Helper Functions for recursive calls
std::string search(std::string);
void BuildTree(std::string);
void inOrderPrint();
private:
//Recursive Functions
void BuildTreeR(int start, int end, Leaf * r);
void inOrderPrint(Leaf * start);
Leaf * search(std::string inquiry, Leaf * start);
void DeallocateTree(Leaf * start);
//Data members
Leaf * root;
std::vector<indexEntries> BSTindex;
};
class Leaf{
public:
Leaf(){
indexID = "";
AccNum = "";
left = NULL;
right = NULL;
};
void set_index(std::string input) {indexID = input;};
void set_AccNum(std::string input) {AccNum = input;};
void set_left(Leaf* newLeft) {left = newLeft;};
void set_right(Leaf* newRight) {right = newRight;};
std::string get_index() {return indexID;};
std::string get_AccNum() {return AccNum;};
Leaf * get_left() {return left;};
Leaf * get_right() {return right;};
private:
std::string indexID;
std::string AccNum;
Leaf * left;
Leaf * right;
};
And when I try to pass Leaf * BinarySearchTree::root to the function void BinarySearchTree::BuildTreeR(int, int, Leaf*) the Leaf that the root is pointing to goes unchanged.
Here is my BuildTreeR() function:
void BinarySearchTree::BuildTreeR(int start, int end, Leaf * parent){
int mid = (start+end)/2;
if(parent == NULL){
parent = new Leaf;
parent->set_index((BSTindex[mid]).indexID);
std::string fullEntry = BSTindex[mid].dataBaseEntry;
parent->set_AccNum(fullEntry.substr(4, 3));
}
if((mid-1)>start){
BuildTreeR(start, mid-1, parent->get_left());
}
if((mid+1)<end){
BuildTreeR(mid+1, end, parent->get_right());
}
}
Using the debugger, I found that the leaf pointed to by Leaf * parent gets changed, but these changes are not carried over to Leaf * BinarySearchTree::root, which stops my program from working.
The debugger says that the value of the data I'm trying to change is
CXX0030: Error: expression cannot be evaluated
Has anyone had this happen before/ know how to fix it?

Your analysis of the problem is exactly right: the pointer is passed by value, so any changes that the function makes to the value of parent are not visible to the caller.
One way to fix the problem is by passing the parent pointer by reference:
void BinarySearchTree::BuildTreeR(int start, int end, Leaf *& parent){
(note the added &).
This way any changes made to parent inside the function will automatically be visible to the caller.

Related

How to create insert function in BST without specifying root as parameter

I've been given a task to create a method of Binary Search Tree class to insert elements in the correct place in the tree. The declaration of this function is:
void BST::insert(int k)
{
}
Can someone explain why isn't a root node given as parameter ? How am i able to traverse the tree, when I do not have its root node ? Having a return type of void hints me to use the 'this' keyword
I've tried implementing the following:
void BST::insert(int k) {
while(this->root != NULL) {
if(k < this->root->value) {
this->root = this->root->leftChild;
} else {
this->root = this->root->rightChild;
}
}
this->root = new node(k);
}
This is additionnal OOP code:
struct node {
int value;
node* parent;
node* leftChild;
node* rightChild;
node (int);
int dessiner(ofstream&, int);
};
class BST {
public:
node* root;
BST();
void dessiner(string);
void swap(node*, node*);
void inserer(int);
};
EDIT: I added 2 pointers. tmp to traverse tree and P to keep track of tmp's parent node
node* tmp = this->root;
node* p = NULL;
while(tmp!=NULL) {
p = tmp;
if(k < tmp->value) {
tmp = tmp->leftChild;
} else {
tmp = tmp->rightChild;
}
}
tmp = new node(k);
tmp->parent = p;
Can someone explain why isn't a root node given as parameter ?
It is. BST::insert implicitly has a BST * parameter, named this. From there you can get at root. Note that you don't need this-> to refer to root, it is implicit in body of the member function.
Having a return type of void hints me to use the 'this' keyword
The return type has nothing to do with it.
Note that you will need to assign the new node to p's leftChild or rightChild, after insert finishes, nothing points to it.
When dealing with BSTs I usually write a public function like the one you have which calls a the private, recursive function with the root of the tree. You don't have access to the root of the tree from outside the class so it doesn't make sense for the public function to accept anything more than the element to insert.
void BST::insert(int k)
{
insert(k, root);
}
void BST::insert(int k, node* curr)
{
// logic to insert the new element
...
}
You can combine these functions with a default parameter so from outside the class you can call bst.insert(5) and curr will start out as the root of the tree.
void BST::insert(int k, node* curr = root)
{
// logic to insert the new element
...
}

C++ Binary Search Tree Insert Implementation

I'm trying to build a function to insert into a binary search tree, but I'm having a hard time figuring out why it won't work. I understand fundamentally how the function is supposed to work, but based on the template I was given it seems as though I am to avoid creating a BST class but instead rely on the Node class and build the desired functions to work on that. Here's the given template:
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
class Node {
int value;
public:
Node* left; // left child
Node* right; // right child
Node* p; // parent
Node(int data) {
value = data;
left = NULL;
right = NULL;
p = NULL;
}
~Node() {
}
int d() {
return value;
}
void print() {
std::cout << value << std::endl;
}
};
function insert(Node *insert_node, Node *tree_root){
//Your code here
}
The issue I'm having is when I implement the following code, where getValue is a simple getter method for Node:
int main(int argc, const char * argv[]) {
Node* root = NULL;
Node* a = new Node(2);
insert(a, root);
}
void insert(Node *insert_node, Node *tree_root){
if (tree_root == NULL)
tree_root = new Node(insert_node->getValue());
The code appears to compile and run without error, but if I run another check on root after this, it returns NULL. Any idea what I'm missing here? Why is it not replacing root with a new node equal to that of insert_node?
I also realize this doesn't appear to be the optimal way to implement a BST, but I am trying to work with the template given to me. Any advice would be appreciated.
As Joachim said your issue relates to difference between passing parameter by reference and by value.
In your code void insert(Node *insert_node, Node *tree_root) you pass Node* tree_root by value. Inside the function you change local copy of this pointer, so outer value is not changed.
To fix it you should pass Node* tree_root by reference. Parameter declaration can be Node*& tree_root (or Node** tree_root). E.g:
void insert(Node* insert_node, Node*& tree_root){
if (tree_root == NULL)
tree_root = new Node(insert_node->getValue());

Searching for a node in a tree structure

I need help finding and returning a "node" in a general tree structure. Each node can have more than 2 children so it's not a binary tree. I've been given the following code, this Element object has a list to contain its children, I create one element node pointer in main and using that I have to add and search for children. This is for a school project but I'm not looking for answers (an answer wouldn't hurt). Any advice on how to go about this problem would be much appreciated, thanks!
#pragma once
#include <iostream>
#include <list>
#include <sstream>
using namespace std;
class Element
{
private:
list<Element*> children;
char* _tag;
int _value;
// private methods
public:
// default constructor
Element();
// non-default constructors
Element( char* name); // value is set to -99 if not given
Element(char* name, char* value);
// destructor, must recursively destruct its children
// and release the memory allocated for _tag
~Element();
// ostream operator ( pre-order traversal)
friend ostream& operator << (ostream& out, const Element& E);
void display_xml(); // print out the tree in xml-- format
void addChild( Element* child); // add a child
// Find the first element such that _tag == tag
// returns “this” pointer of this element
Element* findTag( char* tag);
char* getName();
int getValue();
void setName(char* name);
void setValue( int value);
int height(); //b return the height
int size(); // return the size
// other methods
};
this is my best attempt at a solution, it has obvious problems but I'm new to all of this and some explanation on a proper solution, or some sample code would be very helpful!
Element* Element::findTag(char* tag)
{
list<Element*> temp = children;
int s = temp.size();
if(getName() == tag)
{
return this;
}
else
{
for(int i = 0; i < s; i++)
{
findTag((*temp.front()).getName());
temp.pop_front();
}
}
}
I will give you a pseudo-code for searching for a node that has a value val in a tree rooted at root:
find(Node root, val)
if(root.value == val) return root //-- if the recursion found the node we are searching for
else
for every child x of root //-- re-cursing on the children of root
if(find(x, val) != null) return x //-- if one of the calls found the node we are searching for
return null //-- if we did not find the node we want in the sub-tree rooted at root

C++ Access violation reading location, class this pointer is NULL

I would like to get some help with my current project as I struggle to understand what exactly went wrong in my program. I believe the problem is with my constructor. When I call member function it behaves as I haven't initialized my class members.
Here is my class:
class BankList {
public:
// constructor
BankList();
BankList(int size);
bool isEmpty() const;
int size() const;
void insertBankEntry(std::string account, std::string level, std::string lName, std::string fName, float value); // add a single entry to the list
void insertBankData(std::string fileName); // populate list data from the file
void deleteBankEntry(std::string key); // delete a single entry
void findBankEntry(std::string key) const; // Find and display one element using key
void checkBankEntry(std::string account);
void printHashBankData() const; // List data in hash table sequence
void printHashKeyBankData() const; // List data in key sequence (sorted)
void printTreeBankData() const; // Print indented tree
void writeBankData(); // Write data to a file
void outputHashStatistic() const; // print hash stats
private:
HashList* hashlist;
Tree* tree;
int count; // number of records
int hashSize;
};
Here are my constructors:
BankList::BankList()
{
HashList* hashlist = new HashList();
Tree* tree = new Tree();
count = 0;
hashSize = 0;
}
BankList::BankList(int size)
{
HashList* hashlist = new HashList(size);
Tree* tree = new Tree();
count = 0;
hashSize = size;
}
The function I am trying to call:
void BankList::insertBankEntry(string account, string level, string lName, string fName, float value) // add a single entry to the list
{
BankCustomer* customer = new BankCustomer(account, level, lName, fName, value);
hashlist->insert(customer);
tree->Insert(customer);
count++;
}
However, it does work if I place this code in my function.
if (!tree || !hashlist)
{
tree = new Tree();
hashlist = new HashList();
}
main:
int size = getSize();
BankList* list = new BankList(size);
list->insertBankEntry("123412341234", "Gold", "Jonhson", "Steve", 1234.45);
Thanks in advance!
In the constructors you are hiding the member variables (by declaring variables with the same name as the members) hence your member variables remain un-initialized
HashList* hashlist = new HashList(); // hiding the member variable this->hashlist
Tree* tree = new Tree(); // hiding the member variable this->tree
Just use
hashlist = new HashList();
tree = new Tree();
inside the constructors.

extern global object doesn't seem to initialise its member

I'm getting a segfault when executing this code. Specifically after g_lru_stack.add_node(&lru_node) is called.
Running under GDB shows that the dummy node doesn't look like it has been initialised. Is this to do with how extern globals are initialised? If so any help would be greatly appreciated here.
I have included snippets from both the header and cpp file.
Specifically, my question is this:
How can I get LRU_Stack g_lru_stack , declared at the top of object.cc, to call the LRU_Node ctor that takes no arguments.
It seems this ctor never gets called, hence why my dummy node isn't initialised.
Object.h
class obj_payload;
extern cas_mutex g_lru_stack_mutex;
class LRU_Node {
private:
obj_payload* payload;
LRU_Node* up;
LRU_Node* down;
size_t predicted_bytes_in_cache;
public:
LRU_Node() : payload(nullptr), up(this), down(this), predicted_bytes_in_cache(1337) {} // Dummy ctor
LRU_Node(obj_payload* p) : payload(p), up(nullptr), down(nullptr), predicted_bytes_in_cache(88) {} // Normal Creation of node
//Adds a node to the top of the stack
//Has dummy context
void add_to_stack(LRU_Node* newNode);
//Sets how many bytes of the object are predicted to be in the cache
//Has dummy context
void is_node_in_cache(LRU_Node* node);
//Moves a node to the top of the stack
//Has dummy context
void move_node_to_top(LRU_Node* node);
//Has context of caller
size_t get_predicted_bytes_in_cache();
};
class LRU_Stack {
LRU_Node dummy;
public:
void add_node(LRU_Node* node);
void move_node_to_top(LRU_Node* node);
};
extern LRU_Stack g_lru_stack;
class obj_payload {
typedef uint32_t ctr_t;
private:
ctr_t refcnt;
const uint32_t sz; // size of the data space in bytes
LRU_Node lru_node; // Jordan -- This arg objects node for the LRU_Stack
obj_payload( typeinfo tinfo_,
uint32_t size_,
int refcnt_init=1 )
: refcnt( refcnt_init ),
sz( size_ ),
tinfo( tinfo_ ), lru_node(this) {
g_lru_stack.add_node(&lru_node);
}
Object.cc
#include "object.h"
namespace obj {
//Jordan -- Global LRU_Node Stack
cas_mutex g_lru_stack_mutex;
LRU_Stack g_lru_stack;
//Adds a node to the top of the stack
//Has dummy context
void LRU_Node::add_to_stack(LRU_Node* newNode) {
newNode->down = down; // Set the new nodes previous -> dummys previous
newNode->up = this; // Set new nodes next -> dummy
down->up = newNode; // Dummy next -> new node (i.e. Previous top of stack node up -> newNode)
down = newNode; // Dummy previous -> new node (i.e. Dummy down pointer now links back round to the new node at the top)
}
//Sets how many bytes of the object are predicted to be in the cache
//Has dummy context
void LRU_Node::is_node_in_cache(LRU_Node* node) {
size_t total = 0;
LRU_Node* orignal = node;
while (node != this) {
total += node->payload->get_size(); // Add current size to total
node = node->up; // Go to next node
}
node = orignal; //Reset node to the passed in node, then set how many bytes it has contained within cache
if (total <= cache_size) {
node->predicted_bytes_in_cache = node->payload->get_size();
}
else {
node->predicted_bytes_in_cache = (node->payload->get_size()) - (total - cache_size) < node->payload->get_size() ? (node->payload->get_size()) - (total - cache_size) : 0;
}
}
//Moves a node to the top of the stack
//Has dummy context
void LRU_Node::move_node_to_top(LRU_Node* node) {
if (down != node) { // Check that the node to move is not already top of stack
node->down->up = node->up;
node->up->down = node->down;
if (down == node->up) { // If the node is seccond top of stack
node->up->up = node;
}
node->down = down;
node->up = this;
down->up = node;
down = node;
}
}
//Has context of caller
size_t LRU_Node::get_predicted_bytes_in_cache() {
return predicted_bytes_in_cache;
}
//Has dummy context
bool LRU_Node::is_empty() {
return (up == this);
}
void LRU_Stack::add_node(LRU_Node* node) {
g_lru_stack_mutex.lock();
dummy.add_to_stack(node);
g_lru_stack_mutex.unlock();
}
void LRU_Stack::move_node_to_top(LRU_Node* node) {
g_lru_stack_mutex.lock();
dummy.is_node_in_cache(node);
dummy.move_node_to_top(node);
g_lru_stack_mutex.unlock();
}
"extern globals" are not objects (unless they include an initializer): They are forward-declarations.
Global objects are initialized in two phases:
All those with compile-time constant initializers are done, the rest is zeroed.
The run-time initializers are run in order of object definition. (No ordering guarantees for objects in different compilation units!).
Seems like UB got you there.
To solve the error, do one of these:
Put the definition of the object before its first use in the same compilation unit (The most efficient method).
Put the object as a static in an accessor function. Init will be on first use (thread-safe!).
Type& getTypeSingleton() {
static Type x/*optional initializer*/;
return x;
}
(Implementation dependent) The compilation unit first mentioned on the command-line will be initialized first in all current implementations (As effifient as the first, but fragile).