Question about variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raistlinx
    New Member
    • Jul 2007
    • 14

    Question about variables

    Suppose I am creating a binary tree data structure using a double linked list. each node of the linked list will have an element and a pointer to two other nodes.

    In this instance my question is should I declare the element as a pointer like the pointers to the two other nodes or as a variable?

    My more general question is are there any conventions as to when to use a variable and when to use a pointer?

    Thanks for your tips.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Just out of curiosity, why are you using a doubly-linked list to implement your binary tree? I'm assuming this is one of your own implementation, but why not just use a map? I'm not sure you're using the right tool for the job.

    Comment

    • raistlinx
      New Member
      • Jul 2007
      • 14

      #3
      Originally posted by sicarie
      Just out of curiosity, why are you using a doubly-linked list to implement your binary tree? I'm assuming this is one of your own implementation, but why not just use a map? I'm not sure you're using the right tool for the job.

      http://www.fredosaurus.com/notes-cpp...ontainers.html
      Sorry. Typo. What I meant was doubly-linked nodes, not a list.

      In any case, it was just an example and the question is about variables/pointers.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by raistlinx
        Sorry. Typo. What I meant was doubly-linked nodes, not a list.

        In any case, it was just an example and the question is about variables/pointers.
        Think of it this way: the names and number of variables are determined at
        compile time; you don't know how many nodes your binary tree will have at
        runtime so you can't have a separate variable for each and every node. Simply
        use pointers that point to nodes in your tree.

        kind regards,

        Jos

        Comment

        • raistlinx
          New Member
          • Jul 2007
          • 14

          #5
          I am actually asking about the node here, not the whole tree so let me take a different approach. In the class below is there any reason I should define 'element' as a pointer instead? (i.e. Type* element)

          In fact, what I am really trying to do is ask a general question about when to use a variable and when to use a pointer. I just gave an example to hopefully make the question clearer.

          Code:
          template <class Type> 
          class DLNode : public Position<Type> {
          
          public:
          	DLNode(Type elem, DLNode* prev, DLNode* next){
          		data = elem;
          		prevNode = prev;
          		nextNode = next;
          	}
          
          	Type getElement();
          	void setElement(Type a);
          	DLNode* getNext();
          	void setNext(DLNode* node);
          	DLNode* getPrevious();
          	void setPrevious(DLNode* node);
          
          private:
          	Type element;
          	DLNode *prevNode;
          	DLNode *nextNode;
          };

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by raistlinx
            I am actually asking about the node here, not the whole tree so let me take a different approach. In the class below is there any reason I should define 'element' as a pointer instead? (i.e. Type* element)
            Your question is moot now because you have templatized the type of the
            element, i.e.'Type'. That 'Type' can be a pointer as well if you like when you
            instantiate your template. You have solved your own problem ;-)

            kind regars,

            Jos

            Comment

            • Matthew Page
              New Member
              • Jul 2007
              • 36

              #7
              In the case you first described (doubly linked list) I would use pointers for the children (in the case of a tree) or the neighbors. Why pointers here should be obvious. For the actual value, I would not use pointers. You could, but it would just add another layer if indirection and you would have to maintain the actual objects somewhere.

              Like I said, you COULD use either. There are cases where you would want to use a linked list, so you would have to decide based on what you are doing and the data needed for each node.

              Hope this helps a little bit.

              Comment

              • raistlinx
                New Member
                • Jul 2007
                • 14

                #8
                Originally posted by JosAH
                Your question is moot now because you have templatized the type of the
                element, i.e.'Type'. That 'Type' can be a pointer as well if you like when you
                instantiate your template. You have solved your own problem ;-)

                kind regars,

                Jos
                This is good to know! :-)

                Comment

                • raistlinx
                  New Member
                  • Jul 2007
                  • 14

                  #9
                  Originally posted by Matthew Page
                  In the case you first described (doubly linked list) I would use pointers for the children (in the case of a tree) or the neighbors. Why pointers here should be obvious. For the actual value, I would not use pointers. You could, but it would just add another layer if indirection and you would have to maintain the actual objects somewhere.

                  Like I said, you COULD use either. There are cases where you would want to use a linked list, so you would have to decide based on what you are doing and the data needed for each node.

                  Hope this helps a little bit.
                  Yeah. The thought that prompted the question was was imagining a huge class or series of classes where you have a mass of both pointers and variables and it gets hard to remember what is what.

                  Is it safe to say that basic types are most often left as variables while objects are pointers or is that too often not the case?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    You cannot use pointers in a tree. Changing the object pointed at may invalidate the tree structure.

                    You have to use objects in your nodes.

                    You work around this by having your node contain a constant key as an object and a non-const value as a pointer.

                    Now you can look up by a fixed key and get the current object contents.

                    Or, just use a map<> as was already suggested.

                    Comment

                    Working...