Binary search tree. Pointer as reference parameter - c++

So I am working on Binary search tree function. Why do I have to add a &sign in front of the node pointer? I thought it is already a pointer and it already points to a location. I understand that if I add a node, then I need to make sure the parent node point to the new node or the parent's node will still point to NULL. But why don't I have to do that if I pass my node pointer as node*&?
bool bst::remove123(int data, node*& x)
{
if (x == NULL)
{
return false;
}
else if (x->getData() < data)
{
return remove123(data, x->right);
}
else if (x->getData() > data)
{
return remove123(data, x->left);
}
else
{
node* old = x;
if (x->left == NULL)
{
x = x->right;
}
else if (x->right == NULL)
{
x = x->left;
}
else
{
replacement(old, x->left);
}
delete old;
return true;
}
}
Thank you

node*& x is a reference to a node*. This means that when bst::remove123 modifies x to point to a different address, the code that called bst::remove123 sees the same change in the node* variable that it passed to the method. If you declared the x parameter as node *x instead, then bst::remove123 would only be modifying a copy of the variable passed in that parameter, and those changes would be lost after the method returned. While the & is used to designate a reference, this is very different to the & operator (often used with pointers) which returns the address of the variable following it.
int n = 10;
int *pn = &n; // Create a pointer to int, set it to the address of n.
int& rn = n; // Create an int reference, set it to reference the same variable as n.
*pn = 5; // Set n to 5 via a dereferenced pn. A dereferencing operator *
// is needed to indicate that we want to change the memory that
// pn points to, not the address that the pointer contains.
rn = 20; // Set n to 20 via the reference rn. Unlike with pointers,
// references do not use a dereferencing operator.

Related

How does reference to pointer exactly work in C++, and when do we need them (in the case of linked list)

