简体   繁体   中英

How do I make a directed unweighted graph with a root node in java?

I am working on a school project and I need to build a Data Structure that has a root node, multiple "ending" nodes(the end of the data Structure), and undefined number of nodes(that have at most two directed links to other nodes) in between the root node and the multiple "ending" nodes. I was thinking of creating some sort of binary graph (represented as a adjacency matrix) where each node can lead to a max of two other nodes, but I don't know how I would construct it in such a way that there is a root node and ending nodes. Can anyone give ideas on how I would do this or a better way to do it? Thank you.(It needs to be in java)

Also, I forgot to mention that I would be adding elements to the data structure and that all paths down the structure would eventually have to lead to one of the preset ending nodes.

Why the adjacency matrix? You're right about the binary tree (or DAG, to be precise). A node can be represented with this class:

class BinaryTree<T> {
    BinaryTree<T> left;
    BinaryTree<T> right;
}

Ending nodes would be the nodes with left == null && right == null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM