Size Method for Binary Trees

I just came across this code to find the 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 this is so that there are two methods and one without an argument. I can guess that this is a good practice, but I am not able to think about the reason.

+7
java oop binary-tree
source share
4 answers

OOP offers you to write your business logic in a private method. According to my method, the size of a view with a parameter is private, and the logic for counting the size is here, so no other function (outside the class) can change or access your logic (through inheritance). You use a different size to return this size method, which has a public modifier, and another user will use this class to get the size, basically the other user does not know how you calculate the size.

+4
source share

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

This concept is called encapsulation and is an act of hiding internal parts that should not be shared, in order to simplify the use of a class (or library).

+4
source share

The size method that Node accepts is implemented recursively - it finds the size of the tree from that Node down. This is not useful outside the binary tree class itself, so it is private .

Another size method finds the size of the entire tree, and callers do not need to pass to Node ; the binary tree already knows what its root is. This is not recursive. It delegates another size method, passing root to get the size of the whole tree. This is very useful outside the class, so it is public .

+3
source share

As well as the private argument, this means that I can only use something like

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

Typically, the code may have comments to know what they are intended for. Sometimes 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 a binary tree can also be treated as binary trees.

Binary tree sample

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

 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 did you use a method with the same name and different arguments ?

There may be several reasons for doing this or not doing it. This usually means that there are several ways to do something or that you want to use something else by default, in which case size () will be used as the default root.

+1
source share

All Articles