我想要一个展开树,我想要打印到文本区域。它是在控制台中打印,但我决定添加一个GUI,我想将树打印到textArea。
public class Bst<Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
private Key phone_number;
private Value contact_name;
private Node left, right;
public Node(Key phone_number, Value contact_name) {
this.phone_number = phone_number;
this.contact_name = contact_name;
}
}
public boolean contains(Key phone_number) {
return (get(phone_number) != null);
}
// return contact_name associated with the given phone_number
public Value get(Key phone_number) {
root = splay(root, phone_number);
int cmp = phone_number.compareTo(root.phone_number);
if (cmp == 0)
return root.contact_name;
else
return null;
}
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
private void printTree( Node t )
{
if ( t.left != null )
{
System.out.println( "Phone Number:" + t.phone_number.toString( ) + " Contact Name : " + t.contact_name.toString( ) );
printTree( t.left );
}
if (t.right != null)
{
printTree( t.right );
System.out.println( "name:" + t.phone_number.toString( ) + " Number : " + t.contact_name.toString( ) );
}
}
}
目前我的printTree具有void返回类型,如上所示。
如何修改我的代码,以便能够将所有树值和键打印到TextArea。我知道setText()采用字符串类型,但在这种情况下不起作用(我认为),我怎样才能确保print方法将所有值输出到文本区域?
答案 0 :(得分:0)
以下是您需要的代码。请注意,您的no-arg printTree()方法现在返回一个String,您可以使用该字符串在其他位置打印树。
public class Bst<Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
private Key phone_number;
private Value contact_name;
private Node left, right;
public Node(Key phone_number, Value contact_name) {
this.phone_number = phone_number;
this.contact_name = contact_name;
}
}
public boolean contains(Key phone_number) {
return (get(phone_number) != null);
}
// return contact_name associated with the given phone_number
public Value get(Key phone_number) {
root = splay(root, phone_number);
int cmp = phone_number.compareTo(root.phone_number);
if (cmp == 0)
return root.contact_name;
else
return null;
}
public String printTree( )
{
if( isEmpty( ) )
return "Empty tree";
else
{
StringBuilder sb = new StringBuilder();
printTree( root, sb );
return sb.toString();
}
}
private void printTree( Node t, StringBuilder sb )
{
if ( t.left != null )
{
sb.append( "Phone Number:" + t.phone_number.toString( ) + " Contact Name : " + t.contact_name.toString( ) );
printTree( t.left, sb );
}
if (t.right != null)
{
printTree( t.right, sb );
sb.append( "name:" + t.phone_number.toString( ) + " Number : " + t.contact_name.toString( ) );
}
}
}
答案 1 :(得分:0)
您可以使用以下代码。实现toString()允许您忘记从printTree()获得的任何其他字符串打印。你可以打印出你的物体 顺便说一句,如果“Value”和“Key”正在实现toString(),那么就不需要调用它,你可以在带有运算符“+”的字符串中使用它们。
public String toString()
{
return printTree();
}
public String printTree( )
{
String str;
if( isEmpty( ) )
str = "Empty tree";
else
str += printTree( root );
}
private String printTree( Node t )
{
String str = "";
if ( t.left != null )
{
str += "Phone Number:" + t.phone_number + " Contact Name : " + t.contact_name + "\n";
str += printTree( t.left );
}
if (t.right != null)
{
str += printTree( t.right );
str += "name:" + t.phone_number + " Number : " + t.contact_name + "\n";
}
return str;
}