【问题标题】:Finding Maximum of n-ary tree寻找 n 叉树的最大值
【发布时间】:2012-11-02 06:53:00
【问题描述】:

我有一个由短 NTN 类定义的 n 叉树

public class NTN P {
     public int value;
     public Set<NTN> children;
 }

我想找到这样一棵 n 叉树的最大值。假设它是一个简单的整数 n 叉树,其值为: [parent: 1 children: 2, 3, 4] [parent: 2 children: 5, 6] [parent: 4 children 7, 8, 9] 最大值只需 9。我不知道如何开始编写一个方法来找到原型的最大值:

public static int maximum(NTN t);

根据我的尝试:

public static int maximum(NTN t) {
  int max = 0;
      for (NTN e : t.children) {
          if (e.value > max)
              max = e.value;
      }

  return max;
}

上面的代码最多返回 4,这意味着它只检查 t 的子代,而不是后续的一组子代。在这种情况下,它不会检查 4, [7,8,9] 和 2, [5,6] 的子集。如何更改它以便该方法找到所有后续子项的最大值?

【问题讨论】:

    标签: java algorithm recursion tree


    【解决方案1】:
    public static int maximum(NTN t) {
      int max = t.value;
      Set<NTN> children = t.children;
    
      // only check if this node is not a leaf
      if (children != null && !children.isEmpty) {
        for (NTN e : children) {
          // here is the recursion
          int maxNext = maximum(e);
    
          if (maxNext > max) max = maxNext;
        }
      }
    
      return max;
    }
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      您的解决方案不是Recursive,因此它不会在您的子孩子中继续存在,如果有的话。你可能想看看Tabu SearchHill Climbing 是一种更简单的方法(但容易陷入local maximum)。

      【讨论】:

        【解决方案3】:

        我想这样的事情会有所帮助,你在树上做一个 DFS 来遍历所有节点,然后查看每个节点的值,如果它大于你作为静态变量保留的最大值您是公共类(例如 public static int max),您将 max 设置为该节点的值,这样就可以了(希望未经过测试,请注意返回类型为 void 并且您直接更新公共内部的变量类):

             public void maximum(NTN T){
                    if (!T.children.isEmpty() && T.children != null){
                        for(NTN e: T.children)
                            maximum(e)
                    }
                    if (PUBLIC_CLASS.MAX < T.value)
                        PUBLIC_CLASS.MAX = T.value;
                }
        

        【讨论】:

          【解决方案4】:
          public static int maximum(NTN t) {
              int max = t.value;
              Set<NTN> children = t.children;
              if (children != null && !children.isEmpty) {
                  for (NTN e : children) {
                      max = Math.max(max, maximum(e));
                  }
              }
          }
          

          【讨论】:

            【解决方案5】:

            以下代码从 nary 树中返回具有最大值的完整节点

            // 下面是给定的树节点结构。

            模板

            class TreeNode
             {
                public:
                    T data;
                    vector<TreeNode<T>*> children;
            
                    TreeNode(T data) {
                        this->data = data;
                    }
            
                    ~TreeNode() {
                        for (int i = 0; i < children.size(); i++) {
                            delete children[i];
                        }
                    }
             };
            
            
            
            TreeNode<int>* maxDataNode(TreeNode<int>* root) {
            
                if(root == NULL) return NULL;
                TreeNode<int>* maxNode = new TreeNode<int>(0);
                int max = 0;
            
                for(int i=0; i<root->children.size();i++){
                    if(maxDataNode(root->children[i])->data > max)
                    {    
                        max = maxDataNode(root->children[i])->data;
                        maxNode = maxDataNode(root->children[i]);
                    }
                }
                if(root->data > maxNode->data)
                {
                    maxNode = root;
                    return maxNode;
                }
                return maxNode;
            
            }
            

            【讨论】:

              猜你喜欢
              • 2011-06-03
              • 2015-10-07
              • 2021-11-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-11-28
              相关资源
              最近更新 更多