/* * TreePre.java * */ import net.datastructures.*; public class TreePre { static int order; public static void TreePre(LinkedBinaryTree t, Position p) { MyData1 data = (MyData1)p.element(); System.out.println("The Preorder of "+data.A+ " is "+ order); data.B = order; order++; if (t.hasLeft(p)) { TreePre(t, t.left(p)); } if (t.hasRight(p)) { TreePre(t, t.right(p)); } } public static void main(String[] args) { LinkedBinaryTree t = new LinkedBinaryTree(); Position parent; /* generate a tree t, insert 9 nodes */ t.addRoot(new MyData1('A')); parent = t.root(); t.insertLeft(parent, new MyData1('B')); t.insertRight(parent, new MyData1('C')); parent = t.left(t.root()); t.insertLeft(parent, new MyData1('D')); t.insertRight(parent, new MyData1('E')); parent = t.right(t.root()); t.insertLeft(parent, new MyData1('F')); t.insertRight(parent, new MyData1('G')); parent = t.right(t.left(t.root())); t.insertLeft(parent, new MyData1('H')); t.insertRight(parent, new MyData1('I')); /* print the structure of tree t */ MyData1.printTree(t); /* Preorder tranversal of tree t */ order = 1; TreePre (t, t.root()); } }