Doing away with Globals?

I have a set of tree objects with a depth somewhere in the 20s. Each of the nodes in this tree needs access to its tree's root.

A couple of solutions:

  1. Each node can store a reference to the root directly (wastes memory)
  2. I can compute the root at runtime by "going up" (wastes cycles)
  3. I can use static fields (but this amounts to globals)

Can someone provide a design that doesn't use a global (in any variation) but is more efficient that #1 or #2 in both memory or cycles respectively?

Edit: Since I have a Set of Trees, I can't simply store it in a static since it'd be hard to differentiate between trees. (thanks maccullt)

Answers


Pass the root as a parameter to whichever functions in the node that need it.

Edit: The options are really the following:

  1. Store the root reference in the node
  2. Don't store the root reference at all
  3. Store the root reference in a global
  4. Store the root reference on the stack (my suggestion, either visitor pattern or recursive)

I think this all the possibilities, there is no option 5.


Need Your Help

Renaming a file without using renameTo() - Java

java file rename symlink

Disregarding my last post, I've found the source of the problem. I'm using

my code hangs but when i debug it it never hangs

java client transfer hang

i have a java code that SOMETIMES hangs when i run it.Its a put command that puts through sockets( and input stream etc a file from a server to a client) . Sometimes this works but sometimes it doe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.