Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file Node.java
- public class Node {
- protected final int value;
- public Node(int value) {
- this.value = value;
- }
- }
- // file InnerNode.java
- public class InnerNode extends Node {
- private final List<Node> children;
- public InnerNode() {
- }
- public void add(Node node) {
- children.add(node);
- }
- public void print() {
- print(0);
- }
- private void print(int padding) {
- for (int i = 0; i < children.size(); i++) {
- Node child = children.get(i);
- printPadding(padding);
- System.out.println(child.value);
- child.print(padding + 1);
- }
- }
- private static void printPadding(int padding) {
- for (int i = 0; i < padding; i++) {
- System.out.print(" ");
- }
- }
- }
- // file Main.java
- public class Main {
- public static void main(String[] args) {
- Node left = new Node(42);
- Node right = new Node(99);
- InnerNode inner = new InnerNode();
- inner.add(left);
- inner.add(right);
- inner.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement