Monday, 15 July 2013

algorithm - Binary Tree in Java. Problems with System.out.println() -



algorithm - Binary Tree in Java. Problems with System.out.println() -

i have create look tree. single illustration model of it. shows me unusual chars not strings. can help me solve problem. , 1 question: can show me way simplify code. part of code? can without it?

public iterator<treenode<t>> iterator() { homecoming null; }

import java.util.*; public class treenode<t> implements iterable<treenode<t>> { t data; treenode<t> parent; list<treenode<t>> children; public treenode(t data) { this.data = data; this.children = new linkedlist<treenode<t>>(); } public treenode<t> addchild(t child) { treenode<t> childnode = new treenode<t>(child); childnode.parent = this; this.children.add(childnode); homecoming childnode; } public iterator<treenode<t>> iterator() { homecoming null; } public static void main(string[] args){ treenode<string> root = new treenode<string>("root"); system.out.println(" " + root + " "); system.out.println(" / \\ "); treenode<string> node1 = root.addchild("node1"); treenode<string> node2 = root.addchild("node2"); system.out.println(" " + node1 + " " + node2); system.out.println(" \\"); treenode<string> node20 = node2.addchild(null); system.out.println(" " + node20); system.out.println(" / \\"); treenode<string> node21 = node2.addchild("node21"); treenode<string> node210 = node20.addchild("node210"); system.out.println(" " + node21 + " " + node210); } }

to see more meaningful values in output should override tostring() method of treenode, maybe like:

@override public string tostring(){ homecoming string.valueof(this.data); }

java algorithm io binary-tree

No comments:

Post a Comment