Thread: Height of the Tree

  1. #1
    Registered User
    Join Date
    Jun 2013
    Location
    Curitiba, Brazil
    Posts
    3

    Question Height of the Tree

    I can't make my code just to show the tree size.
    Something like: left -1, right 1, root 0. I've tried to use a few codes that i found here but none has worked... :/

    Code:
    #include <conio.h>#include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct Node
    {
        struct Node *left;
        struct Node *right;
        int value;
    } Node;
    
    
    void insertNode(Node **tree,int element)
    {
        if(*tree == NULL)
        {
            Node *aux = (Node *)malloc(sizeof(Node));
            aux->value = element;
            aux->right= NULL;
            aux->left = NULL;
            *tree = aux;
            printf("The value %d has been inserted.\n",element);
            return;
        }
    
    
        if(element < (*tree)->value)
        {
            insertNode(&(*tree)->left,element);
            return;
        }
    
    
        if(element > (*tree)->value)
        {
            insertNode(&(*tree)->right,element);
            return;
        }
        printf("The value %d is already in the tree.\n",element);
    }
    
    
    Node *Removtwo(Node *two)
    {
        if(two==NULL)
            return NULL;
        else if(two->left == NULL)
            return Removtwo(two->right);
        else
            return Removtwo(two->left);
    }
    
    
    void removerNode(Node **tree,int element)
    {
        if(element < (*tree)->value)
        {
            removerNode(&(*tree)->left,element);
        }
        else if(element > (*tree)->value)
        {
            removerNode(&(*tree)->right,element);
        }
        else if((*tree)->left!=NULL && (*tree)->right!=NULL)
        {
            Node *aux= NULL;
            aux = Removtwo((*tree)->right);
            (*tree)->value = aux->value;
            removerNode(&(*tree)->right,(*tree)->value);
        }
        else
        {
            Node *aux = (*tree);
            if((*tree)->left==NULL)
            {
                (*tree) = (*tree)->right;
            }
            else
            {
                *tree = (*tree)->left;
            }
            printf("The value %d was sucessfull removed.\n",element);
            free(aux);
        }
    }
    
    
    void Print(Node *tree)
    {
        if(tree == NULL)
        {
            return;
        }else
        Print(tree->left);
        printf("%d->",tree->value);
        Print(tree->right);
    }
    
    
    int main()
    {
        int num,insertion,removing;
        Node *tree = NULL;
    
    
        while(num != 4)
        {
            printf("-------------------------------------\n");
            printf("------ 1. Insert         ------------\n");
            printf("------ 2. Remove         ------------\n");
            printf("------ 3. Print Tree     -----------\n");
            printf("------ 4. Exit           ------------\n");
            printf("-------------------------------------\n");
            scanf("%d", &num);
            system("cls");
    
    
            switch(num)
            {
            case 1:
            {
                printf("Put the value: ");
                scanf("%d",&insertion);
                insertNode(&tree,insertion);
                getch();
                system("cls");
                break;
            }
            case 2:
            {
                printf("Put the value: ");
                scanf("%d",&removing);
                removerNode(&tree,removing);
                getch();
                system("cls");
                break;
            }
            case 3:
            {
                Print(tree);
                getch();
                system("cls");
                break;
            }
            case 4:
                 {
                    system("cls");
                   break;
                 }
            }
        }
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not get what you mean by tree size.
    If left size if 1 and right size is 1, Why root size is 0?

    I thought it would be left_size + right_size +1 if you want total number of nodes, or max(left_size, right_size) + 1, if you want the tree depth.

    So what do you want to get when you are talking about tree size?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Heads up - this is C and not C++. Please post in the C section next time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Location
    Curitiba, Brazil
    Posts
    3
    Quote Originally Posted by vart View Post
    I do not get what you mean by tree size.
    If left size if 1 and right size is 1, Why root size is 0?

    I thought it would be left_size + right_size +1 if you want total number of nodes, or max(left_size, right_size) + 1, if you want the tree depth.

    So what do you want to get when you are talking about tree size?
    i expressed myself a little bit wrong, but i want to do just what u said.. i tried to implement this but did not work:

    height(Node) = max ( height(node.Left) + height (node.Right)) +1
    printf (" Left: %d Right: %d ", tree->left, tree->right);


    Sorry about the section... i'll post in the right one next time

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by LStriker View Post
    I can't make my code just to show the tree size.
    Where is the bit of code that attempts to do that?

    Do you mean the number of nodes, or the maximum depth?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2013
    Location
    Curitiba, Brazil
    Posts
    3
    Quote Originally Posted by iMalc View Post
    Where is the bit of code that attempts to do that?

    Do you mean the number of nodes, or the maximum depth?
    Maximum depth;
    i tried too many codes but none has work so i came to the conclusion that i don't know how to do this. I was trying something like this
    height(Node) = max ( height(node.Left) + height (node.Right)) +1
    printf (" Left: %d Right: %d ", tree->left, tree->right);

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by LStriker View Post
    Maximum depth;
    i tried too many codes but none has work so i came to the conclusion that i don't know how to do this. I was trying something like this
    height(Node) = max ( height(node.Left) + height (node.Right)) +1
    printf (" Left: %d Right: %d ", tree->left, tree->right);
    You're on the right track with the recursive function, but there's a small error. Look carefully at your usage of max. You are finding the max of one thing, the sum of the left and right sub-trees. Summing those two things produces a single value and you're asking for the max of just one thing, which doesn't really make sense. You need to find the max of either the left or right sub-tree

    vart showed you the correct way in post #2 (notice the , not a +):
    Quote Originally Posted by vart View Post
    ...or max(left_size, right_size) + 1, if you want the tree depth.
    So take that, and come up with a function. If you get stuck, post the code you have and we will help you fix it.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well judging by all the things that you have in the code posted, you are capable of putting that formula to use already.

    Just try adding it into your code and post the function that you put it in, along with any compile errors.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the height of a binary search tree?
    By aw1742 in forum C Programming
    Replies: 2
    Last Post: 02-03-2012, 12:42 PM
  2. Simple binary tree height function.
    By noodle24 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2007, 07:23 PM
  3. Binary search tree height
    By BoneXXX in forum C Programming
    Replies: 2
    Last Post: 04-09-2007, 09:49 PM
  4. Minimum height of a B-Tree
    By FlatLost in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 08:21 AM
  5. Height
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-20-2003, 04:27 PM