Elimination of globals?

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

A few solutions:

  • Each node can directly store a link to the root (discards memory)
    • I can calculate the root at runtime by "rising" (waste loop)
    • I can use static fields (but this is global as well)

Can someone provide a design that does not use global (in any case), but is more efficient than # 1 or # 2 respectively in memory or loops?

Edit: Since I have a set of trees, I cannot just store it in statics, as it would be difficult to distinguish between trees. (thanks maccullt)

+3
source share
6

, node .

: :

  • node
  • ( , )

, , 5.

+6

? , , .

: . , " ". , . , , .

, , . + , . , ( ) . Process Explorer.

: , , node, 55 . . , O (1). , O (m * n), FindNodeByID.

+3

. - , root.

+2

№ 1 - . # 2 - . , , ? , "", ?

№2. , -, , , , - . , - , . (, , node , , ?) , .

+1

TreeView, singleton. , , , , , .

0

Ignoring the aversion to inner classes, I could define a tree class and define nodes as inner classes. Each of the nodes will have access to its tree, including its root.

This may turn out to be the same as # 1 depending on how Java associates nodes with parents. (I'm not sure, and I will have to comment on it)

0
source

Source: https://habr.com/ru/post/1696911/


All Articles