Static binary tree methods in Java

I have these instance methods in my Java implementation of the binary tree and binary search tree: getSize() , getHeight() , getDepth() , getPreOrder() , getInOrder() , getPostOrder() and getLevelOrder() . These methods use the root of the tree in other recursive methods that have a Node parameter. What is more appropriate to use in terms of OOP:

  • Using these recursive methods as static methods, since they use an object ( Node ) that does not belong to the actual class, and they do not use any class attributes,
  • They can be instances because they can be used in the subtree of this tree, and they do not use any static attributes,
  • or can they be in another static class, for example UtilsTree() ?
+6
source share
3 answers

From the OOP point of view, I think approach number 2 is the way to go. (Statics in general is often disapproving in OOP.)

As I understand it, a method uses this as root and then iterates through the rest of the tree without calling any instance methods? This is not so bad considering that the other nodes are of the same class, which means that the code is already used for all objects. (The method can access private members of other instances, etc.)

I believe that getSize , getHeight , getDepth , getPreOrder , getInOrder , getPostOrder and getLevelOrder can be implemented as appropriate recursive instance methods. (Correct me if I am wrong.)

The fourth option, which is not so bad, is to use a visitor template and have a NodeVisitor interface.

+3
source

Some of these methods should definitely be non-stationary members, as they relate directly to a particular instance.

tree.getSize() or tree.getDepth() much easier to read and understand less BinaryTree.getDepth(tree) .

However, it can be argued that the methods getPreOrder() , getInOrder() , getPostOrder() can be static or even in their own class. You can think of it as StrategyPattern about how to walk on a tree.

TraversalStrategy.PREORDER.walk (tree);

might be a good idea instead of adding extra methods. Thus, if you ever need to add different ways to navigate to a tree, you do not need to add methods to BinaryTree (following the principle of open closure)

+1
source

A class represents a template for a collection of Object s. A class can have static elements and methods, that is, the properties and abilities of class -level. An instance of a class is an Object that has its own specific, non- static elements and methods. In your case, we are talking about a bunch of getters. Key question:

which are these methods?

and the answer is: these are methods, i.e. abilities of your Tree Object . If you have multiple Tree s, other Tree have nothing to do with the size and material of this Tree . As a result, all the methods mentioned must be public , instance-level methods, if you do not want to use them internally, in which case the affected methods must be protected , not static .

0
source

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


All Articles