// Demonstrate implementation of preorder and postorder using a stack. class StackRecord { public AnimalGroup1 element; // Element on stack public int indent; // How deep to indent public int state =0; // How many children have been explored public StackRecord(AnimalGroup1 e, int i) { element = e; indent = i; } } public class StackTreeTraversal2{ public static void displayNode(StackRecord r) { for (int i=0; i stack = new MyStack(new StackRecord[200]); stack.push(new StackRecord(A,0)); while (! stack.isEmpty()) { StackRecord r = stack.top(); if (r.state==0) displayNode(r); AnimalGroup1 e = r.element; if (r.state == e.getNumChildren()) stack.pop(); else { stack.push(new StackRecord( e.getChildren()[r.state], r.indent+3)); r.state++; } } } public static void postorderDisplay(AnimalGroup1 A) { MyStack stack = new MyStack(new StackRecord[200]); stack.push(new StackRecord(A,0)); while (! stack.isEmpty()) { StackRecord r = stack.top(); AnimalGroup1 e = r.element; if (r.state == e.getNumChildren()) { displayNode(r); stack.pop(); } else { stack.push(new StackRecord( e.getChildren()[r.state], r.indent+3)); r.state++; } } } public static void main(String args[]) { AnimalGroup1 Mammal = AnimalGroup1.Taxonomy1(); System.out.println(" ************ PREORDER ***************"); preorderDisplay(Mammal); System.out.println("\n ************ POSTORDER ***************"); postorderDisplay(Mammal); } }