【问题标题】:Find child in non-binary tree (recursively)在非二叉树中查找孩子(递归)
【发布时间】:2016-02-24 08:08:21
【问题描述】:

我有 TreeNode 类——实现非二叉树的节点(List<TreeNode> children)。

我需要在它的子节点中找到具有给定数据的第一个节点。我写了一些方法,但显然有一些问题(java.lang.AssertionError: Failed to find a child with not-null data: expected:<2> but was:<null>)。 (如果数据为空,我需要用空数据返回第一个孩子)。

public TreeNode findChild(Object data) {


    if (data == null) {
        Iterator<TreeNode> a = getChildrenIterator();
        TreeNode tmp;
        while (a.hasNext()) {
            tmp = a.next();
            if (tmp.getData()==null) return tmp;
            tmp.findChild(data);
        }
    }else
    {
        Iterator<TreeNode> a = getChildrenIterator();
        TreeNode tmp;
        while (a.hasNext()) {
            tmp = a.next();
            if (data.equals(tmp.getData())) return tmp;
            tmp.findChild(data);
        }
    }

    return null;
}

【问题讨论】:

  • 可能是你的树太大了,因为你(可能)每个节点都有很多递归调用。
  • @dbrown93 抱歉,现在还有一个错误
  • 如果tmp.getData() 不匹配,则重复出现。递归是死代码,因为你不对结果做任何事情,while 循环继续。
  • @Sylwester 请告诉我我需要在这里修复什么

标签: java algorithm recursion tree


【解决方案1】:

您的递归不正确。你应该返回tmp.findChild()的结果如果它返回一个非空值。

您还需要考虑是否应该实施深度优先或广度优先搜索。

【讨论】:

    【解决方案2】:

    问题在于您没有返回递归调用的结果。 也许下面的代码会有所帮助:

    import java.util.*;
    
    public class TreeNode
    {
      // Constructor
      public TreeNode()
      {
       children = new ArrayList<TreeNode>();
       node_data = null;
      }
    
      // Get node's data
      public Object getData()
      {
        return (node_data);
      }
    
      // Set node's data
      public void setData(Object data)
      {
        node_data = data;
      }
    
      // Find the node with specified data
      // Return null if not found
      public TreeNode findChild(Object data)
      {
        // Maybe we're the one we're looking for
        if (equalData(data))
          return (this);
    
        // Search within child nodes
        Iterator<TreeNode> it;
        TreeNode           node;
        it = getChildrenIterator();
        while (it.hasNext())
        {
          node = findChild(it.next());
          if (node != null)
            return (node);
        }
    
        // If we get here, we didn't find it
        return (null);
    
      } // findChild
    
      // Return whether specified data equals ours
      private boolean equalData(Object data)
      {
        if (node_data == null)
          return (data == null);
        else
          return (node_data.equals(data));
      }
    
      // Return iterator over node's children
      private Iterator<TreeNode> getChildrenIterator()
      {
        return (children.iterator());
      }
    
      // The node's children
      private List<TreeNode> children;
    
      // The node's data
      private Object node_data;
    
    } // class TreeNode
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 2021-09-30
      • 2016-04-19
      相关资源
      最近更新 更多