|
| 1 | +package org.behrang.algorithm.tree.demo; |
| 2 | + |
| 3 | +import org.behrang.algorithm.tree.Node; |
| 4 | + |
| 5 | +public final class SampleTree { |
| 6 | + |
| 7 | + private SampleTree() { |
| 8 | + } |
| 9 | + |
| 10 | + public static Node<String> newUniformInstance() { |
| 11 | + return newUniformInstance(2, 2); |
| 12 | + } |
| 13 | + |
| 14 | + public static Node<String> newUniformInstance(double nodeWidth, double nodeHeight) { |
| 15 | + Node<String> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o; |
| 16 | + |
| 17 | + o = node("O", nodeWidth, nodeHeight); |
| 18 | + e = node("E", nodeWidth, nodeHeight); |
| 19 | + f = node("F", nodeWidth, nodeHeight); |
| 20 | + n = node("N", nodeWidth, nodeHeight); |
| 21 | + link(o, e, f, n); |
| 22 | + |
| 23 | + a = node("A", nodeWidth, nodeHeight); |
| 24 | + d = node("D", nodeWidth, nodeHeight); |
| 25 | + link(e, a, d); |
| 26 | + |
| 27 | + b = node("B", nodeWidth, nodeHeight); |
| 28 | + c = node("C", nodeWidth, nodeHeight); |
| 29 | + link(d, b, c); |
| 30 | + |
| 31 | + g = node("G", nodeWidth, nodeHeight); |
| 32 | + m = node("M", nodeWidth, nodeHeight); |
| 33 | + link(n, g, m); |
| 34 | + |
| 35 | + h = node("H", nodeWidth, nodeHeight); |
| 36 | + i = node("I", nodeWidth, nodeHeight); |
| 37 | + j = node("J", nodeWidth, nodeHeight); |
| 38 | + k = node("K", nodeWidth, nodeHeight); |
| 39 | + l = node("L", nodeWidth, nodeHeight); |
| 40 | + link(m, h, i, j, k, l); |
| 41 | + |
| 42 | + return o; |
| 43 | + } |
| 44 | + |
| 45 | + static Node<String> node(String value, double nodeWidth, double nodeHeight) { |
| 46 | + Node<String> node = new Node<>(value); |
| 47 | + node.setWidth(nodeWidth); |
| 48 | + node.setHeight(nodeHeight); |
| 49 | + |
| 50 | + return node; |
| 51 | + } |
| 52 | + |
| 53 | + @SafeVarargs |
| 54 | + static void link(Node<String> parent, Node<String>... children) { |
| 55 | + for (var child : children) { |
| 56 | + parent.getChildren().add(child); |
| 57 | + child.setParent(parent); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments