Pointer inside the struct

closed account (Nwb4iNh0)
use pointers inside classes or struct, can some explain
why/how use pointers inside the struct, i have an example here , if someone can help, please explain it.

1
2
3
4
5
6
  struct Node {
    int ID;
    Node *left, *right;               
    Node(const char id, Node* l, Node* r) { ID = id;  left = l;  right = r; }
};
Last edited on
This looks like the node for a binary tree. A binary tree has two nodes - left node and right node which points to the left Node and right Node. If you don't understand trees, then look them up.

In the above left is a pointer to Node - points to the next left Node. Right is also a pointer to Node - points to the next right Node.

The constructor takes 3 parameters
1) an id of type char (should be of type int?) node data
2) l of type Node* (pointer to Node) left pointer
3) r of type Node* as right pointer.

The struct data variables are then initialised with these values.
closed account (Nwb4iNh0)
thanks for reply , but the question here is why did we use this line ( Node *left, *right; )
why the pointer is used here?
Each separate Node stores 3 things:
1. Some data. In this case the ID. (It could be all sorts of things - name and address and bank balance - ie not necessarily just a single integer.)
2. The location/pathway/connection to another related Node to the left of it via the left pointer, Node* left
3. - ditto - Node to the right ... right pointer, Node* right.
why the pointer is used here?


So that a new object can be created when required (on the heap).
why the pointer is used here?

As seeplus said, your struct is used for building a binary tree.
https://en.wikipedia.org/wiki/Binary_tree
A node can't include itself. You would have a never ending structure declaration.

left and right pointers are initialized as nullptr until needed.
When you want to store a new piece of information in your tree, you create a new node and decide if it goes on the left or the right. for example, if ID is less than the root node, it would go on the left, if ID is greater than the root node, it would go on the right.

BTW, you have a type mismatch between your data item and your constructor. ID is an int, while your constructor argument is a char.




Last edited on
closed account (Nwb4iNh0)
So you guys mean that Node *left, *right; refer to the value of the left element and right element and NOT refer to the address in memory>_?

like this example :

struct linkedlist {

int value;

struct linkedlist* nextPointer;

}

The nextPointer here is used to point to next item in the linked list not for its memory address!!
stand in front of a mirror, and hold up a mirror. What do you see? Your reflection holding a mirror that reflects the reflection of .... and it goes on forever until its too small to see.

you can't say
struct thing
{
thing t; //each thing has a thing that has a thing holding another thing... forever
}

a pointer can be null, and stop the infinite chain above. This type of construct MUST be done with pointers for that reason.
The nextPointer here is used to point to next item in the linked list not for its memory address!!

A pointer contains a memory address. In the case of nextPointer it contains the address of the next node (or null if at end of list), Linked lists aren't typically ordered, although they can be.

left and right are the same. They contain the address of the next node on one side of the tree (or null). A binary tree is typically used to create an ordered list. i.e. lower values go to the left, higher values go the right.

Last edited on
Topic archived. No new replies allowed.