二叉搜索树中的递归 toString() 方法。这个的时间复杂度是多少?

发布于 2024-10-18 03:32:14 字数 1019 浏览 6 评论 0原文

我是 Java 初学者,正在寻求帮助。

所以我用 Java 制作了这个二叉树,我应该实现一个方法,按顺序对所有元素进行排序并将它们转换为字符串。应该很像前任吧“[1,2,3,4]”。我使用 StringBuilder 来做到这一点。

我的方法代码看起来像这样:

/**
 * Converts all nodes in current tree to a string. The string consists of
 * all elements, in order.
 * Complexity: ?
 * 
 * @return string
 */
public String toString() {
    StringBuilder string = new StringBuilder("[");
    helpToString(root, string);
    string.append("]");
    return string.toString();
}

/**
 * Recursive help method for toString. 
 * 
 * @param node
 * @param string
 */
private void helpToString(Node<T> node, StringBuilder string) {
    if (node == null)
        return; // Tree is empty, so leave.

    if (node.left != null) {
        helpToString(node.left, string);
        string.append(", ");
    }

    string.append(node.data);

    if (node.right != null) {
        string.append(", ");
        helpToString(node.right, string);
    }
}

所以我的问题是,如何计算这个的时间复杂度?另外,如果有任何关于如何改进此方法的建议,我将非常感激。

I'm a beginner in Java and looking for some help.

So I've made this binary tree in Java, and I'm supposed to implement a method which sorts all elements in order and convert them into a string. It's supposed to look like ex. "[1, 2, 3, 4]". I used the StringBuilder in order to do this.

My code for the method looks loke this:

/**
 * Converts all nodes in current tree to a string. The string consists of
 * all elements, in order.
 * Complexity: ?
 * 
 * @return string
 */
public String toString() {
    StringBuilder string = new StringBuilder("[");
    helpToString(root, string);
    string.append("]");
    return string.toString();
}

/**
 * Recursive help method for toString. 
 * 
 * @param node
 * @param string
 */
private void helpToString(Node<T> node, StringBuilder string) {
    if (node == null)
        return; // Tree is empty, so leave.

    if (node.left != null) {
        helpToString(node.left, string);
        string.append(", ");
    }

    string.append(node.data);

    if (node.right != null) {
        string.append(", ");
        helpToString(node.right, string);
    }
}

So my question is, how do I calculate the time complexity for this? Also, if there are any suggestions in how to make this method better, I would gladly appreciate it.

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

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

发布评论

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

评论(3

花伊自在美 2024-10-25 03:32:14

最简单的答案是:O(n)。您访问每个节点一次并执行一 (a) 量的工作。计算过程如下

O(a*n)

,因为我们忽略常数因子,所以简单的答案是

O(n)

但是。也有人可能会争辩说,你只是多做了一点:每次访问没有叶子的地方时,你都会返回 null 。这又是一项需要完成的工作。

我们暂时称这些为隐形叶子。按照这个想法,树中的每个值都是一个节点,它有一个或两个不可见叶子

现在,我们需要执行以下操作(对于每个节点):

a       | if a node has two child nodes
a + b   | if a node has one child node
a + 2b  | if a node has no child node.

对于最坏情况的二叉树(完全不平衡),我们有 (n-1) 个带有一个子节点的节点和一个没有子节点的节点:

  "1"
    \
    "2"
      \
      "3"
        \
        "4"

所以 计算

     (n-1)*(a+b) + b
 <=> an + bn - a - b + b
 <=> n(a+b) + b

  => O(an + bn)  // we ignore `+ b` because we always look at really big n's

幸运的是,即使在最坏的时间场景下,复杂度仍然是线性的。只有常数高于简单答案中的常数。在大多数情况下 - 通常当需要 Big-O 来比较算法时 - 我们甚至忽略这个因素,并且我们很高兴地说,算法时间复杂度是线性的 (O(n))。

The easiest answer is: O(n). You visit every node once and do one (a) amount of work. The calculation would go like

O(a*n)

and because we ignore constant factors, the simple answer would be

O(n)

But. One could also argue, your doing just a little bit more: you return null each time you visit a place where there is no leaf. This again is one (b) amount of work to be done.

Let's call those invisible leaves for a moment. Following this idea, every value in the tree is a node which has one or two invisible leafs.

So now, we have the following to do (for each node):

a       | if a node has two child nodes
a + b   | if a node has one child node
a + 2b  | if a node has no child node.

for a worst case binary tree (totally unbalanced), we have (n-1) nodes with one child node and one node with no child node:

  "1"
    \
    "2"
      \
      "3"
        \
        "4"

And so, the calculation is

     (n-1)*(a+b) + b
 <=> an + bn - a - b + b
 <=> n(a+b) + b

  => O(an + bn)  // we ignore `+ b` because we always look at really big n's

Fortunately, even in a worst case time scenario, complexity is still linear. Only the constant is higher than in the easy answer. In most cases - usually when Big-O is needed to compare algorithms - we even ignore the factor and we're happy to say, that the algorithms time complexity is linear (O(n)).

空‖城人不在 2024-10-25 03:32:14

时间复杂度为 O(n),因为您访问每个节点一次。为了在树上行走,你没有比这更好的了。

The time complexity is O(n) since you are visiting every node once. You cannot do any better than that in order walk the tree.

一念一轮回 2024-10-25 03:32:14

时间复杂度与树中节点的数量成线性关系:您只访问每个节点一次。

Time complexity is linear in the number of nodes in the tree: you are visiting each node exactly once.

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