Pergunta

I have a method in Tree class which calculates the depth of a binary search tree.

My additional task is to, while calculating the depth of the tree, also store (or keep in some way) the nodes which are on this path (from the root to the most distanced leaf).

For instance, consider the following tree:

      10  
     /  \
    6    13 
   / \     \    
  4   9     14  
 /   /
3   8

I need to produce the nodes: 8,9,6,10

I have this Node class:

class Node {

private:
    Node *left;
    Node *right;
    int value;
public:
   Node(int v = 0) : left(NULL) , right(NULL) , value(v) { cout << "Node construcotr      " << endl;}

    Node *getLeft() {return this->left;}
    void setLeft(Node *n) {this->left = n;}
    Node *getRight() {return this->right;}
    void setRight(Node *n) {this->right = n;}
    int getVal() {return this->value;}

};

And here is the relevant part of the Tree class:

class Tree {

private:
    Node* root;
    vector<int> vec; /*.....*/

int calcDepth(Node *n)
{ 
 if (n == NULL)
     return 0;
 else 
 {
     int ld = calcDepth(n->getLeft());
     int rd = calcDepth(n->getRight());

     if (ld > rd)
     {
         if (n->getLeft() != NULL) vec.push_back(n->getLeft()->getVal());
         return ld + 1;
     }
     else 
     {
         if (n->getRight() != NULL) vec.push_back(n->getRight()->getVal());
         return rd + 1;
     }
 }
} // end of calcDepth

At the moment it gives me partial solution (excluding the root and taking into account node which are not part of that path).

Can someone help me fix this code?

In addition, any other comments about the implementation will also be great.

Foi útil?

Solução

You have to make sure that when it calls a push_back, it's one of the nodes in the path. Since your current algorithm adds nodes for every node.

class Tree {

private:
    Node* root;
    vector<int> vec; /*.....*/

int calcDepth(Node *n,int isInPath)
{ 
 if (n == NULL)
     return 0;
 else 
 {
     int ld = calcDepth(n->getLeft(),0);
     int rd = calcDepth(n->getRight(),0);

         if(isInPath){
         vec.push_back(n->getVal());
         if (ld > rd)
         {
             calcDepth(n->getLeft(),isInPath);
             return ld + 1;
         }
         else 
         {   
             calcDepth(n->getRight(),isInPath);
             return rd + 1;
         }
        }
    return max(ld+1,rd+1);
 }

} // end of calcDepth

I've added a variable isInPath, that is 1 if the current node is in the path, and 0 if it's not. You will call this with the root and 1.

It finds the maximum of the two and only then calls with isInPath == 1. And with this method, only nodes with isInPath == 1 will be added to the vector.

I haven't tested it out but it should work. You can also create this as a private function and then a public one with only the node value.

Note: This has O(N^2) complexity. To make it in O(N) you can memorize the values so you don't compute them 2 times.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top