Created
July 16, 2019 09:48
-
-
Save ardacetinkaya/0dcabfe3c04cf874d8307831ee0aa6f8 to your computer and use it in GitHub Desktop.
Simple Binary Search Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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