All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.greatbit.utils.tree.processors.Traverse Maven / Gradle / Ivy

package ru.greatbit.utils.tree.processors;

import ru.greatbit.utils.tree.nodes.Node;
import ru.greatbit.utils.tree.processors.api.Visitor;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by azee on 5/8/14.
 */
public class Traverse {

    /**
     * Collect nodes in order using Breadth-first traversal
     * @param head
     * @return
     */
    public static List> bfsList(Node head){
        final List> result = new LinkedList>();
        bfsVisit(head, new Visitor() {
            @Override
            public void visit(Node node) {
                result.add(node);
            }
        });
        return result;
    }

    /**
     * Process node using visitors pattern
     * @param head
     * @return
     */
    public static  void bfsVisit(Node head, Visitor visitor){
        if (head == null){
            return;
        }

        Queue queue = new LinkedList();
        queue.add(head);

        while (!queue.isEmpty()){
            if (!queue.isEmpty()) {
                head = queue.poll();
                visitor.visit(head);
            }

            for (Node child : head.getChildren()){
                queue.add(child);
            }
        }
    }

    /**
     * Collect nodes in order using Depth-first traversal
     * @param head
     * @return
     */
    public static List> dfsList(Node head){
        List> result = new LinkedList>();
        dfsList(head, result);
        return result;
    }

    /**
     * Recursive method to collect objects in DFS traversal
     * @param head
     * @param result
     */
    private static  void dfsList(Node head, final List> result){
        dfs(head, new Visitor() {
            @Override
            public void visit(Node node) {
                result.add(node);
            }
        });
    }

    /**
     * Recursive method to process objects in DFS traversal using visitors pattern
     * @param head
     */
    private static  void dfs(Node head, Visitor visitor){
        if (head == null){
            return;
        }
        visitor.visit(head);
        for (Node children : head.getChildren()){
            dfs(children, visitor);
        }
    }

    /**
     * Find all leafs
     * @param head
     * @param 
     * @param 
     * @return
     */
    public static  List> getlLeafs(Node head){
        List> result = new LinkedList>();
        getlLeafs(head, result);
        return result;
    }


    /**
     * Recursive method to collect all leafs in DFS traversal
     * @param head
     * @param result
     * @param 
     * @param 
     */
    private static  void getlLeafs(Node head, List> result){
        if (head == null){
            return;
        }
        if (head.getChildren().size() == 0){
            result.add(head);
        } else {
            for (Node node : head.getChildren()){
                getlLeafs(node, result);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy