[英]Recursive toString() method in a binary search tree. What is the time complexity for this?
I'm a beginner in Java and looking for some help. 我是Java的初学者,正在寻求帮助。
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. 因此,我已经用Java制作了这个二叉树,并且我应该实现一种对所有元素进行排序并将它们转换为字符串的方法。 It's supposed to look like ex. 它应该看起来像前。 "[1, 2, 3, 4]". “ [1、2、3、4]”。 I used the StringBuilder in order to do this. 我使用StringBuilder来做到这一点。
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)
. 最简单的答案是: O(n)
。 You visit every node once and do one (a) amount of work. 您访问每个节点一次并完成一(a)项工作。 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. 可能还会有人争辩,您所做的只是多一点:每次访问没有叶子的地方,您都返回null
。 This again is one (b) amount of work to be done. 这又是一项(b)要做的工作。
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: 对于最坏情况的二叉树(完全不平衡),我们有(n-1)个节点,其中有一个子节点和一个没有子节点的节点:
"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)). 在大多数情况下-通常在需要Big-O比较算法时-我们甚至忽略了该因素,并且很高兴地说,算法的时间复杂度是线性的(O(n))。
The time complexity is O(n) since you are visiting every node once. 由于您访问每个节点一次,因此时间复杂度为O(n)。 You cannot do any better than that in order walk the tree. 为了走这棵树,你别无其他。
时间复杂度在树中的节点数中呈线性关系:您正在访问每个节点一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.