简体   繁体   English

显示属于树的深度路径的二叉搜索树的节点

[英]Displaying the nodes of binary search tree which belongs depth path of the tree

I have a method in Tree class which calculates the depth of a binary search tree. 我在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 我需要产生节点:8,9,6,10

I have this Node class: 我有这个Node类:

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: 这是Tree类的相关部分:

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. 另外,关于实现的任何其他评论也将很棒。

You have to make sure that when it calls a push_back , it's one of the nodes in the path. 您必须确保当它调用push_back ,它是路径中的节点之一。 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. 我添加了一个变量isInPath ,如果当前节点在路径中,则为1;如果isInPath ,则为0。 You will call this with the root and 1. 您将使用root和1。

It finds the maximum of the two and only then calls with isInPath == 1 . 它查找两者中的最大值,然后才使用isInPath == 1进行调用。 And with this method, only nodes with isInPath == 1 will be added to the vector. 使用此方法,仅将isInPath == 1节点添加到向量中。

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. 注意:这具有O(N^2)复杂度。 To make it in O(N) you can memorize the values so you don't compute them 2 times. 要使其成为O(N)您可以记住这些值,这样就不必计算两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM