I wrote this code which adds children to nodes according to the weight of the heading of a non-binary tree. I am a novice programmer, so I would love to know how this code can be improved.import java.util.LinkedList;public class Sample {    public static void main(String... args) {        Sample buildTree = new Sample();        LinkedList<Heading> headings = new LinkedList<>();        headings.add(new Heading(1, a));        headings.add(new Heading(2, b));        headings.add(new Heading(2, c));        headings.add(new Heading(3, d));        headings.add(new Heading(3, e));        headings.add(new Heading(3, f));        headings.add(new Heading(4, g));        headings.add(new Heading(5, h));        headings.add(new Heading(2, i));        headings.add(new Heading(1, j));        headings.add(new Heading(1, k));        headings.add(new Heading(2, l));        Node structuredHeadings = buildTree.outline(headings);        System.out.print(buildTree.print(structuredHeadings));    }    /* @param  input  A linked list which contains headings with weights and its text    *  @return A non binary  tree which contains headings sorted by their weights    *    */    Node outline(LinkedList<Heading> input) {        Node mainNode = new Node(new Heading(0, ));        Node currentNode = mainNode;        Node parentNode = null;        for (Heading head : input) {            if (head == null) {                return null;            }            if(head.weight < 0){                throw new IllegalArgumentException(Weight can not be negative);            }            if (currentNode.head.weight == head.weight) {                currentNode = parentNode;                currentNode.children.add(new Node(head));                currentNode = currentNode.children.getLast();            }            if (currentNode.head.weight < head.weight) {                parentNode = currentNode;                currentNode.children.add(new Node(head));                currentNode = currentNode.children.getLast();            }            if (currentNode.head.weight > head.weight) {                currentNode = mainNode;                while (true) {                    int weight = currentNode.children.getLast().head.weight;                    if (weight == head.weight) {                        parentNode = currentNode;                        currentNode.children.add(new Node(head));                        currentNode = currentNode.children.getLast();                        break;                    }                    currentNode = currentNode.children.getLast();                }            }        }        return mainNode;    }    String print(Node node) {        String build  =  Node(Heading( + node.head.weight + , ' + node.head.text + ') + , List( ;        for (Node child : node.children) {            build +=   print(child);        }        build += ) ;        return build;    }    static class Heading {        int weight;        String text;        Heading(int weight, String text) {            this.weight = weight;            this.text = text;        }    }    static class Node {        Heading head;        LinkedList<Node> children;        Node(Heading head) {            this.head = head;            children = new LinkedList<>();        }    }}
Creating a non-binary tree by weights of the node and then traversing it
java;beginner;tree