1
votes

I am writing the the answer for a test sample given in testdome https://www.testdome.com/for-developers/solve-question/9708

The question is about binary search tree:

Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.

Write a function that checks if a given binary search tree contains a given value.

For example, for the following tree: n1 (Value: 1, Left: null, Right: null) n2 (Value: 2, Left: n1, Right: n3) n3 (Value: 3, Left: null, Right: null) Call to contains(n2, 3) should return true since a tree with root at n2 contains number 3.

enter image description here

I modified the code as below, the output looks working well, but the test result tells one fail exist as: Performance test on a large tree: Time limit exceeded Can you help to modified my mode to fix this fail?

#include <stdexcept>
#include <string>
#include <iostream>

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

int getValue() const
{
    return value;
}

Node* getLeft() const
{
    return left;
}

Node* getRight() const
{
    return right;
}

private:
int value;
Node* left;
Node* right;
};

class BinarySearchTree
{
public:
static bool contains(const Node& root, int value)
{
    Node* tree;
    int val = root.getValue();
    std::cout<<"current node's value is:"<<val<<'\n';

        if (val==value)
        {
            return true;
        }
        else if (val>value)
        {
            tree = root.getLeft();                
            if(tree != NULL)
            {
                std::cout<<"left node's value is:"<<tree->getValue()<<'\n';
                return contains(*tree, value);
            }
            else 
            {
                return false;
            }
        }
        else
        {
            tree = root.getRight();
            if(tree != NULL)
            {
                std::cout<<"right node's value is:"<<tree->getValue()<<'\n';
                return contains(*tree, value);
            }
            else 
            {
                return false;
            }
        }      
    //throw std::logic_error("Waiting to be implemented");
   }   
};

#ifndef RunTests
int main()
{
Node n1(1, NULL, NULL);
Node n3(3, NULL, NULL);
Node n2(2, &n1, &n3);
std::cout << BinarySearchTree::contains(n2, 3);
}
#endif
2
See my question, and answer (for C#) here: stackoverflow.com/questions/45625016/…Adam Cox

2 Answers

1
votes

Remove std::cout will do. Printing to terminal has a huge time cost.

1
votes

Oh, A better solution here. Why do you want to use temporary variables? While using recursion remember that the temporary variables do get stored on function's calling stack also don't use print statements.

static bool contains(const Node& root, int value)
{
    if(root.getValue() == value){
         return true;
     }
    else if(root.getValue() < value && root.getRight() != NULL){
        return  contains(*(root.getRight()), value);
    }
    else if(root.getLeft() != NULL){
        return contains(*(root.getLeft()), value);
    }
return false;
}