如何使树具有多种类型的节点,并且每个节点在Java中可以具有多个子节点

悉达思(Siddharth Sinha)

基本上我正在尝试实现这样的事情,其中​​合作伙伴节点为“ type1”,客户端节点为“ type2”,用户节点为“ type3”。每个节点可以具有多个子节点。因此,Partner1下可以有任意数量的客户端节点,类似地,客户端节点下可以有任意数量的用户。

在此处输入图片说明

我已经开始实现,但是现在被卡住了。我编写的代码如下。

public class ClientProperty {
    public class Root{}         //NodeType1

    public class Partner{       //NodeType2
        public String partner_id;
        public String partner_name;
        public int partner_node_id;

        public Partner(String partner_id,String partner_name,int partner_node_id){
            this.partner_id = partner_id;
            this.partner_name = partner_name;
            this.partner_node_id = partner_node_id;
        }
    }

    public class Clients{       //NodeType3
        public String client_name;
        public String client_id;
        public int client_node_id;
        public Map<Enum,List<Enum>> clientproperty = new HashMap<Enum,List<Enum>>();

        public Clients(String client_name, String client_id, int client_node_id,Map<Enum,List<Enum>> clientproperty){
            this.client_name = client_name;
            this.client_id = client_id;
            this.client_node_id = client_node_id;
            this.clientproperty = clientproperty;
        }
    }
    public class Users{         //NodeType4
        public String user_name;
        public String user_id;
        public int user_node_id;

        public Users(String user_id,String user_name, int user_node_id){
            this.user_id = user_id;
            this.user_name = user_name;
            this.user_node_id = user_node_id;
        }
    }
    public class Node{
        Node next;
        Object nodes;

        public Node(){
            next = null;
        }

        public Node(Object nodes, Node next){
            this.nodes = nodes;
            this.next = next;
        }
    }
}

让我知道是否需要一些见解

幽灵猫

首先是一些更具体的事情:

You want to read about data encapsulation. Putting all public fields on your classes is simply wrong. You actually want to hide such information as far as possible.

Then you want to read about java coding style conventions; as you are violating quite some of them (which simply doesn't help when you show your code to more experienced java coders).

Finally, most important: you want to read quite a bit about OO design in general (I recommend "Agile practices" by Robert Martin; there is a free PDF of the "C# version" of that book):

It starts wit the fact that

a) being a client/user is a "different responsibility" than

b) being some element in a graph

换句话说:您在类中投入了太多的“角色”。

含义:您想引入各种抽象。例如:

interface GraphNode<N, C extends GraphNode> {
    N getNodeContent();
    List<C> getChildrenNodes();
}

现在您可以表达:任何“节点”确实具有某些内容(可以是用户或客户端或任何对象);它具有一个(或一组)子代(也称为“节点”)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

每个节点具有多个子节点的树的搜索方法

返回节点列表Java父级中的树可以具有多个子节点

打印具有多个子节点的节点的数据[Java]

C中具有任意多个子节点的树节点

减少一棵树的节点数,以获得具有多个子节点的节点

从根到具有多个子树的树中给定节点的打印路径

每个节点有两个子节点的树的节点数

有没有办法在 Java 中实现具有多个子节点的 LinkedList?

当根节点可能具有多种类型时,将名称空间添加到根节点

Java-具有多个节点的树数据结构-如何有效搜索

具有两种类型的节点的图

如何使用beautifulsoup将具有多个子节点的父节点和子节点信息提取到一个数据帧中?

是否可以使用networkx为每个节点绘制具有多种颜色的图形

具有多个子节点的 Oracle XML 查询

如何在C中实现具有多个节点的链表?

验证给定级别的所有节点在二叉树中是否具有不同的值

PDDL中的变量可以具有多种类型吗?

读取具有多个子节点的xml文件只会返回第一个节点

具有包含数组的节点的JavaScript树

如何检查Firebase中是否存在具有多个字段的节点(子节点)?

当多个节点具有相同名称时,如何在XML中编辑特定节点的值?

如何使用xmlstarlet附加具有多个子节点的xml文件?

Java从树中找到具有递归功能的节点

Java中具有多种类型的值的映射

在具有完整路径的树中查找匹配的节点

将具有多个子节点的Firebase节点复制到另一个节点

Linux设备树,具有多个中断父节点的节点

如何为具有多个类和属性的树视图节点分配标签

在决策树中,如果我的节点具有多个分支,应该使用什么日志库?