java recursive, non-recursive depth-first traversal of the tree node [, breadth-first]

java tree node Recursion

In fact, the number of similar Bowen and a lot, but think a lot of Bowen is quite chaotic and somewhat complicated, so I decided to try to write a straightforward blog ...

 Since the tree nodes are generally unable to determine its level layers, the conventional method is generally a node tree traversal recursive function can be used.

Benefits recursive function is the code easier to read, but the drawback is that the level of the tree too deep can cause memory overflow, the way to write the following simple recursive tree traversal Code:

Assuming that there is a demand, it is necessary to traverse the tree to find the node is equal to the code, and then return to these nodes.

 

Recursive implementation:

//递归遍历树,主函数入口
public List<Node> findCodeListByTree(List<String> codeList) {
    List<Node> nodeList = new ArrayList<Node>();  //储存节点
    Node rootNode = tree.getRootNode();   //获取根节点
    for(String code : codeList) {
        nodeList.addAll(findNodeByChildren(rootNode,code));  
    } 
    return nodeList;
}

//递归方法
public List<Node> findNodeByChildren(Node node,String code) {
    List<Node> nodeList = new ArrayList<Node>();
    if(code.equals(node.getCode())) {
         nodeList.add(node);
    }
    Node childNode = node.getChildren();
    if(childNode != null) {
        nodeList.addAll(findNodeByChildren(childNode,code));
    }
    return nodeList;
}

 

Which can also be achieved using non-recursive functions, personally I prefer this implementation.

Non-recursive, depth-first traversal of the tree, it means the use of the advanced features Stack

//递归遍历树,主函数入口
public List<Node> findCodeListByTree(List<String> codeList) {
    List<Node> nodeList = new ArrayList<Node>();  //储存节点
    Stack<Node> nodeStack = new Stack<Node>();    //节点栈
    Node rootNode = tree.getRootNode();   //获取根节点
    nodeStack.add(rootNode);
    for(String code : codeList) {
        while(!nodeStack.isEmpty()) {
            Node node = nodeStack.pop();
            if(node.getCode().equals(code)) {
                nodeList.add(node);
            }
            List<Node> childrenNode = node.getChildren();
            nodeStack.addAll(childrenNode);
        } 
    } 
    return nodeList;
}

Non-recursive, breadth-first traversal of the tree, you can use FIFO Queue feature

//递归遍历树,主函数入口
public List<Node> findCodeListByTree(List<String> codeList) {
    List<Node> nodeList = new ArrayList<Node>();  //储存节点
    Queue<Node> nodeQueue = new LinkedList<Node>();    //节点栈
    Node rootNode = tree.getRootNode();   //获取根节点
    nodeQueue.offer(rootNode);
    for(String code : codeList) {
        while(!nodeQueue.isEmpty()) {
            Node node = nodeQueue.poll();
            if(node.getCode().equals(code)) {
                nodeList.add(node);
            }
            List<Node> childrenNode = node.getChildren();
            nodeQueue.addAll(childrenNode);
        } 
    } 
    return nodeList;
}

 

Since the actual tree data Many scenes are not binary, just to give a relatively simple tree traversal method, the subsequent traversal later before there is time to supplement, if there is something wrong place welcome that!

Published 21 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/a5552157/article/details/83717148