0

I am trying to implement Dynamic graph using Treap data structue.

Here is node structure:

    class TreapNode
    {
        public:
                int key;
                int priority;
                TreapNode* left, *right;
                vector<int> neighbourNode;
                
                TreapNode(int key)
                {
                    this->priority = 0;
                    this->key = key;
                    this->left  = nullptr;
                    this->right = nullptr;
                }
                
                TreapNode()
                {}
                
                TreapNode* addNode(TreapNode*&,int);
                void updateNode(TreapNode*&,int,int);
     };          
     

When I want to add neighbouring node to particular node,

  1. I search the node , and then add neighbouring node to search node's vector<int> neighbourNode through updateNode(), in the following way.

searchAddress->neighbourNode.push_back(x);

But my professor says, store address of vector<int> neighbourNode in the node.

  1. Is it going to reduce my TreapNode size ?
  2. How to store address and access it ? I tried this way in TreapNode class but it is giving neighbourNode undefine error.

int* neighbourNodeAddress = neighbourNode.data()

Can anyone help me ?

tushar
  • 313
  • 4
  • 10
  • Are you sure your professor didn't mean to store the addresses of the *nodes*, rather than the vector containing the nodes? That would make more sense given the return value of `addNode`. – Botje Oct 30 '20 at 14:20
  • @Botje : my professor saying , store address of vector neighbourNode in TreapNode. I am returning head node through addNode() that's why its return type is TreapNode*. – tushar Oct 30 '20 at 14:24
  • @Botje : `addNde()` just add node with priority as 0. Adding edges between nodes and adding neighbour is done through `updateNode()` – tushar Oct 30 '20 at 14:28
  • 1
    Storing a `vector*` instead of a `vector` will save either two pointers or two size+capacity numbers in your `TreapNode`, but you still have to store them in the heap-allocated `vector`. You also need an extra pointer dereference to get to your neighbours. It is a questionable optimization, I would say. – Botje Oct 30 '20 at 14:52

0 Answers0