に置か復帰後に終了していないノードの定義されています機能は、ここで条件
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;
}
今、私はこの機能を使用します。
Node n1(1, NULL, NULL);
Node n3(3, NULL, NULL);
Node n2(2, &n1, &n3);
cout << "Contains? " << contains(n2, 1) << endl;
この1の値は左のサブツリーにあります。 「入力しました」と表示されます。結果として0を表示する関数の最後に行く。なぜ1(真)を表示しないのですか?私は何を取りこぼしたか?
重要な2つの「リターン」キーワードがありません。 '*(root.getLeft())、value);' –