0

そこで、教授のバイナリ ツリー クラスに findHeight メソッドを追加しようとしていますが、少し問題があります。

  BTNode<T> findParent(BTNode<T> Root, BTNode<T> child)
  {
    if(!root)  
    {
        return Null;
    }

    if(Root.left* == child || Root.right* == child)
    {
        return Root;
    }
    else
    {
        //recursively checks left tree to see if child node is pointed to
        findParent(Root.left, child);

        //recursively checks right tree to see if child node is pointed to
        findParent(Root.right, child);
    }

  int findHeight(BTNode<T> thisNode)
  {
      if (Count.findParent(.root, thisNode) == null)    
      {
            return 0;
      }

      return findHeight(thisNode.findParent) + 1;
  } 

私の問題は、findHeight メソッドで findParent() メソッドが呼び出され、パラメータ thisNode が由来するバイナリ ツリーのルートを参照する必要があることです。これはクラスの一部にすぎないため、わかりませんルートを参照する方法。BT(バイナリツリー)クラスにはツリーのルートを返す関数がありますが、参照するバイナリツリーがないのでどうすればいいのかわかりません。助けてください!!!

4

1 に答える 1

1

通常、findHeight関数はツリーのルートを見つけることを「心配」しません。渡されたノードの下にあるツリーの高さを見つけるだけです。通常は次のようになります。

int findHeight(BTNode <T> *thiNode) { 
    if (thisNode == NULL)
        return 0;
    int left_height = findHeight(thisNode->left);
    int right_height = findHeight(thisNode->right);
    return 1 + max(left_height, right_height);
}

次に、必要な高さのツリーのルートを渡すのはユーザー次第です。

于 2012-05-04T17:12:42.393 に答える