E anche quella con argomento è privata, ciò significa che posso solo usare qualcosa di simile
MyBinaryTree bt = new MyBinaryTree();
int treeSize = bt.size();
solito codice può avere commenti per sapere quello che sono pensati per. A volte un codice pulito non ha nemmeno bisogno di commenti.
/**
* 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 teoria, tutti i rami con i bambini nell'albero binario possono anche essere gestiti come alberi binari.
noti che size()
chiamerà la seconda con nodo radice come argomento, in questo caso significa conteggio a partire da una, internamente sarà.
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
Ora, perché dovresti usare uno method with the same name and diferent arguments?
Ci possono essere alcuni motivi per farlo o non farlo. Di solito significa che ci sono più modi per fare qualcosa o che vuoi usare qualcos'altro come predefinito, in questo caso size() userà come root di default.