public static void Main(string[] args)
Console.WriteLine("Hello World");
public static void Test1()
tree.root = new Node(10);
tree.root.left = new Node(5);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(1);
public static void Test2()
tree.root = new Node(50);
tree.root.left = new Node(17);
tree.root.right = new Node(72);
tree.root.left.left = new Node(12);
tree.root.left.right = new Node(23);
tree.root.right.left = new Node(54);
tree.root.right.right = new Node(76);
public static void Test3()
public static void Test4()
Console.WriteLine($"{tree.isContained(17)}");
Console.WriteLine($"{tree.isContained(50)}");
public void printInorder(){ printInorder(root); }
public void printInorder(Node n)
Console.Write(n.data + " ");
public void Add(Object data){ root = Add(root, data); }
public Node Add(Node root, Object data)
if ((int)data < (int)root.data){
root.left = Add(root.left, data);
} else if ((int)data > (int)root.data){
root.right = Add(root.right, data);
Console.WriteLine($"invalid data {(int)data}");
public void Remove(Object data){ root = Remove(root, data); }
public Node Remove_(Node root, Object data)
if (root == null) return root;
if ((int)data < (int)root.data){
root.left = Remove(root.left, data);
} else if ((int)data > (int)root.data){
root.right = Remove(root.right, data);
root.data = root.right.left;
public Node Remove(Node root, Object data)
if (root == null) return root;
if ((int)data < (int)root.data){
root.left = Remove(root.left, data);
} else if ((int)data > (int)root.data){
root.right = Remove(root.right, data);
else if (root.right == null)
root.data = minValue(root.right);
root.right = Remove(root.right, root.data);
private int minValue(Node n){
while (n.left != null){ min = (int)n.left.data; n = n.left; }
public bool isContained(int target)
Node r = search(this.root, target);
if (r == null) return false;
private Node search(Node n, int target)
if ((int)n.data < target)
return search(n.right, target);
else if ((int)n.data > target)
return search(n.left, target);
public Node(Object data){
this.left = this.right = null;