查找树的有限函数节点的位置值 - Java

时间:2014-09-04 10:01:10

标签: java tree

好的,我有一个独特的问题。因此,问题的一个子部分是获取树的父节点的值。但是没有像函数一样的getPosition或getValue。

此部分可用的唯一功能是:

  1. Tree.parent(p) - >采取一个位置 - 返回p
  2. 的父级的位置
  3. Tree.children(p) - >它也采用了一个位置参数,但是却吐出了它的孩子的字符串列表
  4. - 编辑:位置是您提供它的树中的位置。

    我试图找出如何获取父节点的值(节点的内容),当从任何东西中获取值的唯一方法是使用打印出的子方法父节点的值以及如何识别正确的节点。我认为这与升级和获得它的父母有关吗?但它让我的思绪有点扭曲。

    P.S开始我的遍历我只是在做tree.children(0) - 根。

    Edit2:树的类型是:

    Tree<String> tree
    
    • 不确定这是否有帮助,并且该文件的唯一导入是:

    import adt。*;

    import java.util。*;

    树具有以下功能:

    package adt;

    import java.util.*;
    
    /**
     * An interface for a tree. A tree may contain zero or more nodes.
     * 
     * - taken and adapted from Goodrich, Tamassia and Goldwasser (6th Ed)
     * 
     * @param <E>, the type of the objects to be stored in the tree.
     * 
     */
    public interface Tree<E> {
    
        /**
         * Returns the number of nodes in the tree.
         * 
         * @return number of nodes in the tree
         */
        int size();
    
        /**
         * Tests whether the tree is empty.
         * 
         * @return true if the tree is empty, false otherwise
         */
        boolean isEmpty();
    
        /**
         * Returns the root Position of the tree (or null if tree is empty).
         * 
         * @return root Position of the tree (or null if tree is empty)
         */
        Position<E> root();
    
        /**
         * Returns the Position of p's parent (or null if p is root).
         *
         * @param p
         *            A valid Position within the tree
         * @return Position of p's parent (or null if p is root)
         * @throws IllegalArgumentException
         *             if p is not a valid Position for this tree.
         */
        Position<E> parent(Position<E> p) throws IllegalArgumentException;
    
        /**
         * Returns an iterable collection of the Positions representing p's
         * children.
         * 
         * NOTE: For efficiency reasons, the list of returned children may be part
         * of the internal state of the tree, and so must not be modified. The
         * internal consistency of the tree, and its future behaviour are not
         * defined if the list of children returned by this method is modified.
         *
         * @param p
         *            A valid Position within the tree
         * @return iterable collection of the Positions of p's children
         * @throws IllegalArgumentException
         *             if p is not a valid Position for this tree.
         */
        List<Position<E>> children(Position<E> p) throws IllegalArgumentException;
    
        /**
         * Returns the number of children of Position p.
         *
         * @param p
         *            A valid Position within the tree
         * @return number of children of Position p
         * @throws IllegalArgumentException
         *             if p is not a valid Position for this tree.
         */
        int numChildren(Position<E> p) throws IllegalArgumentException;
    
        /**
         * Returns true if Position p represents the root of the tree.
         *
         * @param p
         *            A valid Position within the tree
         * @return true if p is the root of the tree, false otherwise
         */
        boolean isRoot(Position<E> p);
    
        /**
         * Returns true if Position p has one or more children.
         *
         * @param p
         *            A valid Position within the tree
         * @return true if p has at least one child, false otherwise
         * @throws IllegalArgumentException
         *             if p is not a valid Position for this tree.
         */
        boolean isInternal(Position<E> p) throws IllegalArgumentException;
    
        /**
         * Returns true if Position p does not have any children.
         *
         * @param p
         *            A valid Position within the tree
         * @return true if p has zero children, false otherwise
         * @throws IllegalArgumentException
         *             if p is not a valid Position for this tree.
         */
        boolean isExternal(Position<E> p) throws IllegalArgumentException;
    
    }
    

0 个答案:

没有答案