簡體   English   中英

如何計算二叉樹中“唯一子”節點的數量?

[英]How do I calculate the number of “only child”-nodes in a binary tree?

注意:這是與作業相關的,但我沒有將其標記為因為“家庭作業”標簽被標記為obselete(?)

使用以下實現二叉樹的類...

class TreeNode
{
  private Object value; 
  private TreeNode left, right;

   public TreeNode(Object initValue)
  { 
     value = initValue; 
     left = null; 
     right = null; 
  }

   public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight)
  { 
     value = initValue; 
     left = initLeft; 
     right = initRight; 
  }

   public Object getValue()
  { 
     return value; 
  }

   public TreeNode getLeft() 
  { 
     return left; 
  }

   public TreeNode getRight() 
  { 
     return right; 
  }

   public void setValue(Object theNewValue) 
  { 
     value = theNewValue; 
  }

   public void setLeft(TreeNode theNewLeft) 
  { 
     left = theNewLeft;
  }

   public void setRight(TreeNode theNewRight)
  { 
     right = theNewRight;
  }

}

我需要計算二進制樹中“只有子節點”的節點數,這被定義為一個節點,它沒有源自其父節點的另一個節點。

這是我到目前為止:

public static int countOnlys(TreeNode t)
  {
     if(t == null)
         return 0;
     if(isAnOnlyChild(t))
         return 1;
     return countOnlys(t.getLeft()) + countOnlys(t.getRight());
  }

我不知道如何實現boolean方法isAnOnlyChild(TreeNode t)

有人可以幫我嗎?

你非常接近並且遍歷看起來很好但是在你的Treenode中你沒有孩子和它的父母之間的聯系。 因此,您無法說出一個左孩子是否存在兄弟姐妹(右孩子)。

您可以擁有父級Treenode(以及左側和右側),以便您可以檢查給定節點的父級有多少個孩子。 或如ajp15243建議的那樣,改用一種檢查給定節點有多少個子節點的方法。

后者的一些偽代碼:

//we still need to check if that only child has its own children
if hasOnlyChild(t) 
      return 1 + checkOnlys(left) + checkOnlys(right)
else 
      return checkOnlys(left) + checkOnlys(right)

正如您已經注意到的,一種解決方案是計算只有一個孩子的父母的數量。 這應該工作:

public static int countOnlys(TreeNode t)
  {
     if(t == null || numberOfChildren(t)==0){
         return 0;
     }
     if(numberOfChildren(t)==1){
         return 1+ countOnlys(t.getLeft()) + countOnlys(t.getRight());
     }
         if(numberOfChildren(t)==2 ){
         return countOnlys(t.getLeft()) + countOnlys(t.getRight());
    }
         return 0;
  }

public static int numberOfChildren (TreeNode t){
    int count = 0;
    if(t.getLeft() != null ) count++;
    if(t.getRight() != null) count++;       
    return count;   
    }

如果其中一個子項非空(這意味着它的一個子項為空),則父項具有唯一的子項:

((t.getLeft() == null || t.getRight() == null)) && !(t.getLeft() == null && t.getRight() == null)

但是,當遞歸代碼遍歷樹時,您無需測試該節點。 (這類似於“訪客”模式。)您要做的是在您坐在父母的陪伴下測試唯一的孩子。 它實際上是一個邏輯異或 - 測試,因為只有一個子節點需要非null才能檢測到該節點只有一個子節點。

所以算法是

  1. 訪問樹中的每個節點。
  2. 如果節點只有一個孩子,請計算它

而已。 其余的都很好。

無論何時遍歷二叉樹,都要遞歸思考。 這應該工作。

public static int countOnlys(TreeNode t)
  {
     if(t == null)
         return 0;
     if (t.getLeft()==null&&t.getRight()==null)
         return 1;

     return countOnlys(t.getLeft())+countOnlys(t.getRight());

  }
public static int onlyChild(TreeNode t){
    int res = 0;
    if( t != null){
        // ^ means XOR 
        if(t.getLeft() == null ^ t.getRight() == null){
            res = 1;
        }

        res += onlyChild(t.getLeft()) + onlyChild(t.getRight()));
    }

    return res;
}
public int countNode(Node root) {

         if(root == null)
             return 0;

         if(root.leftChild == null && root.rightChild == null)
             return 0;
         if(root.leftChild == null || root.rightChild == null)
             return 1 + countNode(root.leftChild) + countNode(root.rightChild);
         else
             return countNode(root.leftChild) + countNode(root.rightChild);

     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM