Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created May 24, 2016 13:54
Show Gist options
  • Save developer-sdk/2f06406f02acb2f5585e4933d0527c38 to your computer and use it in GitHub Desktop.
Save developer-sdk/2f06406f02acb2f5585e4933d0527c38 to your computer and use it in GitHub Desktop.
Java 를 이용한 트리
package sdk.java;
import java.util.ArrayList;
public class TreeNode {
public TreeNode parent;
public String data;
public ArrayList<TreeNode> childs;
public TreeNode(String data) {
this.data = data;
this.childs = new ArrayList<>();
}
public static void main(String[] args) {
TreeNode node1 = new TreeNode("1");
TreeNode node2 = new TreeNode("2");
TreeNode node3 = new TreeNode("3");
TreeNode node4 = new TreeNode("4");
TreeNode node5 = new TreeNode("5");
node1.childs.add(node2);
node1.childs.add(node3);
node1.childs.add(node4);
node2.parent = node1;
node3.parent = node1;
node4.parent = node1;
node2.childs.add(node5);
node5.parent = node2;
printTree("start ", node1);
}
public static void printTree(String str, TreeNode node) {
for(TreeNode child : node.childs) {
String s = str + "->" + child.data;
if(child.childs.size() != 0)
printTree(s, child);
else
System.out.println(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment