B 树实现 - 我是否应该将 Node 类设为静态成员类?

发布于 2024-09-14 21:08:27 字数 866 浏览 8 评论 0原文

我需要为大学实现一棵 B 树:

我有一个“外部”类 B 树,其属性为 root 和 _ Degree。表示节点的类被实现为静态成员类:

public class BTree<E> {
    private Node<E> root;
    // the minimal degree
    private int degree;

    public BTree(int degree) {
        if (degree < 2) {
            throw new IllegalArgumentException();
        }

        this.degree = degree;
    }

    // other stuff

    private static class Node<T> {
        T[] elements       = (T[])new Object[degree * 2 - 1];
        Node<T>[] children = (Node<T>[])new Object[degree * 2];
        int size           = 0;
    }
}

所以,现在我的问题是: 当我将 Node 类实现为静态成员类时,我无法访问外部类的 Degree 属性。

现在我必须选择:

  1. 使 Node 类成为内部类(非静态成员类)或
  2. 为 Node 类创建一个构造函数,并在每次需要构造 Node 时传递度数。

最好的选择是什么?使其成为内部类意味着节点都将引用 Btree(外部类),但使其成为静态成员类意味着我每次都必须传递学位。

I need to implement a B-Tree for University:

I have an "outer" class B-Tree with attributes root and _degree. The class to represent the nodes is implemented as a static-member class:

public class BTree<E> {
    private Node<E> root;
    // the minimal degree
    private int degree;

    public BTree(int degree) {
        if (degree < 2) {
            throw new IllegalArgumentException();
        }

        this.degree = degree;
    }

    // other stuff

    private static class Node<T> {
        T[] elements       = (T[])new Object[degree * 2 - 1];
        Node<T>[] children = (Node<T>[])new Object[degree * 2];
        int size           = 0;
    }
}

So, now my problem is: As I implemented the Node class as a static member class, I can't access the degree attribute of the outer class.

Now I have to choices:

  1. Make the Node class an inner class (non-static member class) OR
  2. Create a constructor for the Node class and pass the degree in every time I need to construct a Node.

What would be the best choice? Making it an inner class would mean the Nodes would all have a reference to the Btree (outer class), but making it a static member class would mean I would have to pass the degree in every time.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那一片橙海, 2024-09-21 21:08:27

我会将其保持静态并传递学位。这样您就可以确保 Node 无法了解有关 BTree 的任何详细信息。

I would keep it static and pass degree in. That way you ensure that Node cannot know any details about BTree.

看透却不说透 2024-09-21 21:08:27

如果是我,我会将 Node 类公开,这样我就可以在其他包含数据结构中重用它,但这只是我。在这种情况下,我必须通过构造函数传递学位,这对我来说也没关系。我不喜欢内部类操纵封闭类的成员的想法。我觉得这使得各个班级之间的联系过于紧密。我知道有时这是合适的,但我会尽可能避免,这似乎是一个很容易避免的情况。

If it were me, I'd have the Node class public so I could reuse it in other containing data structures, but that's just me. In that case, I'd have to pass the degree through the constructor, and that's OK with me too. I don't like the idea of inner classes manipulating the members of enclosing classes. I feel it makes the classes too tightly bound to each other. I know sometimes it's appropriate, but I avoid when I can and this seems an easily avoidable case.

七色彩虹 2024-09-21 21:08:27

有观点认为它是静态的,因为它解耦了类。

但我认为 BTree.Node 是特定 BTree 的节点。创建一堆节点(具有随机度数)是没有意义的。你不能有节点,但没有树。因此,我说非静态。

There are arguments to make it static, because it decouples the classes.

But I think a BTree.Node is a node from a particular BTree. It doesn't make sense to go and create a bunch of Nodes (with random degrees). You can't have a Node, but no Tree. Thus, I say non-static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文