我在java中实现Tree的错误是什么

时间:2014-06-22 00:04:45

标签: java data-structures tree linked-list

我试图实现一个包含给定节点子节点列表的Tree。当我尝试在main方法中输出大小时,它返回1.任何人都可以看到我的createNode方法是否有问题?在任何人生气之前,我只是包含我的所有代码,以便您可以看到我正在尝试做的事情:)

public class LinkedTree<E> implements Tree<E> {

    protected TreePosition<E> root; // reference to the root
    protected int size; // number of nodes

    public LinkedTree() {
        root = null; // start with an empty tree
        size = 0;
    }

    /** Returns the number of nodes in the tree. */
    public int size() {
        return size;
    }

    /** Returns whether the tree is empty. */
    public boolean isEmpty() {
        return (size == 0);
    }

    /** Returns whether a node is internal. */
    public boolean isInternal(Position<E> v) throws InvalidPositionException {
        return !isExternal(v);
    }

    /** Returns whether a node is external. */
    public boolean isExternal(Position<E> v) throws InvalidPositionException {
        TreePosition<E> vv = checkPosition(v); // auxiliary method
        return (vv.getChildren() == null) || vv.getChildren().isEmpty();
    }

    /** Returns whether a node is the root. */
    public boolean isRoot(Position<E> v) throws InvalidPositionException {
        checkPosition(v);
        return (v == root());
    }

    /** Returns the root of the tree. */
    public Position<E> root() throws EmptyTreeException {
        if (root == null)
            throw new EmptyTreeException("The tree is empty");
        return root;
    }


    /** Returns the parent of a node. */
    public Position<E> parent(Position<E> v) throws InvalidPositionException,
            BoundaryViolationException {
        TreePosition<E> vv = checkPosition(v);
        Position<E> parentPos = vv.getParent();
        if (parentPos == null)
            throw new BoundaryViolationException("No parent");
        return parentPos;
    }

    /** Returns an iterable collection of the children of a node. */
    public Iterable<Position<E>> children(Position<E> v)
            throws InvalidPositionException {
        TreePosition<E> vv = checkPosition(v);
        if (isExternal(v))
            throw new InvalidPositionException(
                    "External nodes have no children");
        return vv.getChildren();
    }

    /** Returns an iterable collection of the tree nodes. */
    public Iterable<Position<E>> positions() {
        PositionList<Position<E>> positions = new NodePositionList<Position<E>>();
        if (size != 0)
            preorderPositions(root(), positions); // assign positions in
                                                    // preorder
        return positions;
    }

    /** Returns an iterator of the elements stored at the nodes */
    public Iterator<E> iterator() {
        Iterable<Position<E>> positions = positions();
        PositionList<E> elements = new NodePositionList<E>();
        for (Position<E> pos : positions)
            elements.addLast(pos.element());
        return elements.iterator(); // An iterator of elements
    }

    /** Replaces the element at a node. */
    public E replace(Position<E> v, E o) throws InvalidPositionException {
        TreePosition<E> vv = checkPosition(v);
        E temp = v.element();
        vv.setElement(o);
        return temp;
    }


    /** Adds a root node to an empty tree */
    public Position<E> addRoot(E e) throws NonEmptyTreeException {
        if (!isEmpty())
            throw new NonEmptyTreeException("Tree already has a root");
        size = 1;
        root = createNode(e, null, null);
        return root;
    }

    /** Swap the elements at two nodes */
    public void swapElements(Position<E> v, Position<E> w)
            throws InvalidPositionException {
        TreePosition<E> vv = checkPosition(v);
        TreePosition<E> ww = checkPosition(w);
        E temp = w.element();
        ww.setElement(v.element());
        vv.setElement(temp);
    }


    /** If v is a good tree node, cast to TreePosition, else throw exception */
    protected TreePosition<E> checkPosition(Position<E> v)
            throws InvalidPositionException {
        if (v == null || !(v instanceof TreePosition))
            throw new InvalidPositionException("The position is invalid");
        return (TreePosition<E>) v;
    }

    /** Creates a new tree node */
    protected TreePosition<E> createNode(E element, TreePosition<E> parent,
            PositionList<Position<E>> children) {
        return new TreeNode<E>(element, parent, children);
    }

    /**
     * Creates a list storing the the nodes in the subtree of a node, ordered
     * according to the preorder traversal of the subtree.
     */
    protected void preorderPositions(Position<E> v,
            PositionList<Position<E>> pos) throws InvalidPositionException {
        pos.addLast(v);
        for (Position<E> w : children(v))
            preorderPositions(w, pos); // recurse on each child
    }

    public Iterator<E> iteratorO() {
        return null;
    }

    public boolean islnternal(Position<E> v) throws InvalidPositionException {
        return false;
    }



    public static void main(String[] args) {

        LinkedTree<Character> T = new LinkedTree();

        // add root
        T.addRoot('A');

        // add children of root
        T.createNode('B', (TreeNode) (T.root()), new NodePositionList());
        TreePosition C = T.createNode('C', (TreeNode) (T.root()),
                new NodePositionList());
        T.createNode('D', (TreeNode) (T.root()), new NodePositionList());

        // add children of node C

        T.createNode('E', C, new NodePositionList());
        TreePosition F = T.createNode('F', C, new NodePositionList());
        T.createNode('G', C, new NodePositionList());

        // add childrn of Node F
        T.createNode('H', F, new NodePositionList());
        T.createNode('I', F, new NodePositionList());

        // print out tree

        System.out.println("Size = " + T.size());

    }

}

1 个答案:

答案 0 :(得分:1)

简单。 size的值未充分设置。只有两个地方可以访问大小进行写入。

Line 4:     protected int size; // number of nodes
Line 8:         size = 0;
Line 12:     public int size() {
Line 13:         return size;
Line 18:         return (size == 0);
Line 69:         if (size != 0)
Line 97:         size = 1;
Line 173:         System.out.println("Size = " + T.size());

这是你的size()方法:

/** Returns the number of nodes in the tree. */
public int size() {
    return size;
}

主要问题似乎是size本质上是多余的。但是,它会阻止您解析整个树以确定元素计数,因此可以将其视为缓存。

正如您现在所经历的那样,缓存和其他冗余信息的一般问题是,您需要仔细跟踪它们并使它们保持最新。战略性地放置一些assert语句可以极大地帮助您完成该任务。