@Test public void getLevel() throws Exception { TreeNode<Integer> l1, l2, l3; TreeNode<Integer> root = node(0) .addChild(l1 = node(1)) .addChild(node(1).addChild(l2 = node(2)).addChild(node(2).addChild(l3 = node(3)))) .addChild(node(1)); System.out.format("bfs: %s%n", Iterables.toString(root.breadthFirstTraversal().labels())); assertEquals("level", 0, root.getLevel()); assertEquals("level", 1, l1.getLevel()); assertEquals("level", 2, l2.getLevel()); assertEquals("level", 3, l3.getLevel()); }
@Override public int getLevel() { int level = 0; TreeNode<T> node = parent; while (node != null) { level++; node = node.getParent(); } return level; }
ListTreeNode implements TreeNode<T> { @Override public int getLevel() { int level = 0; TreeNode<T> node = parent; while (node != null) { level++; node = node.getParent(); } return level; } }
ListTreeNode implements TreeNode<T> { @Override public int getLevel() { int level = 0; TreeNode<T> node = parent; while (node != null) { level++; node = node.getParent(); } return level; } ListTreeNode(T label); }
ListTreeNode implements TreeNode<T> { @Override public int getLevel() { int level = 0; TreeNode<T> node = parent; while (node != null) { level++; node = node.getParent(); } return level; } ListTreeNode(T label); @Override T getLabel(); @Override Iterable<TreeNode<T>> children(); @Override int getLevel(); @Override int getChildCount(); @Override TreeNode<T> getParent(); @Override TreeNode<T> setParent(TreeNode<T> parent); @Override ListTreeNode<T> addChild(TreeNode<T> child); @Override boolean isRoot(); @Override NodeTraversal<T> breadthFirstTraversal(); @Override boolean isLeaf(); @Override String toString(); }
ListTreeNode implements TreeNode<T> { @Override public int getLevel() { int level = 0; TreeNode<T> node = parent; while (node != null) { level++; node = node.getParent(); } return level; } ListTreeNode(T label); @Override T getLabel(); @Override Iterable<TreeNode<T>> children(); @Override int getLevel(); @Override int getChildCount(); @Override TreeNode<T> getParent(); @Override TreeNode<T> setParent(TreeNode<T> parent); @Override ListTreeNode<T> addChild(TreeNode<T> child); @Override boolean isRoot(); @Override NodeTraversal<T> breadthFirstTraversal(); @Override boolean isLeaf(); @Override String toString();  }