【问题标题】:toString() method for a LinkedBinaryTree<E> classLinkedBinaryTree<E> 类的 toString() 方法
【发布时间】:2016-10-22 02:18:43
【问题描述】:

有人可以帮我弄清楚如何为这个类创建一个 toString() 吗?它是一个包含 Node 类的 LinkedBinaryTree 类。我不认为提供这些类需要的超类和接口有助于创建 toString 方法,但如果我错了,请纠正我。这是课程:

 public class LinkedBinaryTree<E> extends AbstractBinaryTree<E>{
protected static class Node<E> implements Position<E> {
    private E element; // an element stored at this node
    private Node<E> parent; // a reference to the parent node (if any)
    private Node<E> left; // a reference to the left child (if any)
    private Node<E> right; // a reference to the right child (if any)

    public Node(E e, Node<E> above, Node<E> leftChild, Node<E> rightChild) {
        element = e;
        parent = above;
        left = leftChild;
        right = rightChild;
    }
    // accessor methods
    public E getElement( ) { return element; }
    public Node<E> getParent( ) { return parent; }
    public Node<E> getLeft( ) { return left; }
    public Node<E> getRight( ) { return right; }
    // update methods
    public void setElement(E e) { element = e; }
    public void setParent(Node<E> parentNode) { parent = parentNode; }
    public void setLeft(Node<E> leftChild) { left = leftChild; }
    public void setRight(Node<E> rightChild) { right = rightChild; }
} //----------- end of nested Node class -----------


protected Node<E> createNode(E e, Node<E> parent,
        Node<E> left, Node<E> right) {
    return new Node<E>(e, parent, left, right);
}

// LinkedBinaryTree instance variables
protected Node<E> root = null; // root of the tree
private int size = 0; // number of nodes in the tree

// constructor
public LinkedBinaryTree( ) { } // constructs an empty binary tree

// nonpublic utility

protected Node<E> validate(Position<E> p) throws IllegalArgumentException {
    if (!(p instanceof Node))
        throw new IllegalArgumentException("Not valid position type");
    Node<E> node = (Node<E>) p; // safe cast
    if (node.getParent( ) == node) // our convention for defunct node
        throw new IllegalArgumentException("p is no longer in the tree");
    return node;
}

// accessor methods (not already implemented in AbstractBinaryTree)

public int size( ) {
    return size;
}


public Position<E> root( ) {
    return root;
}


public Position<E> parent(Position<E> p) throws IllegalArgumentException {
    Node<E> node = validate(p);
    return node.getParent( );
}


public Position<E> left(Position<E> p) throws IllegalArgumentException {
    Node<E> node = validate(p);
    return node.getLeft( );
}


public Position<E> right(Position<E> p) throws IllegalArgumentException {
    Node<E> node = validate(p);
    return node.getRight( );
}

public Position<E> addRoot(E e) throws IllegalStateException {
    if (!isEmpty( )) throw new IllegalStateException("Tree is not empty");
    root = createNode(e, null, null, null);
    size = 1;
    return root;
}


public Position<E> addLeft(Position<E> p, E e)
        throws IllegalArgumentException {
    Node<E> parent = validate(p);
    if (parent.getLeft( ) != null)
        throw new IllegalArgumentException("p already has a left child");
    Node<E> child = createNode(e, parent, null, null);
    parent.setLeft(child);
    size++;
    return child;
}

public Position<E> addRight(Position<E> p, E e)
        throws IllegalArgumentException {
    Node<E> parent = validate(p);
    if (parent.getRight( ) != null)
        throw new IllegalArgumentException("p already has a right child");
    Node<E> child = createNode(e, parent, null, null);
    parent.setRight(child);
    size++;
    return child;
}


public E set(Position<E> p, E e) throws IllegalArgumentException {
    Node<E> node = validate(p);
    E temp = node.getElement( );
    node.setElement(e);
    return temp;
}

public void attach(Position<E> p, LinkedBinaryTree<E> t1,
        LinkedBinaryTree<E> t2) throws IllegalArgumentException {
    Node<E> node = validate(p);
    if (isInternal(p)) throw new IllegalArgumentException("p must be a leaf");
    size += t1.size( ) + t2.size( );
    if (!t1.isEmpty( )) { // attach t1 as left subtree of node
        t1.root.setParent(node);
        node.setLeft(t1.root);
        t1.root = null;
        t1.size = 0;
    }
    if (!t2.isEmpty( )) { // attach t2 as right subtree of node
        t2.root.setParent(node);
        node.setRight(t2.root);
        t2.root = null;
        t2.size = 0;
    }
}

public E remove(Position<E> p) throws IllegalArgumentException {
    Node<E> node = validate(p);
    if (numChildren(p) == 2)
        throw new IllegalArgumentException("p has two children");
    Node<E> child = (node.getLeft( ) != null ? node.getLeft( ) : node.getRight( ) );
    if (child != null)
        child.setParent(node.getParent( )); // child’s grandparent becomes its parent
    if (node == root)
        root = child; // child becomes root
    else {
        Node<E> parent = node.getParent( );
        if (node == parent.getLeft( ))
            parent.setLeft(child);
        else
            parent.setRight(child);
    }
    size--;
    E temp = node.getElement( );
    node.setElement(null); // help garbage collection
    node.setLeft(null);
    node.setRight(null);
    node.setParent(node); // our convention for defunct node
    return temp;
}
}

我们将不胜感激任何帮助,谢谢。

【问题讨论】:

    标签: java string tree binary-tree tostring


    【解决方案1】:

    您可以使用简单的递归来实现。

    public String toString(){
        String stringVal = "[";
        if(root != null){
            stringVal+=getChildNoteString(root);
        }
    
        return stringVal+"]";
    }
    
    private String getChildNodeString(Node n){
        String nodeStringVal  = n+",";
        if(n.getLeft()!=null){
            nodeStringVal += ","+ getChildNodeString(n.getLeft());
        }
    
        if(n.getRight()!=null){
            nodeStringVal += ","+ getChildNodeString(n.getRight());
        }
    }
    

    【讨论】:

    • 谢谢。但是,这仍然给了我一个看起来像这样的列表:
    • [LinkedBinaryTree$Node@15db9742, LinkedBinaryTree$Node@6d06d69c, LinkedBinaryTree$Node@7852e922]
    • 我认为这是因为每个单独的节点也需要有一个 toString
    • @Anon 没错。单个节点也需要有一个 toString。但是它们可以直接返回存储在节点中的元素值。
    • 谢谢,我需要做的就是获取节点的元素并将其作为字符串返回,然后您的方法就可以完美运行了。
    【解决方案2】:

    LinkedBinaryTree&lt;E&gt;.class 的某个地方,您需要覆盖toString(),例如:

    @Override // Not strictly necessary but good practice.
    public String toString() {
        return "Hello World!"; // Replace this with what you want to be returned.
    }
    

    您需要定义方法返回的内容以满足您的需求。

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 2016-07-21
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2017-05-24
      相关资源
      最近更新 更多