节点树<t>解释</t>

时间:2012-04-24 01:36:05

标签: java tree

不知道我是否被允许按照网站规则这样做...但我会抓住机会......请耐心等待我,我只是一名学生...: - )< / p>

我有一个大学任务......我很难理解课程应该做什么...我已经在三个不同的场合去找我的老师,我从他那里得到的答案根本没有帮助。无论如何,作业细节如下......

创建一个名为Tree的类,它充当节点的容器。树类应该支持以下方法。

  

public void add(Node parent,Node child){} - 将新子节点添加到父节点

     

public void removeChild(Nodeparent,Node child){} - 从父级删除子节点。

     

public Node getRootNode(){} - 返回树的根

     

public void setRoot(Node root){} - 设置树的根节点

     

public boolean contains(T data){} - 在树中搜索给定类型

     

public void dfs(Node child){} - 执行树的深度优先搜索并输出每个节点(缩进)

     

public void bfs(Node child){} - 执行树的广度优先搜索并输出每个节点(缩进)

  1. 应该对树类进行参数化以处理泛型类型T,从而允许创建字符串,文件等的树... Tree<String> tree = new Tree<String>()
  2. 树类应使用邻接列表实现树结构,并按以下方式定义:Map<Node<T>, List<Node<T>>> tree = new HashMap<Node<T>, List<Node<T>>();
  3. 还应该对节点类进行参数化以处理泛型类型T并公开几种方法......

    现在我编写了我的Node类,它工作正常......说实话,我确信我已经编写了一个创建树的Node类。但在阅读了Tree类描述之后我很困惑。我应该在树图中存储什么。我很难想象整个事情。

    也许有人可以解释老师想要什么并让我朝着正确的方向前进。我寻找代码本身...只是想了解我想要做的事情。

    我的节点类

    public class Node<T> 
    {
        private Node<T> root; // a T type variable to store the root of the list
        private Node<T> parent; // a T type variable to store the parent of the list
        private T child;
        private List<Node<T>> children = new ArrayList<Node<T>>(); // a T type list to store the children of the list
    
        // default constructor
        public Node(T child)
        {
            setParent(null);
            setRoot(null);
            setItem(child);
        }
    
        // constructor overloading to set the parent
        public Node(Node<T> parent)
        {
            this.setParent(parent);
            //this.addChild(parent);
        }
    
        // constructor overloading to set the parent of the list  
        public Node(Node<T> parent, Node<T> child)
        {
            this(parent);
            this.children.add(child);
        }
    
        /**
        * This method doesn't return anything and takes a parameter of 
        * the object type you are trying to store in the node 
        * 
        * @param  Obj  an object
        * @param 
        **/
        public void addChild(Node<T> child)
        {
            child.root = null;
            child.setParent((Node<T>)this);
            this.children.add(child); // add this child to the list
        }
    
        public void removeChild(Node<T> child)
        {
            this.children.remove(child); // remove this child from the list
        }
    
        public Node<T> getRoot() {
            return root;
        }
    
        public boolean isRoot()
        {
            // check to see if the root is null if yes then return true else return false
            return this.root != null;     
        }
    
        public void setRoot(Node<T> root) {
            this.root = root;
        }
    
        public Node<T> getParent() {
            return parent;
        }
    
        public void setParent(Node<T> parent) {
            this.parent = parent;
        }
    
        public T getItem() {
            return child;
        }
    
        public void setItem(T child) {
            this.child = child;
        }
    
        public boolean hasChildren()
        {
            return this.children.size()>0;
        }
    
        @SuppressWarnings("unchecked")
        public Node<T>[] children()
        {
            return (Node<T>[]) children.toArray(new Node[children.size()]);
        }
    
        @SuppressWarnings({ "unchecked"})
        public Node<T>[] getSiblings()
        {
            if(this.isRoot()!=false && parent==null)
            {
                System.out.println("this is root or there are no siblings");
                return null;
            }
            else{
                List<Node<T>> siblings = new ArrayList<Node<T>>((Collection<? extends Node<T>>) Arrays.asList(new Node[this.parent.children.size()]));  
                Collections.copy(siblings, this.parent.children);  
                siblings.remove(this);
                return siblings.toArray(new Node[siblings.size()]);
            }
        }
    }
    

3 个答案:

答案 0 :(得分:7)

您可以使用地图进行以下操作:

hashmap的关键是给定节点,hashmap的值是给定节点的子节点。

public class Tree<T> {
    private Node<T> rootNode;
    private HashMap<Node<T>, List<Node<T>> tree;

    //and then some kind of function to go through the tree.
    public void expandNode(Node<T> node) {
        if (tree.get(node) == null) {
            System.out.println(node);
            return;
        }
        for(Node<T> n : tree.get(node)) {
            System.out.println(node);
            expandNode(n);
        }
    }
}

我可以说清楚树是如何工作的吗?

答案 1 :(得分:0)

看看列表中的2个点,我会猜测第1点对你来说是最令人困惑的。

树本身可以参数化。树类的类型参数可以在用于创建节点的内部类中使用。也就是说,出于赋值的目的,节点类可能应该在Tree类中,以便它可以使用给予Tree类的T类型参数。

这样,树可以存储任何东西(字符串,文件,双打等)。 Node类只是一种存储任何类型T对象的好方法。

答案 2 :(得分:0)

这有点奇怪。如果我这样做并且作业没有另外说明,我可能根本不会写一个Tree类;我只是使用Node作为递归数据结构,并让树的根节点代表整个树。

由于它看起来不像你应该这样做,你可以让Tree<T>成为一个包含单个Node<T>对象引用的包装类。您可以将所需方法的逻辑放在任一类中。

代码的开头可能如下所示:

public class Tree<T> {

    private Node<T> root;

    public Tree(Node<T> root) {
        this.root = root;
    }

}