开发者

Recursive toString() method in a binary search tree. What is the time complexity for this?

开发者 https://www.devze.com 2023-02-11 14:30 出处:网络
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开发者_如何学JAVA in order and convert them

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开发者_如何学JAVA 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.


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)).


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消