I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.
So basically every time you keep adding a node to the tree you tail pointer gets updated to point to the last node. I have this so far.
I really appreciate your help.
Now, that doesn't appear to be how linked lists work. You have the List object and it has a start node. When you create the List it will be empty so start defaults to null. If you have an end reference too (that will be null initially as well), that sounds like a doubly‑linked list. It is usually easiest to understand of you have a diagram, like that in this Wikipedia page.
Linked Lists and trees are very similar. The only difference is that nodes in a tree have more than one branch arising from them. In which case you cannot always have one “end” node. In which case it cannot be a static field.
Are you saying then, if I specify the tail as private static and every time I call insert function the tail will be updated with the new node being inserted to the Tree and therefore tail will always be up to date with the last node being inserted?
Harish Shivaraj wrote:I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.
Probably a different question, but:Why?
There's a big difference between a Tree, and a Node in a Tree; and while there may be a good reason for each Node to know the last Node in its LIST; it's normally regarded as redundant information.
Reason: Yes, it allows you to get to the end of the list from anywhere in the list; but why would you want to do that? It's simply not a normal requirement. And even if it was, it would probably be simpler (and more flexible) to have each Node include its List.
But trust me, it isn't. A Node is a "piece" of a List, and it should be concerned only with its "neighbours". If it's forward-linked, then it should have a "next" link; if it's "double"-linked, then it will also need a "prev[ious]" link. Keeping track of "head" and "tail" Nodes is normally the job of the List, not of each Node.
What the comment on this line says is most probably wrong. Why would all instances of this class share a reference to tail?! The last node added is specific to each instance; that is, if I had tree1, tree2, and tree3 and added different items to each of them, I would expect each tree to have a different tail. If you declare tail as static then you will end up with trees that reference a tail node that is really in another tree altogether. That would be wrong.
Harish Shivaraj wrote: . . . if I specify the tail as private static and every time I call insert function the tail will be updated with the new node being inserted to the Tree and therefore tail will always be up to date with the last node being inserted?
If you have two trees, each will record the most recent node in either.