Traversing a non-binary tree in post order
17:54 22 Nov 2020

I am writing a java program that creates a non-binary tree and implements a post order traversal. I have set up the tree, but I am stuck on how to implement post order for a non-binary tree. The program uses linked lists to store the children. For binary trees this is simple but I am not sure how to iterate and through the linked list and also print after it finds a node with null children

I have seen traversing a non binary tree in java this does not work as I cannot add methods and you cannot iterate a linked list like that

/***  DO NOT ADD A NEW IMPORT DECLARATION HERE  ***/
 
/***  DO NOT MAKE ANY CHANGE TO CLASS A5 EXCEPT THE PLACEHOLDER TO FILL IN  ***/
/***  YOU CANNOT ADD A NEW FIELD VARIABLE  ***/ 
/***  YOU CANNOT ADD A NEW METHOD DECLARATION  ***/ 
public class A5 {
    public static void main(String[] args) {
        //---------------------------------------------------------------------
        TreeNode root = new TreeNode(1);
        MyGenericLinkedList children = new MyGenericLinkedList();
 
        TreeNode two = new TreeNode(2);
        TreeNode three = new TreeNode(3);
        TreeNode four = new TreeNode(4);
        TreeNode five = new TreeNode(5);
        TreeNode six = new TreeNode(6);
        TreeNode seven = new TreeNode(7);
        TreeNode eight = new TreeNode(8);
        TreeNode nine = new TreeNode(9);
        TreeNode ten = new TreeNode(10);
        TreeNode eleven = new TreeNode(11);
        TreeNode twelve = new TreeNode(12);
        TreeNode thirteen = new TreeNode(13);
        TreeNode fourteen = new TreeNode(14);
 
        children.add(two);
        children.add(three);
        children.add(four);
        root.setChildren(children);
        children.remove(0);
        children.remove(0);
        children.remove(0);
 
        children.add(five);
        children.add(six);
        two.setChildren(children);
        children.remove(0);
        children.remove(0);
 
        children.add(ten);
        children.add(eleven);
        four.setChildren(children);
        children.remove(0);
        children.remove(0);
 
        children.add(seven);
        children.add(eight);
        children.add(nine);
        six.setChildren(children);
        children.remove(0);
        children.remove(0);
        children.remove(0);
 
        children.add(twelve);
        ten.setChildren(children);
        children.remove(0);
 
        children.add(thirteen);
        children.add(fourteen);
        twelve.setChildren(children);
        children.remove(0);
        children.remove(0);
        //---------------------------------------------------------------------
 
        /***  DO NOT MAKE ANY CHANGE TO THE FOLLOWING CODE  ***/
        MyGenericTree tree = new MyGenericTree(root);
        tree.traverseInPostOrder();
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS MyGenericTree EXCEPT THE PLACEHOLDER TO FILL IN  ***/
/***  YOU CANNOT ADD A NEW FIELD VARIABLE  ***/ 
/***  YOU CANNOT ADD A NEW METHOD DECLARATION  ***/ 
class MyGenericTree {
    private TreeNode root = null;
 
    public MyGenericTree(TreeNode root) {
        this.root = root;
    }
 
    public void traverseInPostOrder() {
        traverseInPostOrder(root);
    }
 
    public void traverseInPostOrder(TreeNode node) {     
        //---------------------------------------------------------------------
        
        //---------------------------------------------------------------------
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS TreeNode  ***/
class TreeNode {
    N data = null;
    TreeNode parent = null;
    MyGenericLinkedList> children = null;
 
    public TreeNode(N data) {
        this.data = data;
    }
 
    public void setChildren(MyGenericLinkedList> children) {
        this.children = children;
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS MyGenericLinkedList  ***/
class MyGenericLinkedList {
    Node front;
 
    public MyGenericLinkedList() {
        front = null;
    }
 
    public void add(S value) {
        if (front == null) {
            front = new Node(value);
        } else {
            Node current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node(value);
        }
    }
 
    public S get(int index) {
        Node current = front;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return (S)current.data;
    }
 
    public void remove(int index) {
        if (index == 0) {
            front = front.next;
        } else {
            Node current = front;
            for (int i = 0; i < index - 1; i++) {
                current = current.next;
            }
            current.next = current.next.next;
        }
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS Node  ***/
class Node {
    X data;
    Node next;
 
    public Node(X data) {
        this.data = data;
        this.next = null;
    }
 
    public Node(X data, Node next) {
        this.data = data;
        this.next = next;
    }
}```
java tree