更改“自定义”根目录树与树的另一个节点

发布于 2025-01-11 03:22:39 字数 2529 浏览 1 评论 0原文

基本上我有一个类 Node,它包含一个 IP、它的父节点和一个子节点列表:

public class Node implements Comparable<Node> {

    private final IP address;
    private List<Node> children;
    private Node parent;

    /**
     * @param address  IP-Address
     * @param children List of children this node has
     */

    public Node(IP address, List<Node> children) {
        this.address = address;
        this.children = children;
        if (!children.isEmpty()) {
            for (Node child : children) {
                child.parent = this;
            }
            Collections.sort(children);
        }
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    public List<Node> getChildren() {
        return children;
    }

    public IP getAddress() {
        return address;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    @Override
    public int compareTo(Node o) {
        return this.address.compareTo(o.address);
    }
}

所以树结构可以如下所示: 以 85 为根的树 (这些数字将 IP 的第一个八位字节显示为十进制)

并且根已更改的树如下所示: 根部改变的树 请注意,我将之前的根 85 更改为 39

现在我需要帮助的是找到一种良好且干净的方法来更改每个节点的根和父/子节点。

现在我的方法看起来像这样,但它非常丑陋:

public void changeRoot(IP newRoot, Node newParent) {
        Node node = getAsNode(newRoot);
        if (newParent == null) {
            changeRoot(node.getParent().getAddress(), node);
            node.getChildren().add(node.getParent());
            node.setParent(newParent);
        } else if (node.getParent() == null) {
            node.getChildren().remove(newParent);
            node.setParent(newParent);
        } else {
            changeRoot(node.getParent().getAddress(), node);
            node.getChildren().add(node.getParent());
            node.getChildren().remove(newParent);
            node.setParent(newParent);
        }
        if (node.getParent() == null) {
            network.remove(root);
            root = node;
            network.add(root);
        }
    }

在类网络中,我存储当前的根节点、该网络中所有节点的列表以及包含根节点和可能的子网的网络列表。

public class Network extends AddressParser{

    List<Node> network = new ArrayList<>();
 
    SortedSet<Node> allNodes = new TreeSet<>();

    Node root;
}

提前感谢您的帮助!

Basically what I have is a class Node, which contains an IP, it's parent node and a list of children:

public class Node implements Comparable<Node> {

    private final IP address;
    private List<Node> children;
    private Node parent;

    /**
     * @param address  IP-Address
     * @param children List of children this node has
     */

    public Node(IP address, List<Node> children) {
        this.address = address;
        this.children = children;
        if (!children.isEmpty()) {
            for (Node child : children) {
                child.parent = this;
            }
            Collections.sort(children);
        }
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    public List<Node> getChildren() {
        return children;
    }

    public IP getAddress() {
        return address;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    @Override
    public int compareTo(Node o) {
        return this.address.compareTo(o.address);
    }
}

So an tree structure can look like this: Tree with 85 as root
(the numbers are displaying the first octet of an IP as decimal)

And a tree with changed root looks like this: Tree with changed root
note that i changed the prior root 85 with 39

Now where i need help is to finde a good and clean way to change the root and the parent/children of every node.

Right now my methode looks like this but it's very ugly:

public void changeRoot(IP newRoot, Node newParent) {
        Node node = getAsNode(newRoot);
        if (newParent == null) {
            changeRoot(node.getParent().getAddress(), node);
            node.getChildren().add(node.getParent());
            node.setParent(newParent);
        } else if (node.getParent() == null) {
            node.getChildren().remove(newParent);
            node.setParent(newParent);
        } else {
            changeRoot(node.getParent().getAddress(), node);
            node.getChildren().add(node.getParent());
            node.getChildren().remove(newParent);
            node.setParent(newParent);
        }
        if (node.getParent() == null) {
            network.remove(root);
            root = node;
            network.add(root);
        }
    }

And in the class network i am storing the current root node, a list of all nodes in this network snd a list the network which contains the root node and possible subnetworks.

public class Network extends AddressParser{

    List<Node> network = new ArrayList<>();
 
    SortedSet<Node> allNodes = new TreeSet<>();

    Node root;
}

Thanks for your help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文