将子树插入树节点

时间:2016-03-18 19:24:23

标签: java hierarchy

我有以下代码(道歉它有点长),有效:

public class Node {
        public String name;
        public List<Node> children = new ArrayList<>();
    }

    private Node root = new Node();

    public static void main(String[] args)
    {
        TestCreateHierarchy obj = new TestCreateHierarchy();
        obj.run();
    }

    public void run()
    {
        List<List<String>> objects = new ArrayList<>();

        String[] str = {"Person","Manager","Hourly","New"};
        objects.add(Arrays.asList(str)) ;

        String[] str2 = {"Person","Manager","Salary","Hello"};
        objects.add(Arrays.asList(str2)) ;

        String[] str3 = {"Person","Manager","Salary", "New"};
        objects.add(Arrays.asList(str3)) ;

//        String[] str4 = {"Person","Manager","Salary", "New", "John"};
//        objects.add(Arrays.asList(str4)) ;

        root.name = "ROOT";

        for (List<String> seq : objects) {
            LOG.debug("------------- NEW SEQUENCE -----------");
            LOG.debug(seq);
            LOG.debug("--------------------------------------");

            insert(seq);

//            break;
        }

        LOG.debug(root.children.size() + " <-- should be 1 ");

    }

    public Node createNode(List<String> seq, Integer start)
    {
        if (start >= seq.size())
            return null;

        Node node = new Node();
        node.name = seq.get(start);
        node.children.add(createNode(seq, start+1));

        return node;
    }


    public void insert(List<String> seq) {

        List<Node> children = root.children;

        // Where should we start to build the tree (how
        // deep into the sequence?)
        int start = 0 ;

        // Find the right place to add
        for (int i=0; i<seq.size(); i++) {
            boolean foundMatchingNode = false;

//            if (children.size() == 0)
//                continue;

            if (children.size() > 0) {
                for (Node node : children) {

                    LOG.debug("HERE --> " + node.name);

                    if (node.name.equals(seq.get(i))) {
                        children = node.children;
                        foundMatchingNode = true;
                        break;
                    }
                }
            }

            if (!foundMatchingNode) {
                children.add(createNode(seq,i));
                break;
            }
        }

    }

我的问题是,当我取消注释str4部分时,我会NullPointerException引用insert方法,特别是for (Node node : children)部分。

我需要更改什么才能使此代码适用于不同的层次结构深度?

2 个答案:

答案 0 :(得分:0)

您的createNode方法正在创建向叶子添加null子元素。发生NullPointerException是因为当您到达树的底部时,Node本身为null。

如果这还不够,请询问更多信息。

答案 1 :(得分:0)

问题在于这个功能:

const state = {
  isLogin: false,
  userid:0
}

const mutations = {
  login(state, userid){
    state.userid = userid;
    state.isLogin = true;
  }
}

您正在递归地为每个新创建的节点添加一个子节点,并且对于此条件public Node createNode(List<String> seq, Integer start) { if (start >= seq.size()) return null; Node node = new Node(); node.name = seq.get(start); node.children.add(createNode(seq, start+1)); return node; } 满足您的最后一个函数调用,您将创建一个(start >= seq.size())子节点。

因此,在插入str1之后,您的树看起来像这样: null。现在,当您尝试输入str4时,您的逻辑将用于检查匹配的节点名称代码。对于条目,循环运行正常:经理每小时,直到针对该节点运行ROOT->Person->Manager->Hourly->New->NULL。在内部for循环NULL中,您有此调试器日志:for (Node node : children)。此时您已LOG.debug("HERE --> " + node.name); NullPointerException,因为nodenull