How can I get the exact value from the IPL function?

Hyeonbinshin
#include <stdio.h>
#include <stdlib.h>

typedef struct treeNode {
    int key;
    struct treeNode* left;
    struct treeNode* right;
} treeNode;


int count = 0;

treeNode* insertNode(treeNode** t1, int x) {
    if (*t1 == NULL) {
        *t1 = malloc(sizeof(treeNode));
        (*t1)->key = x;
        (*t1)->left = NULL;
        (*t1)->right = NULL;
    }
    else if (x < (*t1)->key) {  //left
        (*t1)->left = insertNode(&(*t1)->left, x);
    }
    else if (x > (*t1)->key) { //right
        (*t1)->right = insertNode(&(*t1)->right, x);
    }
    else {
        printf("SameValue exist.\n");
    }
    return *t1;
}

int getIPL(treeNode* n1) {
    int rightcount;
    int leftcount;
    int rootcount = 1;

    if (n1 != NULL) {
        if (n1->left == NULL && n1->right == NULL) {
            count = rootcount;
        }
        else if (n1->left != NULL && n1->right == NULL) {
            leftcount = 1 + getIPL(n1->left);
            count += leftcount;
        }
        else if (n1->left == NULL && n1->right != NULL) {
            rightcount = 1 + getIPL(n1->right);
            count += rightcount;
        }
        else {
            leftcount = 1 + getIPL(n1->left);
            rightcount = 1 + getIPL(n1->right);
            count += leftcount + rightcount;
        }
    }
    return count;
}


int main() {
    treeNode* T1 = NULL;

    insertNode(&T1, 10);
    insertNode(&T1, 20);
    insertNode(&T1, 3);
    insertNode(&T1, 25);

    printf("IPL : %d", getIPL(T1));
}

This code is to get an internal path length (IPL) from BST. It works normally up to level 2, but it doesn't work above that.

When put on tree 10 5 15, IPL value of 5 comes out.

After, put on 30, you will get 9, not the normal value 8.

I want the IPL value of 8.

Please tell me where the problem is.

Ian Abbott

The depth of each node needs to be taken into account. For example:

static int getIPL_helper(treeNode *n1, int depth) {
    if (n1) {
        return getIPL_helper(n1->left, depth + 1) +
               getIPL_helper(n1->right, depth + 1) + depth;
    } else {
        return 0;
    }
}

int getIPL(treeNode *n1) {
    return getIPL_helper(n1, 1);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the exact word in a sentence

How I can call javascript function and get the return value from javascript function

How can I get a value from an URL?

How can I get a value from LiveData?

How can I get value from GroupedObservable?

How to get exact count of value from json

How can I get this exact formatting in R?

How can I get exact row and column number i.e cell address by value in Pandas dataframe

How can I convert or get the exact value from ctypes.c_byte

How can I get exact user plus the user's exact posts in laravel from jwt token

How can I get the value of a widget the exact moment a button is clicked FLTK

How I can get second value from function using *pointer?

How can I get this value from a JFileChooser?

how to get exact value in setTimeout function

How can i get value from textbox?

How can i get the value from json

How to get exact value from NSNumberFormatter?

How can I find exact Instr value

How can I get the correct sum() of time fields with exact value in HH:MM:SS format

How can I get the value from html page which is assigned with inner function

how can I get the value from json?

How can I get the exact time from my laptop?

how I can get the value from datalist

How can I find exact value for this function?

How to get exact value from input in jquery

How can I get value from option?

How can I get my returning value from a function in GlobalContext?

How can I get a value from an array?

How to get a value from an Array of object without calling the key (i've searched through the internet can't found the exact/similar problem to this)