Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Could you give an example of a function (recursive or not) that finds if a binary search tree is too tall?

How can I find using C if a binary search tree is too tall relatively fast (most of the time)?

To be more specific , lets say I need to find if a tree has a height of at least 10 ,without having to search the whole tree most of the time.

(This is possible because I expect most of the input to be binary search trees which have a heigh greater than 10.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If there are no preconditions about the structure of the tree, there’s no other way but checking one side of the tree, then the other.

int check_depth(struct tree *root, int depth)
{
    if (!root) {
        return 0;
    } else if (depth <= 1) {
        return 1;
    } else {
        return check_depth(root->left,  depth-1) ||
               check_depth(root->right, depth-1);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading