Skip to content

Instantly share code, notes, and snippets.

@ardacetinkaya
Created July 16, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardacetinkaya/0dcabfe3c04cf874d8307831ee0aa6f8 to your computer and use it in GitHub Desktop.
Save ardacetinkaya/0dcabfe3c04cf874d8307831ee0aa6f8 to your computer and use it in GitHub Desktop.
Simple Binary Search Tree
namespace BinarySearchTree
{
public class Node
{
public int Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
}
public class Tree
{
public Node Insert(Node n, int value)
{
if (n == null)
{
n = new Node
{
Value = value
};
}
else if (value < n.Value)
{
n.Left = Insert(n.Left, value);
}
else
{
n.Right = Insert(n.Right, value);
}
return n;
}
public void Display(Node n)
{
if (n == null) return;
Display(n.Left);
Console.Write($"{n.Value} - ");
Display(n.Right);
}
public Node FindNode(Node node, int value)
{
if (node == null) return null;
if (value < node.Value)
{
return FindNode(node.Left, value);
}
else if (value > node.Value)
{
return FindNode(node.Right, value);
}
else
{
return node;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment