HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Height of a general tree in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
generaltreeheight

Problem

I need to calculate the height of an optional (not AVL, binary) tree. For input I receive an n integer positive number: parent(0), parent(1), ... parent(n-1). Here, parent(i) is a parent of node i. If parent(i) == -1, that i is the root of the tree. It is guaranteed that the sequence has only one root and presets a tree.

Limitation:

\$1 \le n \le 1e5\$

Example input:

5
4 -1 4 1 1


Output:

3

 1
/ \
3  4
   /\
   0 2


My program on C++ works but it looks like that the algorithm is not optimal. The program is verified automatically and one of the test failed with error "Time limit exceeded". Could you advise me, please, on how I can optimize the program?

#include "iostream"

int tree[10000];
int n = 0;

int Height(int parent);

int main ()
{
    std::cin >> n;

    for (int i = 0; i > tree[i];
    }

    std::cout << Height(-1) - 1;
    return 0;
}

int Height(int parent)
{
    int height = 1;
    for (int i = 0; i < n; ++i)
    {
        if (tree[i] == parent)
        {
            height = std::max(height, 1 + Height(i));
        }
    }
    return height;
}

Solution

The explanation above is almost excellent. (user1118321) Some cases, this program returns a wrong answer. for example:

if the input will be:

10

9 7 5 5 2 9 9 9 2 -1

9
           / /  \ \
          / /    \ \
         0  5     6 7
           / \       \
          2   3       1
         / \
        4   8


expected: 4

That solution returns: 5

It can be resolved, replacing this variable "currentHeight" for a height stack which each node has it's respective height.

You can use the second stack with a similar mode the first stack.

I can't share the whole answer, even using this code you will need to implement something else. But I hope this helps. :D

int findTreeHeight(Node* rootNode) {

    int maxHeight   = 0;
    std::stack   nodeStack;
    nodeStack.push(rootNode);

    // int currentHeight   = 0; // create the stack here, and add 1; 
    while (nodeStack.size() > 0)
    {
        /* currentHeight++;
        if (currentHeight > maxHeight)
        {
            maxHeight = currentHeight;
        } */

        Node*   top = nodeStack.top();
        nodeStack.pop();

        int currentHeight = stackHeight.top() + 1; // it's a new level
        // after that pop the top of the second stack.  

        if (top->children.size() == 0) {
            // just compare (currentHeight > maxHeight) here because it's a leaf node.       
        }

        for (auto it = top->children.begin(); it children.end(); it++) {
            nodeStack.push(*it);
        }
    }

    return maxHeight;
}

Code Snippets

9
           / /  \ \
          / /    \ \
         0  5     6 7
           / \       \
          2   3       1
         / \
        4   8
int findTreeHeight(Node* rootNode) {

    int maxHeight   = 0;
    std::stack<Node*>   nodeStack;
    nodeStack.push(rootNode);

    // int currentHeight   = 0; // create the stack here, and add 1; 
    while (nodeStack.size() > 0)
    {
        /* currentHeight++;
        if (currentHeight > maxHeight)
        {
            maxHeight = currentHeight;
        } */

        Node*   top = nodeStack.top();
        nodeStack.pop();

        int currentHeight = stackHeight.top() + 1; // it's a new level
        // after that pop the top of the second stack.  



        if (top->children.size() == 0) {
            // just compare (currentHeight > maxHeight) here because it's a leaf node.       
        }

        for (auto it = top->children.begin(); it < top->children.end(); it++) {
            nodeStack.push(*it);
        }
    }

    return maxHeight;
}

Context

StackExchange Code Review Q#162079, answer score: 3

Revisions (0)

No revisions yet.