I know that pointers hold the address of a variable. And references point to the same address in the symbol table (that is, the same address of the variable, that they are assigned to).
My question is, how do reference to pointers exactly work. And when do we need them, as opposed to using pointer alone (and not using reference to pointer). It will be helpful if you could explain me the use of reference to pointer, with respect to a singly linked list.
I have the following code that deletes the head pointer of a linked list using a function:
struct Node
{
int data;
Node* next;
};
struct Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = nullptr;
return temp;
}
Node* deleteHead(Node* &head)
{
if (head)
{
Node* temp = head;
head = head->next;
delete temp;
}
return head;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(6);
head->next->next = newNode(4);
head->next->next->next = newNode(8);
head = deleteHead(head);
Node* temp = head;
while (temp != nullptr)
{
cout << temp->data << " " << endl;
temp = temp->next;
}
return 0;
}
In the deleteHead(Node* &head) function, the function takes the argument, Node* &head. But, the code works fine, even when the argument is Node* head. for what cases do we need to pass Node* & instead of Node* in a linked list?
Following is the deleteHead(Node* &head) function above, which works the same, if we only use Node* head as the argument, instead of Node* &head -
You pass a pointer by reference for the same reason you pass a non-pointer by reference: To let the function modify its value.
Let me use a simpler example
#include <iostream>
void foo(int*& x) {
*x = 42; // change the value of the int x points to
x = nullptr; // change the value of x
}
The first line modifies the value x points to (but it does not modify x). The second line modifies x itself.
int main() {
int y = 42;
int* y_ptr = &y;
foo(y_ptr);
if (y_ptr == &y) std::cout << "cannot happen";
}
Because we set x = nullptr, y_ptr will not point to y anymore after the call.
Now if we modify foo to not take a reference we get:
#include <iostream>
void foo(int* x) {
*x = 42; // change the value of the int x points to
x = nullptr; // change the value of x
}
Again the first line modifies the int pointed to by x. However, now the second line only has an effect on x local to the function.
int main() {
int y = 42;
int* y_ptr = &y;
foo(y_ptr);
if (y_ptr == nullptr) std::cout << "cannot happen";
}
The value of y_ptr cannot change by passing it to foo, because it is passed by value.
In your code you have
Node* deleteHead(Node* &head)
{
if (head)
{
Node* temp = head;
head = head->next;
delete temp;
}
return head;
}
And when you write head = deleteNode(head) two things are happening:
the function modifies head (because it is passed by reference) to point to head->next.
the function also returns this "new" head (pointing to head->next) and that is assigned to head.
So you basically asign to headtwice. Because head is passed by reference deleteNode would do the right thing without using the return value:
deleteNode(head); // this already does modify head
...or put the other way around: If you return the "new" head (head->next) from the fucntion and assign it to head, then it does not matter if you pass the pointer by reference, because the assignment done inside the function has the same effect.
Your code is similar to
int* bar(int*& x) {
x = nullptr;
return x;
}
and then call it via
int y = 42;
int* y_ptr = &y;
y_ptr = bar(y_ptr);
where the same effect could be achieved by not using the returned value bar(y_ptr). Or the same without pointers (because pointers really make no difference here):
int moo(int& x) {
x = 0;
return x;
}
int x = 42;
x = moo(x); // same as `moo(x)`
PS: You dont need both (return the pointer and assign it already in the function), so better make the function return void.
So,
both, reference and pointer contain an address of variable/memory.
reference has semantic of variable (if you set the value, you write data into referenced memory),
pointer has semantic of pointer (if you set the value, pointered memory isn't changed) and you can set the pointer which addresses an other memory.
about deleteHead(Node* &head) - you use the reference of real variable that contains pointer of Node. The function returns new value of head in the same variable and as return value too.
References are "safe pointers" that are used with value semantics (which is really helpful in an operator overloading context), so the usage of references is very similar to the usage of pointers in C, except these points:
References hold a single value and not an array
References are non-null (which is not always desirable)
It means that you can (or, should) pass a reference whenever you want to change the original passed variable (and not a copy of it).
Saying that, the C (rought) equivalent to your function is Node* deleteHead(Node** head).
Note that since you passed a reference, the original head variable was modified and thus your function becomes a bit weird, since it both modifies head and returns its value.
You can use one of the following options:
(1) deletes head (if the list size is non-empty) and returns a pointer to the next element, this is not advisable since it will leave head as a dangling pointer. This is your original function but it does not receive a reference.
Node* deleteHead(Node* head)
{
if (head)
{
Node* temp = head; // You might want to use auto
head = head->next;
delete head;
}
return head;
}
(2) The same as your function, but returns no value (because you already modified head). This one will not work without passing a reference.
void deleteHead(Node* &head)
{
if (head)
{
Node* temp = head->next;
delete head; // deletes the content of head
head = temp;
}
}
With the reference to pointer deleteHead function, you may use head = deleteHead(head) or deleteHead(head) since head is just a reference of head in the main function, any change on head in the deleteHead function is actually applied on head variable in the main function. With the pointer version, you must use head = deleteHead(head).

Passing a pointer to a function

I was trying to implement BST using C++ , so i tried this:
#include <iostream>
#include <stdlib.h>
struct node
{
int value;
node* left;
node* right;
};
void insert(node *cur , int val)
{
if(!cur)
{
cur = new node;
cur->value = val;
cur->left = NULL;
cur->right = NULL;
return;
}
if(val <= cur->value)
insert(cur->left , val);
else
insert(cur->right , val);
}
using namespace std;
int main()
{
node *root = NULL;
insert(root , 20);
insert(root , 21);
cout<<(*root).value;
return 0;
}
but I have a problem, my insert() function works good, but the change in cur does not seem to reflect into the root pointer, as root remains NULL after the `insert() function calls. What is wrong here?
EDIT: Thanks for all your answers, making a pointer to a pointer seems to be to be ugly and tedious, is there any other way around, acheiving this with some other design?
Here, the root itself has been passed to insert() using pass-by-value. so, from insert(), the value of root cannot be changed. In other words, the cur is local to insert() function. Any changes made to cur itself won't impact the actual argument passed.
If you want to change the value of root from insert(), you need to pass a pointer to root from main().
To elabotare, you can change the value at the address pointed by cur from insert(). So, following the same analogy, if you change
insert(&root , 20);
void insert(node **cur , int val)
all the occurrences of cur to *cur
you should be all good to go.
If you want the function to operate on the outside pointer rather than a local copy you need to pass by reference:
void insert(node*& cur, int val)
{
// ...
}
Otherwise the function works on a copy of the pointer and the outside variable remains unchanged.
The wrong is that you pas a pointer by value, change that value but the caller does not know about it. Change it to
void insert(node **cur , int val)
{
if(!*cur)
{
*cur = new node;
(*cur)->value = val;
(*cur)->left = NULL;
(*cur)->right = NULL;
return;
}
if(val <= (*cur)->value)
insert((*cur)->left , val);
else
insert((*cur)->right , val);
}
And change function call accordingly (...exercise!)
C++ makes function calls as Call by Value. So it makes a copy of the pointer and passes that to the function. If you pass that pointer, you have access to the data the pointer is pointing to, but NOT to the pointer outside the function itself, as only the adress the pointer is pointing to is copied.
You need to pass a pointer to the pointer if you want to modify the pointer itself (that would be the C attempt) or pass by reference, of which c++ is capable of.
If you want to use the C attempt:
void insert(node ** cur , int val)
if(!(*cur))
{
(*cur) = new node;
(*cur)->value = val;
(*cur)->left = NULL;
(*cur)->right = NULL;
return;
}
or the C++ attempt (here you only have to modify the type of cur, everthing else will remain as it is):
void insert(node *& cur , int val)
If you reassign cur to a new node in insert, that does not mean that root is assigned that value (especially that root is not an address at all, but NULL).
Either pass a pointer to an empty node to insert on initialization (and update it with relevant data) or return a new node (and assign it to root in main).

C++ bad PTR in char* (Expression cannot be evaluated)

I've been searching for quite a time for an answer, although there were similar problems I still couldn't improve my code so it would work.
I have a simple lifo structure to which I am trying to add one element and print the structure. It prints nothing and when I am debbuging I have this <bad ptr> in char * nameOfVariable.
I would appreciate any help! Here is my source code:
#include<stdio.h>
struct Variable
{
double value;
char *name;
struct Variable *next;
} *variables[80000];
void pop(Variable * head);
void push(Variable * head, char *name, double value);
void show(Variable * head);
int main(){
for(int i = 0; i <80000; i++){
variables[i] = nullptr;
}
char *nameOfVariable = "aaab";
double value = 5;
push(variables[0], nameOfVariable, value );
show(variables[0]);
system("pause");
return 0;
}
void push(Variable * head, char *name, double value)
{
Variable * p ;
p = head;
head = new Variable;
head -> name = name;
head -> value = value;
head -> next = p;
}
void pop(Variable * head)
{
Variable *p;
if (head != NULL)
{
p = head;
head = head -> next;
free(p);
}
}
void show(Variable * head)
{
Variable *p;
p = head;
while (p!=NULL){
printf("%c %f ", p->name, p->value);
p=p->next;
}
printf("\n");
}
PS - I cant use STL so string is not an option :)
You are storing a pointer into a parameter location:
void push(Variable * head, char *name, double value)
{
Variable * p ;
p = head;
head = new Variable;
But the parameter location is local to the function and discarded upon return.
Why do you allocate an array of 80000 elements?
In order to change a location by a function you must either pass the address of that location (a Variable** head in your case) or use a reference.
Much better would be the definition of a class for your stack...
And another one: storing a variable's name as a char* will almost certainly cause trouble later on. Prepare for memory allocation of a char[] and copy the name string.
You do not save the variable you created in push so they all get lost
void push(Variable * head, char *name, double value) {
Variable * p ;
p = head;
head = new Variable;
head -> name = name;
head -> value = value;
head -> next = p;
}
When the function enters head points to null.
in head = new Variable; head now points to a newly created variable on the heap
when the function exits no one keeps track of the newly created variable on the heap. The memory is leaked and there is no way to access that element.
NOTE: You should be aware that Changes you write to head in the function push do not affect variables[0] you passed to the function. variables[0] is pointer to a Variable somewhere. Initially it is nullptr meaning it does not point at anything. head is a copy of variables[0] that means a different pointer that happens to point at the same place in memory (in your case nullptr). That means though that if you change head it points at something else and is no longer pointing to the same object as variables[0]
Suggested Changes:
Make push a function that returns a Variable* to the caller. Which is the new head.
Make push a function that accepts a Variable*& as an in/out parameter and returns the new head in that
(My preference) create a deque struct that holds a Variable* head memeber. pass a deque* to all these functions (push/pop) and in these functions manage the memory

What is Node *& aNode? [duplicate]

This question already has answers here:
Can someone help me understand this? int *& pr [duplicate]
(3 answers)
What does *& mean in a function parameter
(5 answers)
Closed 10 years ago.
In the following code:
void insert(Node *& aNode, int x) {
if (!aNode) {
aNode = new Node(x);
aNode->next = aNode;
return;
}
Node *p = aNode;
Node *prev = NULL;
do {
prev = p;
p = p->next;
if (x <= p->data && x >= prev->data) break; // For case 1)
if ((prev->data > p->data) && (x < p->data || x > prev->data)) break; // For case 2)
} while (p != aNode); // when back to starting point, then stop. For case 3)
Node *newNode = new Node(x);
newNode->next = p;
prev->next = newNode;
}
What is Node *& aNode?
How should I use this function, I mean, which type of parameter should I pass?
I think this code is C++, not C, and Node *&aNode is a reference to a pointer to a Node, so you would pass a Node* to the function, and function would make a reference to that (so the memory location your Node* is pointing to can change).
You may find the Wikipedia article on References (C++) interesting.
A simple example:
#include <iostream>
void addOneToValue(int num) {
++num;
}
void addOneToRef(int &num) {
++num;
}
int main() {
int num = 0;
// print 0
std::cout << num << std::endl;
// print 0 again (addOneToValue() has no effect)
addOneToValue(num);
std::cout << num << std::endl;
// print 1 (addOneToRef() changes the value of num)
addOneToRef(num);
std::cout << num << std::endl;
}
#crashmstr's comment reminded me that I should say how they're different from pointers. Wikipedia does a better job that I could though:
It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.
References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.
Most compilers will support a null reference without much complaint, crashing only if you try to use the reference in some way.

how to design const member function, preventing it from modifying the object

This a const member function, which allow me to get the minimum node of the tree:
BinarySearthTree* BinarySearchTree::min() const
{
// Return the minimum node (left-most node) value of the tree
BinarySearchTree * next = (BinarySearchTree *) this;
for (next ; next != NULL; next = next->pLeft)
if (next->pLeft == NULL)
return next;
}
I have to cast down the const-ness of 'this' pointer when assigning it to 'next', but this actually rises the potential that I may modify the value of what 'this' points to? Rather than always remind myself not to modify whatever 'next' points to, is there a way to prevent it happening by a better designing of the function?
Make next const:
const BinarySearthTree* BinarySearchTree::min() const
{
// Return the minimum node (left-most node) value of the tree
const BinarySearchTree *next;
for (next = this; next != NULL; next = next->pLeft)
if (next->pLeft == NULL)
return next;
return NULL;
}
If you don't want the contents to be modified, then you should make min() return a pointer to a const object.
Your next variable should also be a pointer to a const object, therefore.
Here is how I think your method should look:
const BinarySearchTree* BinarySearchTree::min() const
{
// Return the minimum node (left-most node) value of the tree
for (const BinarySearchTree* next = this; next != NULL; next = next->pLeft)
{
if (next->pLeft == NULL)
{
return next;
}
}
return this;
}
Also, in C++, you should avoid C-style casts. The const_cast exists for this purpose:
BinarySearchTree* next = const_cast<BinarySearchTree*>(this);
But that is not necessary in this instance.