【问题标题】:Summing up all nodes总结所有节点
【发布时间】:2010-09-18 17:19:45
【问题描述】:

这可能是一个简单的修复 - 但我试图将二叉搜索树上的所有节点(来自 Node 类的 Size 属性)加在一起。到目前为止,在我的 BST 类下面,我有以下内容,但它返回 0:

    private long sum(Node<T> thisNode)
    {
        if (thisNode.Left == null && thisNode.Right == null)
            return 0;
        if (node.Right == null)
            return sum(thisNode.Left);
        if (node.Left == null) 
            return sum(thisNode.Right);


        return sum(thisNode.Left) + sum(thisNode.Right);
    }

在我的 Node 类中,我有 Data,它将 Size 和 Name 存储在它们的给定属性中。我只是想总结整个大小。有什么建议或想法吗?

【问题讨论】:

    标签: c# recursion tree subtree


    【解决方案1】:

    也许你的意思是

        if (thisNode.Left == null && thisNode.Right == null)
            return thisNode.Size;
    

    ?

    【讨论】:

      【解决方案2】:

      这是因为当您到达叶节点时返回零。您应该返回存储在该叶节点中的大小。

      此外,如果您的非叶节点也有大小,那么您也需要对它们进行处理:

      private long sum(Node<T> thisNode)
      {
          if (thisNode.Left == null && thisNode.Right == null)
              return thisNode.Size;
          if (node.Right == null)
              return thisNode.Size + sum(thisNode.Left);
          if (node.Left == null) 
              return thisNode.Size + sum(thisNode.Right);
          return thisNode.Size + sum(thisNode.Left) + sum(thisNode.Right);
      }
      

      如果您的非叶节点没有大小,请使用:

      private long sum(Node<T> thisNode)
      {
          if (thisNode.Left == null && thisNode.Right == null)
              return thisNode.Size;
          if (node.Right == null)
              return sum(thisNode.Left);
          if (node.Left == null) 
              return sum(thisNode.Right);
          return sum(thisNode.Left) + sum(thisNode.Right);
      }
      

      第一个更优雅的版本是:

      private long sum(Node<T> thisNode)
      {
          if (thisNode == null)
              return 0;
          return thisNode.Size + sum(thisNode.Left) + sum(thisNode.Right);
      }
      

      【讨论】:

      • Node 类不包含 Size 属性 - 相反,它位于我调用并在 Form 上实例化的另一个类中。例如,在我的表单上: NameAndSize obj_NS = new NameAndSize("Name", 320);然后以我调用 sum() 的形式返回所有对象的总大小。
      • 那么你希望总和会如何神奇地出现?你不能从节点访问包含对象的 size 属性吗?
      • 你说得对……显然无法访问,所以我只是做了一些小修改,就搞定了!
      【解决方案3】:

      怎么样

      private long Sum(Node<T> thisNode)
      {
        if( thisNode == null )
          return 0;
      
        return thisNode.Size + Sum(thisNode.Left) + Sum(thisNode.Right);
      }
      

      如果节点本身没有 size 属性,那这个呢?

          public class Node<T>
          {
              public T Data;
              public Node<T> Left;
              public Node<T> Right;
      
              public static void ForEach(Node<T> root, Action<T> action)
              {
                  action(root.Data);
      
                  if (root.Left != null)
                      ForEach(root.Left, action);
                  if (root.Right != null)
                      ForEach(root.Right, action);
              }
          }
      
          public interface IHasSize
          {
              long Size { get; }
          }
      
          public static long SumSize<T>(Node<T> root) where T : IHasSize
          {
              long sum = 0;
              Node<T>.ForEach(root, delegate(T item)
              {
                  sum += item.Size;
              });
              return sum;
          }
      

      【讨论】:

        【解决方案4】:

        试试这个:

            private long sum(Node<T> thisNode)
            {
                if (thisNode == null)
                    return 0;
                return thisNode.Size + sum(thisNode.Left) + sum(thisNode.Right);
            }
        

        原始代码返回的唯一“值”是 0 - 这就是结果始终为 0 的原因。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多