Andy897
Andy897

Reputation: 7133

Size method for binary trees

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.

Upvotes: 8

Views: 39297

Answers (4)

porfiriopartida
porfiriopartida

Reputation: 1556

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 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.

Upvotes: 1

kailash gaur
kailash gaur

Reputation: 1447

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.

Upvotes: 4

Daniel Imms
Daniel Imms

Reputation: 50189

One is public and 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).

This concept is called encapsulation and 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).

Upvotes: 5

rgettman
rgettman

Reputation: 178263

The size method that takes a Node 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's private.

The other size method 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 size method, passing the root to get the size of the entire tree. It's very useful outside of the class, so it's public.

Upvotes: 3

Related Questions