/* * TreeHeight1.java * */ import net.datastructures.*; import java.util.Iterator; public class TreeHeight1 { public static int height1 (Tree t) { int h = 0; Iterator v = t.positions(); while (v.hasNext()) { h = Math.max(h, TreeDepth.depth( t, (Position)v.next() ) ); } return h; } public static void main(String[] args) { LinkedBinaryTree t = new LinkedBinaryTree(); Position parent; int h; /* 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); /* get the height of tree t */ h = height1 (t); System.out.println("The height of tree t is " + h); } }