473,505 Members | 16,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about variables

14 New Member
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.
Jul 20 '07 #1
9 1297
sicarie
4,677 Recognized Expert Moderator Specialist
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
Jul 20 '07 #2
raistlinx
14 New Member
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.
Jul 20 '07 #3
JosAH
11,448 Recognized Expert MVP
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
Jul 20 '07 #4
raistlinx
14 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. template <class Type> 
  2. class DLNode : public Position<Type> {
  3.  
  4. public:
  5.     DLNode(Type elem, DLNode* prev, DLNode* next){
  6.         data = elem;
  7.         prevNode = prev;
  8.         nextNode = next;
  9.     }
  10.  
  11.     Type getElement();
  12.     void setElement(Type a);
  13.     DLNode* getNext();
  14.     void setNext(DLNode* node);
  15.     DLNode* getPrevious();
  16.     void setPrevious(DLNode* node);
  17.  
  18. private:
  19.     Type element;
  20.     DLNode *prevNode;
  21.     DLNode *nextNode;
  22. };
Jul 20 '07 #5
JosAH
11,448 Recognized Expert MVP
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
Jul 20 '07 #6
Matthew Page
36 New Member
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.
Jul 20 '07 #7
raistlinx
14 New Member
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! :-)
Jul 20 '07 #8
raistlinx
14 New Member
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?
Jul 20 '07 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
339
by: Rhino | last post by:
Is there any way to invoke and perform a given subroutine within an SQL procedure several different times, doing other work in between invocations of the subroutine? For instance, let's say that...
4
3598
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
7
1345
by: Bob Morvay | last post by:
I am trying to determine how far I should go in encapsulating data. As I understand it, OO practices state to create private member variables and use these variables in your publicly accessible...
14
2164
by: Coleen | last post by:
Hi All :-) We have an APSX application using VB.net as the code behind, which uses one or two session variables per page. These Session variables are passed to the final page and calculations...
8
1223
by: Florian Daniel Otel | last post by:
Hello all, As the subject says, I am a newcomer to Python and I have a newcomer question wrt namespaces and variable scope. Obviously, I might be missing smth obvious, so TIA for the patience...
4
343
by: Diffident | last post by:
Hello All, I have a question which is pertinent to Page's lifecycle. I declared a protected static object (global variable within that class) whose value is set only once when the page is...
2
1740
by: Michaelk | last post by:
Can somebody tell me how many Session variables would be considered exessive, and when they start really affect the server speed. For example on 20-30 asp pages I need to use about 200-300 session...
2
1449
by: DLN | last post by:
Hello all, I apologize for the naivety of this question, but I'm wondering whether session variables can ever be modified (somehow) by a client without having to go through code that I write. ...
16
1348
by: Donn Ingle | last post by:
Hello, Is there a way I can, for debugging, access the instance variable name from within a class? E.g: Class X: def debug(self): print "My instance var is %s" % (some magic Python stuff) ...
12
2995
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.