Time complexity of a stack-based tree traversal
What is the time complexity of the implementation of a binary tree traversal below?
void Tree::nonRecInOrder()
{
// nonrecursive inOrder Traversal using Stack
Stack< TreeNode* > s ; // declare and initialize stack
TreeNode* currentNode = root ;
while( true )
{
while( currentNode )
{
// move down leftChild fields
s.add( currentNode ) ;
currentNode = currentNode->leftChild ;
}
if( ! s.isEmpty() ) // stack is not empty
{
currentNode = *s.del( currentNode ) ; // delete from stack
cout << currentNode->data ;
currentNode = currentNode->rightChild ;
}
else
{
break ;
}
}
}
Could you also explain how to calculate the complexit开发者_如何学运维y?
One way to characterize the big-O complexity of a difficult function is to think about some resource that it accesses and then to bound the number of times that that resource is accessed. In this case, when you're doing a tree traversal, you might think about the number of times each node is pushed or popped from the stack. The reason this is a good bound is that all the hard work of this function - the inner loop descending down through a chain of nodes and the outer loop processing the topmost on the stack - can be bounded by the number of times a node is pushed onto the stack or popped from the stack. This is because the outermost loop terminates when the stack is empty, so it can't run more times than the stack has something pushed onto it, and the innermost loop does work proportional to the number of times something is pushed onto the stack over the course of the loop.
So let's see how we can bound these quantities. The first question is how many times each node can be added to the stack. Well, a node is only added to the stack if it or one of its ancestors along a left-only path is the current node when the loop starts executing. How many times can this happen? My claim is that this occurs at most once. The proof of this is an induction based on the depth of the node in the tree. We use the observation that a node is only chosen again as the current node if it is the direct right child of a node in the stack. As a base case of the induction, if the node is the root (it's at depth zero), then it can't be chosen a second time because it has no parent. For the inductive step, if it's true that no node at depth d can be chosen as the current node twice, then no node at depth d + 1 can be chosen twice because nodes at depth d + 1 are chosen only if their parents are chosen again, but by the inductive assumption we know this not to be true. Consequently, we have that no node is ever chosen as the current node twice. We follow this up with a simple observation that no node that's a left child can ever be the current node at the start of the loop, since the current node is either the root (not a left child) or was the right child of some node. This claim, compounded with the fact that no node is visited twice, means that a node is only added to the queue at most once, which happens when its highest left ancestor becomes the current node.
This also gives us a bound on the number of dequeues that are possible, since the number of dequeues can't exceed the number of enqueues. Since each node is enqueued at most once, it's also dequeued at most once. To finish things off, we know that the complexity of the whole function is bounded by the number of enqueues and dequeues performed, and so the complexity is O(n), where n is the number of nodes.
Whew! That was not fun to analyze. I like the recursive version a lot more. :-)
精彩评论