节点树<T>解释

A G*_*ani 4 java tree

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

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

创建一个名为的节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>>();

还应该对节点类进行参数化以处理泛型类型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()]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jua*_*tti 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以说清楚这棵树的运作方式吗?