473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

TreeHeight...how the two variables h1 and h2 work?????

momotaro
357 100+
Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. int h1, h2;
  3. if (root == NULL) return 0;
  4. else{
  5.        h1 = TreeHeight(Tree -> left);
  6.        h2 = TreeHeight(Tree -> right);
  7.        if (h1 > h2) return h1+1;
  8.        else return h2+1;
  9.       }
  10. }
************************************************** *******************************************
this is my version what is wrong ????
************************************************** *******************************************

Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. if (root == NULL) return 0;
  3. else{
  4.        TreeHeight(root -> left); return 1;
  5.        TreeHeight(root -> right); return 1;
  6.        }
  7. }
Apr 25 '07 #1
2 1324
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. int h1, h2;
  3. if (root == NULL) return 0;
  4. else{
  5.        h1 = TreeHeight(Tree -> left);
  6.        h2 = TreeHeight(Tree -> right);
  7.        if (h1 > h2) return h1+1;
  8.        else return h2+1;
  9.       }
  10. }
************************************************** *******************************************
this is my version what is wrong ????
************************************************** *******************************************

Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. if (root == NULL) return 0;
  3. else{
  4.        TreeHeight(root -> left); return 1;
  5.        TreeHeight(root -> right); return 1;
  6.        }
  7. }
Your version is extremely wrong ;-) For one, it neglects the return values
of your TreeHeight() functions and above all: you simple return 1 twice in
the else part. After the first return the rest of the code will not be executed.

kind regards,

Jos
Apr 25 '07 #2
Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. int h1, h2;
  3. if (root == NULL) return 0;
  4. else{
  5.        h1 = TreeHeight(Tree -> left);
  6.        h2 = TreeHeight(Tree -> right);
  7.        if (h1 > h2) return h1+1;
  8.        else return h2+1;
  9.       }
  10. }
************************************************** *******************************************
this is my version what is wrong ????
************************************************** *******************************************

Expand|Select|Wrap|Line Numbers
  1. int TreeHeight(Tree *root){
  2. if (root == NULL) return 0;
  3. else{
  4.        TreeHeight(root -> left); return 1;
  5.        TreeHeight(root -> right); return 1;
  6.        }
  7. }

frnd
your code in any case is always is going report height of tree one.
the correct code recursively first reaches the checks the height of left part of root node and then compute the right part of root, compares both and reports the maximum
Apr 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
10
by: EOZyo | last post by:
Hi, i'm trying to set pagination for a search i run on my website, i'll try to explain the easiest i can: When i click the search button on search.php, data is received and stored in variables...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
26
by: BillE | last post by:
Some ASP.NET applications use Session Variables extensively to maintain state. These should be re-written to use viewstate, hidden fields, querystring, etc. instead. This is because if a user...
12
by: MrHelpMe | last post by:
Hello again all, I've finished my whole application and now I don't like the whole session variables that I am using. I have a form, user fills in info clicks submit and using CDOSYSMail an...
19
by: LucasLondon | last post by:
Hi there, First of all apologies for the long post. Hope someone can offer some advice. I have about 200 columns of time series data that I need to perform a correlation analysis on in terms...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.