recurrence relation for binary tree height algorithm?

Posted on 1998-11-10
Last Modified: 2008-02-26
what would the recurrence relation be for the following pseudo code algorithm?

void Height(T)
     if (T is not empty)
          lefttreeht = Height(left subtree of T);
          righttreeht = Height(right subtree of T);
          if (lefttreeht > righttreeht)
               return lefttreeht + 1;
               return righttreeht + 1;
          return 0;

Question by:melissak
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 1254160
do you mean big O complexity ?
if so, it's O(log n), n being the number of nodes in the tree.

Arnon David.

Author Comment

ID: 1254161
No, I mean;

     base case C(0)=0

recurrence relation for a tree of n nodes:

     C(n) = C(n-1) + something or nothing
     C(n+1) = C(n) + something or nothing

*parenthesis indicate subscript

this is what I think the format should somewhat be but
it could be something different.  I do know that the
explicit formula should be:


which was developed from some recurrence relation.
LVL 84

Expert Comment

ID: 1254162
Are you assuming the tree is balanced?
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Comment

ID: 1254163
I don't think it matters.  The definition for the height of a
tree is: the number of nodes on the longest path from the root
to a leaf.  So, worst case binary tree would be one that has nodes attatched to the left or right side only (a diagonal).
LVL 84

Expert Comment

ID: 1254164
so is C(n) supposed to be the worst case height of a balanced tree of n nodes?
LVL 16

Accepted Solution

heyhey_ earned 100 total points
ID: 1254165
maybe i am missing something, but if
the explicit formula should be:
than the recurrence relation should be
     C(n+1)=C(n) + log(2)[n+1]

Expert Comment

ID: 1254166
I have one comment
if C(n)=log(2)[n+1]-1 then let's n=4


but height of tree must be integer!

Expert Comment

ID: 1254167
may be (i don't sure)
c(n+1) = C(n) + 1

Author Comment

ID: 1254168
I had math student help me.  With each call to the algorithm the list of N elements is halved.  Since the algorithm recursively calls itself twice the formula is:
C(N) = 2*C(N/2)
To Prove relation:
Let N = 2^n

C(2^n) = 2*C(2^n-1)
C(2^n-1) = 2*C(2^n-2)
C(2^n) = 2^k * C(2^n-k)
Let k=n
C(2^n) = 2^n * C(2^0) = 2^n

since 2^n = N

C(N) = N

BigOH = O(N)

sorry took so long to respond.  e-mail and internet down.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C dll call freezes 5 107
SQL handling single and double quotes 3 97
Need example 5 122
Grammars for C C++ and java 1 131
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question