当前位置: 首页 > 知识库问答 >
问题:

节点解释树

王棋
2023-03-14

不知道根据站点的规则我是否被允许这么做...但我会抓住机会...请你忍耐一下,我只是个学生...:-)

我有个大学作业...我很难理解类应该做什么。我曾三次去找我的老师,他给我的答案一点帮助也没有。总之,分配的详细信息如下所示...

创建一个名为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 tree=new tree ()
  2. 树类应使用邻接列表实现树结构,并以以下方式定义:map ,list >tree=new hashmap ,list >();

node类也应该被参数化以处理泛型类型T并公开几个方法...

现在我已经编写了我的节点类,它运行良好...老实说,我确信我已经编写了一个节点类,它正在创建一个树。但是看完树类的描述后,我感到很困惑。我应该存储在树图中的内容。我很难想象整件事。

也许有人能解释一下老师想要什么,把我带到正确的方向。我不是在找代码本身...只是想知道我该做什么。

我的节点类

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()]);
        }
    }
}

共有1个答案

雍俊远
2023-03-14

您可以使用该映射进行以下操作:

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);
        }
    }
}

我能说清楚这棵树是怎么工作的吗??

 类似资料:
  • 字节码的解释执行和AST的解释执行有类似之处,而且更简单,因为树形结构已经展开成顺序了,以栈虚拟机为例,为方便起见,假设所有的指令都在一个指令数组里,每个元素是一个指令对象,有code和arg两个属性,解释器入口: Object execute(Inst[] inst_list, Object[] func_arg); 由于continue和break已经被jmp指令代替了,这里我们认为exec

  • 我使用 据我所知,celery将此任务转换为消息,并通过AMQP协议发送给代理(redis或rabbitmq)。然后将这些消息排队并传递给工作节点以处理该消息。 我的问题是, null

  • 如果我没弄错的话,树通常是一个列表,其中的元素按特定顺序排列。孩子们不在他们自己的子列表中,他们都在同一个列表中。 所以,我试图创建一个Tree类,其中包含TreeNodes(类)使用Tree类中的List。 我如何跟踪父母/孩子/叶子?如果父母“父母1”,有两个孩子“孩子A”和“孩子B”,我如何将他们联系在一起?

  • 问题内容: 我遇到了一些有关JVM / JIT活动的参考,其中似乎在编译字节码和解释字节码之间有区别。该特定注释声明的字节码在前10000次运行时进行解释,然后进行编译。 “编译”和“解释”字节码之间有什么区别? 问题答案: 解释字节码基本上是逐行读取字节码,不进行任何优化或任何操作,然后对其进行解析并实时执行。由于许多原因,这种方法效率低下,其中包括Java字节码设计得不能快速解释的问题。 编译

  • 我正在使用断点在 Android Studio 中调试我的 Android 项目。 我注意到有时我的断点中有一个叉号,并且不会暂停执行。 在intellij站点上,这被解释为:“当断点被设置在注释行或不可执行行上时显示,指示这样的断点不会被命中。” 谁能告诉我上面的总结如何解释以下断点的行为: 为什么第二个断点有一个勾号?我很困惑!

  • 问题内容: 在CPython的上下文中,我并没有真正理解“字节码解释器”的概念。有人可以说明整个情况吗? 这是否意味着CPython将编译并执行pyc文件(字节码文件?)。那么什么将py文件编译为pyc文件呢?而且Jython与CPython有何不同(除了它们以不同的语言实现)。 我还在某处读到Python是C ++的解释。这个对吗?那是什么意思呢? 我对Python还是很陌生,所以请原谅我这个愚