Skip to content

Instantly share code, notes, and snippets.

View claymccoy's full-sized avatar

Clay McCoy claymccoy

View GitHub Profile
public class TreeNode {
private final String name;
private final Collection<TreeNode> children;
public TreeNode(String name) {
this.name = name;
children = new HashSet<TreeNode>();
}
public String getName() {
final TreeNode shape = new TreeNode("shape");
final TreeNode ellipse = new TreeNode("ellipse");
shape.addChild(ellipse);
ellipse.addChild(new TreeNode("circle"));
final TreeNode polygon = new TreeNode("polygon");
shape.addChild(polygon);
final TreeNode triangle = new TreeNode("triangle");
polygon.addChild(triangle);
triangle.addChild(new TreeNode("equilateral"));
triangle.addChild(new TreeNode("isosceles"));
shape {
ellipse {
circle
}
polygon {
petagon
triangle {
scalene
equilateral
isosceles
import java.util.ArrayList;
import java.util.List;
public class abc {
public static void main(String[] args) {
List<String> abc = new ArrayList<String>();
abc.add("a");
abc.add("b");
abc.add("c");
[:a,:b,:c].each do |s|
puts s
end
require 'java'
require 'Trees.jar'
TreeNode = com.claymccoy.trees.TreeNode
class TreeNode
def to_s
puts to_string.gsub("\\r", "\\n")
end
end
class TreeNode
def method_missing(methodname, *args, &block)
child = self.children().find { |child| methodname.to_s == child.name }
if child.nil?
child = TreeNode.new(methodname.to_s)
self.add_child(child)
end
child.instance_eval(&block) if block_given?
child
end
TreeNode ellipse = null;
for (TreeNode treeNode : shape.getChildren()) {
if ("ellipse".equals(treeNode.getName())) {
ellipse = treeNode;
break;
}
}
if (null == ellipse) {
ellipse = new TreeNode("ellipse");
shape.addChild(ellipse);
shape {
ellipse.circle
polygon {
petagon
triangle {
scalene
equilateral
isosceles
}
hexagon
Tree.create {
shape {
ellipse.circle
polygon {
petagon
triangle {
scalene
equilateral
isosceles
}