binary-search-tree
C# & C++
using System;
public class Node
{
public int Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node(int value, Node left, Node right)
{
Value = value;
Left = left;
Right = right;
}
}
public class BinarySearchTree
{
public static bool Contains(Node root, int value)
{
if (root == null) {
return false;
}
if (root.Value == value) {
return true;
}
if (value < root.Value) {
return Contains(root.Left, value);
} else {
return Contains(root.Right, value);
}
}
public static void Main(string[] args)
{
Node n1 = new Node(1, null, null);
Node n3 = new Node(3, null, null);
Node n2 = new Node(2, n1, n3);
Console.WriteLine(Contains(n2, 3));
}
}
C++ version
#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)
{
if (&root == NULL)
{
return false;
}
if (root.getValue() == value)
{
return true;
}
if (value < root.getValue())
{
return contains(*root.getLeft(), value);
}
if (value > root.getValue())
{
return contains(*root.getRight(), value);
}
}
};
#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