[英]Binary Tree static methods in Java
I have these instance methods in my Java implementation of Binary Tree and Search Binary Tree: getSize()
, getHeight()
, getDepth()
, getPreOrder()
, getInOrder()
, getPostOrder()
and getLevelOrder()
. 我在二进制树和搜索二进制树的Java实现中有这些实例方法: getSize()
, getHeight()
, getDepth()
, getPreOrder()
, getInOrder()
, getPostOrder()
和getLevelOrder()
。 These methods use the root of the tree in others recursive methods that have a parameter Node
. 这些方法在其他具有参数Node
递归方法中使用树的根。 Which is more appropiate to use from the point of view of the OOP: 从OOP的角度来看,哪个更适合使用:
Node
) that doesn't belong to the actual class, and they don't use any class attributes, 使用这些递归方法作为静态方法,因为它们使用不属于实际类的对象( Node
),并且它们不使用任何类属性, UtilsTree()
? 或者他们可能在其他静态类中,如UtilsTree()
? From an OOP perspective I believe approach number 2 is the way to go. 从OOP的角度来看,我认为第二种方法是可行的方法。 (Statics are in general often frowned upon in OOP.) (在OOP中,静态通常是不受欢迎的。)
As I understand it, the method uses this
as root, and then traverses the rest of the tree without calling any instance methods? 据我所知,该方法使用this
作为root,然后遍历树的其余部分而不调用任何实例方法? This isn't too bad considering that the other nodes are of the same class, which means that the code is already shared among all objects. 考虑到其他节点属于同一类,这并不是太糟糕,这意味着代码已经在所有对象之间共享。 (The method can access private members of other instances and so forth.) (该方法可以访问其他实例的私有成员,等等。)
That being said, I think getSize
, getHeight
, getDepth
, getPreOrder
, getInOrder
, getPostOrder
and getLevelOrder
can all be implemented as proper recursive instance methods. 话虽这么说,我认为getSize
, getHeight
, getDepth
, getPreOrder
, getInOrder
, getPostOrder
和getLevelOrder
都可以作为正确的递归实例方法实现。 (Correct me if I'm wrong.) (如我错了请纠正我。)
A fourth option which isn't too bad would be to use a visitor pattern and have a NodeVisitor
interface. 第四个不太糟糕的选择是使用访问者模式并具有NodeVisitor
接口。
Some of those methods definitely should be non-static members since they relate directly to a specific instance. 其中一些方法肯定应该是非静态成员,因为它们直接与特定实例相关。
tree.getSize()
or tree.getDepth()
is much easier to read and understancd than BinaryTree.getDepth(tree)
. tree.getSize()
或tree.getDepth()
比BinaryTree.getDepth(tree)
更容易阅读和BinaryTree.getDepth(tree)
。
However one could argue that the methods getPreOrder()
, getInOrder()
, getPostOrder()
can be static or even in their own class. 然而,有人可能会争辩说getPreOrder()
, getInOrder()
, getPostOrder()
可以是静态的,甚至可以在它们自己的类中。 You can think of that as a StrategyPattern
for how to walk the tree. 您可以将其视为如何走树的StrategyPattern
。
TraversalStrategy.PREORDER.walk(tree); TraversalStrategy.PREORDER.walk(树);
might be a good idea instead of adding more methods. 可能是个好主意而不是添加更多方法。 That way, if you ever need to add different ways to walk to the tree you don't have to keep adding methods to BinaryTree
(Following the Open-Closed Principle) 这样,如果你需要添加不同的方式来走到树,你不必继续向BinaryTree
添加方法(遵循开放 - 封闭原则)
A class
represents a pattern for a set of Object
s. class
表示一组Object
的模式。 A class
can have static
members and methods, that is, class
-level properties and abilities. 一个class
可以有static
成员和方法,即class
-level属性和异能。 An instance of a class is an Object
, which has its particular, non- static
members and methods. 类的实例是Object
,它具有特定的非static
成员和方法。 In your case, we are talking about a bunch of getters. 在你的情况下,我们谈论的是一堆吸气剂。 The key question is: 关键问题是:
whose are those methods? 那些方法是谁?
and the answer is: they are methods, that is, abilities of your Tree
Object
. 答案是:它们是方法,即Tree
Object
能力。 If you have several Tree
s, the other Tree
s have nothing to do with the size and stuff of a given Tree
. 如果你有几个Tree
,那么另一个Tree
与给定Tree
的大小和东西无关。 As a result, all the mentioned methods should be public
, instance-level methods unless you want to use them internally, in which case the affected methods should be protected
, non- static
methods. 因此,除非您想在内部使用它们,否则所有提到的方法都应该是public
实例级方法,在这种情况下,受影响的方法应该受到protected
,非static
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.