私はLinkedBinaryTreeクラスのために最もエレガントなisEmpty()メソッドを作成しようとしています。最もエレガントなisEmpty()バイナリツリーのメソッド
コンパイラがifステートメントと対話するときは、ブール値またはfalseのいずれかを返す必要があります。しかし、の外にの値を返す必要があるので、私はfalseの値を返しました。私のロジックはif(count == 0)で、isEmpty()はtrueを返し、2行目で気にしません(falseを返します)。これは正しいです?
public class LinkedBinaryTree<T> implements BinaryTreeADT<T>
{
protected int count;
protected BinaryTreeNode<T> root;
/**
* Creates an empty binary tree.
*/
public LinkedBinaryTree()
{
count = 0;
root = null;
}
/**
* Creates a binary tree with the specified element as its root.
*
* @param element the element that will become the root of the new binary tree
*/
public LinkedBinaryTree (T element)
{
count = 1;
root = new BinaryTreeNode<T> (element);
}
/**
* Returns true if this binary tree is empty and false otherwise.
*
* @return true if this binary tree is empty
*/
public boolean isEmpty()
{
if (count == 0);
return false;
}
せずにあなたの状態を返すことができます。セミコロンで終了する 'if'文は無視されますが、メソッドは常に' false'を返します。また、 'count'はあなたの文脈では役に立たない。ルートが定義されているときには1に増やされるので、 'if(root == null)はtrueを返します。それ以外の場合はfalseを返す '、より短い、' return root == null' – BackSlash
'if(count == 0);'は技術的に何もしません。あなたは何をしようとしているのですか。たぶん 'return count == 0;'? –
if文で返すことができます。あなたはまた、すべてのパスが戻ってくることを確認しなければならないし、それは間違っていたように聞こえる。 – user1675642