To get closer to the code for graphs, consider general trees, such as the following.

 file:XTree.java [source] [doc-public] [doc-private]
 ``` 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 ``` ```package algs32; import java.util.Scanner; import algs13.Queue; import stdlib.*; public class XTree { private Node root; private XTree (Node root) { this.root = root; } private static class Node { public final int val; public final Queue children; // children must not be null public Node (int val) { this.val = val; this.children = new Queue<>(); } } // tree input has the form "num ( ... )" // where num is an integer and ... is another tree input // example: "0 ( 1 ( 11 12 13 ) 2 ( 21 22 ( 221 222 223 ) ) 3 ( ) 4 )" // empty parens "( )" are optional. public static XTree parse (String in) { Scanner sc = new Scanner (in); Node root = parseHelper (sc); return new XTree (root); } private static Node parseHelper (Scanner sc) { int val = sc.nextInt (); Node result = new Node (val); if (sc.hasNext ("\\(")) { sc.next (); // gobble "(" while (sc.hasNextInt ()) { Node child = parseHelper (sc); result.children.enqueue (child); } sc.next ("\\)"); //gobble ")" } return result; } // prefix with parenthesis public String toString() { StringBuilder sb = new StringBuilder(); if (root != null) toString (sb, root); return sb.toString (); } private static void toString (StringBuilder sb, Node n) { sb.append (n.val); sb.append (" "); if (!n.children.isEmpty ()) { sb.append ("( "); for (Node child : n.children) toString (sb, child); sb.append (") "); } } public void toGraphviz(String filename) { GraphvizBuilder gb = new GraphvizBuilder (); if (root != null) toGraphviz (gb, null, root); gb.toFileUndirected (filename, "ordering=\"out\""); } private static void toGraphviz (GraphvizBuilder gb, Node parent, Node n) { gb.addLabeledNode (n, Integer.toString (n.val)); if (parent != null) gb.addEdge (parent, n); for (Node child : n.children) toGraphviz (gb, n, child); } // prefix order traversal public void printPre () { if (root != null) printPre (root); StdOut.println (); } private static void printPre (Node n) { StdOut.print (n.val + " "); for (Node child : n.children) { printPre (child); } } // level order traversal public void printLevel () { Queue queue = new Queue<>(); if (root != null) queue.enqueue(root); while (!queue.isEmpty()) { Node n = queue.dequeue(); StdOut.print (n.val + " "); for (Node child : n.children) { queue.enqueue(child); } } StdOut.println (); } public static void main(String[] args) { XTree xtree = XTree.parse ("0 ( 1 ( 11 12 ( 121 ( 1211 1212 ) 122 123 124 125 ) 13 ) 2 ( 21 22 ( 221 222 223 ) ) 3 )"); StdOut.println (xtree); xtree.printPre (); xtree.printLevel (); xtree.toGraphviz ("xtree.png"); } } ```