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.
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
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.
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
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. - 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;
-
};
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
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.
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! :-)
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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. ...
|
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)
...
|
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....
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |