Java 二叉树的大小方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19166510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:40:24  来源:igfitidea点击:

Size method for binary trees

javaoopbinary-tree

提问by Andy897

I just came across this code for finding size of a Binary tree.

我刚刚遇到了这个用于查找二叉树大小的代码。

public int size() {
  return(size(root)); 
}
private int size(Node node) { 
  if (node == null) return(0); 
  else { 
    return(size(node.left) + 1 + size(node.right)); 
  } 
} 

I am confused why is it so that there are two methods and one without argument. I can guess that it is some good practice but not able to think of the reason.

我很困惑为什么会有两种方法,一种没有参数。我可以猜测这是一些很好的做法,但无法想到原因。

采纳答案by kailash gaur

OOPs suggests that you should write your business logic in private method.As per my view size method with parameter is private and you logic for counting size is here so no other on(outside the class) can modify or access your logic(through inheritance).You are using another size for returning this size method which has public modifier and other user will use that class for getting size basically other user don't know how you are calculating the size.

OOPs 建议你应该在私有方法中编写你的业务逻辑。根据我的视图大小方法,参数是私有的,你计算大小的逻辑在这里,所以没有其他人(类外)可以修改或访问你的逻辑(通过继承) .您正在使用另一个大小来返回此大小方法,该方法具有公共修饰符,其他用户将使用该类来获取大小,基本上其他用户不知道您如何计算大小。

回答by rgettman

The sizemethod that takes a Nodeis implemented recursively -- it finds the size of the tree from that Nodedown. This is not useful outside the binary tree class itself, so it's private.

size采用 a的方法Node是递归实现的——它从那个Node向下找到树的大小。这在二叉树类本身之外没有用,所以它是private.

The other sizemethod finds the size of the entire tree, and callers should not have to pass in a Node; the binary tree already knows what its root is. It's not recursive. It delegates to the other sizemethod, passing the rootto get the size of the entire tree. It's very useful outside of the class, so it's public.

size一种方法查找整个树的大小,调用者不应该传入一个Node; 二叉树已经知道它的根是什么。它不是递归的。它委托给另一个size方法,传递root以获取整个树的大小。它在课堂之外非常有用,所以它是public.

回答by Daniel Imms

One is publicand one is private. So one is exposed and used externally without any parameters public int size(), and the other is only used internally and hidden externally private int size(Node).

一个是public,一个是private。所以一个是暴露在外部使用,没有任何参数public int size(),另一个只在内部使用,隐藏在外部private int size(Node)

This concept is called encapsulationand is the act of hiding internal details that don't need to be exposed for general consumption in an effort to simplify the usage of the class (or library).

这个概念称为封装,是隐藏内部细节的行为,这些细节不需要为一般消费而公开,以简化类(或库)的使用。

回答by porfiriopartida

And also the one with argument is private, that means that I can only use something like

而且有参数的那个是私有的,这意味着我只能使用类似的东西

MyBinaryTree bt = new MyBinaryTree();
int treeSize = bt.size();

Usually code can have comments to know what they are meant for. Sometimes a clean code does not even need comments.

通常代码可以有注释以了解它们的含义。有时,干净的代码甚至不需要注释。

/**
* Gets the size of the current binary tree.
*/
public int size() {
  return(size(root)); 
}
/**
* Gets the size of the given branch
* @param node The branch to count from.
*/
private int size(Node node) { 
  if (node == null) return(0); 
  else { 
    return(size(node.left) + 1 + size(node.right)); 
  } 
} 

In theory, all branches with children in the binary tree can also be handled as binary trees.

理论上,二叉树中所有有子树的分支也可以作为二叉树处理。

Binary Tree sample

二叉树示例

Note that size()will call the second one with root Node as argument, in this case it means count starting from A, internally it will be.

请注意,size()将使用根节点作为参数调用第二个,在这种情况下,它意味着从 A 开始计数,在内部它将是。

Size of the tree is count of items from A
Items from A are 1 + Items from B + Items from C
Items from B are 1
Items from C are 1 + Items from D + items from E

Now, why would you use a method with the same name and diferent arguments?

现在,为什么要使用具有相同名称和不同参数的方法

There may be few reasons to do it or not to do it. Usually it means that there are more than one way to do something or that you want to use something else as default, in this case, size() will use as default root.

做或不做的原因可能很少。通常这意味着有不止一种方法可以做某事,或者您想使用其他方法作为默认值,在这种情况下, size() 将用作默认根。