Remark:
The department has asked me to make the following announcements.
Our complexity analysis will proceed in a somewhat unusual order. Instead of starting with the bottom (the tree methods in 2.3.1, e.g., is Internal(v)) or the top (the traversals), we will begin by analyzing some middle level procedures assuming the complexities of the low level are as we assert them to be. Then we will analyze the traversals using the middle level routines and finally we will give data structures for trees that achieve our assumed complexity for the low level.
Let's begin!
These will be verified later.
Definitions of depth and height.
Remark: Even our definitions are recursive!
From the recursive definition of depth, the recursive algorithm for its computation essentially writes itself.
Algorithm depth(T,v) if T.isRoot(v) then return 0 else return 1 + depth(T,T.parent(v))
The complexity is Θ(the answer), i.e. Θ(dv), where dv is the depth of v in the tree T.
Problem Set #1, Problem 3:
Rewrite depth(T,v) without using recursion.
This is quite easy. I include it in the problem set to ensure
that you get practice understanding recursive definitions.
The following algorithm computes the height of a position in a tree.
Algorithm height(T,v): if T.isLeaf(v) then return 0 else h←0 for each w in T.children(v) do h←max(h,height(T,w)) return h
Remarks on the above algorithm
Theorem: Let T be a tree with n nodes and let cv be the number of children of node v. The sum of cv over all nodes of the tree is n-1.
Proof:
This is trivial! ... once you figure out what it is saying.
The sum gives the total number of children in a tree. But this almost
all nodes. Indeed, there is just one exception.
What is the exception?
The root.
Corollary: Computing the height of an n-node tree has time complexity Θ(n).
Proof:
Look at the code.
The while loop has cv iterations, so by the theorem the
total number of iterations executed is n-1.
Everything else is Θ(1) per iteration.
Do a few on the board. As mentioned above, becoming facile with recursion is vital for tree analyses.
Definition: A traversal is a systematic way of "visiting" every node in a tree.
Visit the root and then recursively traverse each child. More formally we first give the procedure for a preorder traversal starting at any node and then define a preorder traversal of the entire tree as a preorder traversal of the root.
Algorithm preorder(T,v): visit node v for each child c of v preorder(T,c) Algorithm preorder(T): preorder(T,T.root())
Remarks:
Do a few on the board. As mentioned above, becoming facile with recursion is vital for tree analyses.
Theorem: Preorder traversal of a tree with n nodes has complexity Θ(n).
Proof:
Just like height.
The nonrecursive part of each invocation takes O(1+cv)
There are n invocations and the sum of the c's is n-1.
Homework: R-2.3
First recursively traverse each child then visit the root. More formerly
Algorithm postorder(T,v): for each child c of v postorder(T,c) visit node v Algorithm postorder(T): postorder(T,T.root())
Theorem: Preorder traversal of a tree with n nodes has complexity Θ(n).
Proof: The same as for preorder.