返回状态后,功能不会结束

时间:2017-08-21 11:10:12

标签: c++

这是节点定义:

DECLARE
  column_names CLOB;
  sql          CLOB;
BEGIN
  SELECT LISTAGG(
           column_name,
           ','
         ) WITHIN GROUP ( ORDER BY column_id )
  INTO   column_names;
  FROM   user_tab_columns
  WHERE  table_name  = 'YOUR_TABLE'
  AND    column_name <> 'STUDENT';

  sql := 'SELECT * FROM ( SELECT t.*, ROW_NUMBER() OVER ( PARTITION BY student ORDER BY total DESC ) AS rn FROM your_table UNPIVOT ( total FOR subject IN ( ' || column_names || ' ) ) t ) WHERE  rn = 1;'

  EXECUTE IMMEDIATE sql;
END;
/

这里的功能包含:

Node::Node(int value, Node* left, Node* right) {
   this->value = value;
   this->left = left;
   this->right = right;
}

int Node::getValue() const {
   return value;
}

Node* Node::getLeft() const {
   return left;
}

Node* Node::getRight() const {
   return right;
}

现在我使用这个功能:

static bool contains(const Node& root, int value)
{
   cout << "Root value: " << root.getValue() << endl;
   if(root.getValue() == value)
   {
      cout << "You entered." << endl;
      return true;
   }
   else if(root.getLeft() != NULL)
   {
      cout << "Left tree: " << endl;
      contains(*(root.getLeft()), value);
   }
   else if(root.getRight() != NULL)
   {
      cout << "Right tree: " << endl;
      contains(*(root.getRight()), value);
   }

   cout << "End" << endl;
   return false;
}

这个1值在左子树中。它显示&#34;您输入了。&#34;并且无论如何都会结束显示0的功能。为什么它不显示1(真)?我错过了什么?

1 个答案:

答案 0 :(得分:2)

您缺少一些退货声明。

cout << "Root value: " << root.getValue() << endl;

if(root.getValue() == value)
{
    cout << "You entered." << endl;
    return true;
}
else if(root.getLeft() != NULL)
{
    cout << "Left tree: " << endl;
    contains(*(root.getLeft()), value); // no return here 
}
else if(root.getRight() != NULL)
{
    cout << "Right tree: " << endl;
    contains(*(root.getRight()), value); // and no return here
}

cout << "End" << endl;
return false;

它检查树是否在某处包含值,但是nether将结果传递给外部世界。将其更改为

if (root.getValue() == value)
{
    cout << "You entered." << endl;
    return true;
}
auto left = root.getLeft();
if (left)
{
   cout << "Checking Left tree: " << endl;
   if (contains(*left, value))
        return true;
}
auto right = root.getRight();
if (right)
{
   cout << "Checking Right tree: " << endl;
   if (contains(*right, value))
        return true;
}

cout << "End" << endl;
return false;

它应该有用。