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 - A head node of a tree
     * @return - returns a list of visited nodes
     * @param  - Tree node key
     * @param  - Tree node value
     */
    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 - A head node of a tree
     * @param visitor - Visitor object - a processor
     * @param  - Tree node key
     * @param  - Tree node value
     */
    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 - A head node of a tree
     * @return - returns a list of visited nodes
     * @param  - Tree node key
     * @param  - Tree node value
     */
    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 - A head node of a tree
     * @param result - returns a list of visited nodes
     * @param  - Tree node key
     * @param  - Tree node value
     */
    private static  void dfsList(Node head, final List> result){
        dfsVisit(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 - A head node of a tree
     * @param visitor - Visitor object - a processor
     * @param  - Tree node key
     * @param  - Tree node value
     */
    public static  void dfsVisit(Node head, Visitor visitor){
        if (head == null){
            return;
        }
        visitor.visit(head);
        for (Node children : head.getChildren()){
            dfsVisit(children, visitor);
        }
    }

    /**
     * Find all leafs
     * @param head - A head node of a tree
     * @param  - Tree node key
     * @param  - Tree node value
     * @return - List of Tree Nodes - all leafs
     */
    public static  List> getLeafs(Node head){
        List> result = new LinkedList>();
        getLeafs(head, result);
        return result;
    }


    /**
     * Recursive method to collect all leafs in DFS traversal
     * @param head - A head node of a tree
     * @param result - List of Tree Nodes - all leafs
     * @param  - Tree node key
     * @param  - Tree node value
     */
    public static  void getLeafs(Node head, List> result){
        if (head == null){
            return;
        }
        if (head.getChildren().size() == 0){
            result.add(head);
        } else {
            for (Node node : head.getChildren()){
                getLeafs(node, result);
            }
        }
    }

    /**
     * Recursive method to count all leafs in DFS traversal
     * @param head - A head node of a tree
     * @param  - Tree node key
     * @param  - Tree node value
     * @return - ling value showing number of leafs
     */
    public static  long countLeafs(Node head){
        long count = 0;
        if (head == null){
            return count;
        }
        if (head.getChildren().size() == 0){
            return 1;
        } else {
            for (Node node : head.getChildren()){
                count += countLeafs(node);
            }
            return count;
        }
    }

    /**
     * Recursive method to verify max height of the tree
     * @param head - A head node of a tree
     * @param  - Tree node key
     * @param  - Tree node value
     * @return  int - max tree height
     */
    public static  int countMaxHeight(Node head){
        if (head == null){
            return 0;
        }

        List heights = new LinkedList();
        for (Node node : head.getChildren()){
            heights.add(countMaxHeight(node));
        }

        int max = 0;
        for (Integer childHeight : heights){
            max = Math.max(max, childHeight);
        }
        return 1 + max;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy