简体   繁体   English

Java中的二进制树静态方法

[英]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的角度来看,哪个更适合使用:

  1. Using these recursive methods as static methods, because they use an object ( Node ) that doesn't belong to the actual class, and they don't use any class attributes, 使用这些递归方法作为静态方法,因为它们使用不属于实际类的对象( Node ),并且它们不使用任何类属性,
  2. They can be instance methods because they can use in a subtree of this tree and they don't use any static attributes, 它们可以是实例方法,因为它们可以在此树的子树中使用,并且它们不使用任何静态属性,
  3. or they may be in other static class like 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. 话虽这么说,我认为getSizegetHeightgetDepthgetPreOrdergetInOrdergetPostOrdergetLevelOrder都可以作为正确的递归实例方法实现。 (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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM