void Add(V value, MyBinaryNode <V> node) { if (head == null) { head = new MyBinaryNode <V>(value); } else if (node.value.CompareTo(value) > 0) { if (node.left == null) { node.left = new MyBinaryNode <V>(value); } else { Add(value, node.left); } } else if (node.value.CompareTo(value) < 0) { if (node.right == null) { node.right = new MyBinaryNode <V>(value); } else { Add(value, node.right); } } }
void Traverse(MyBinaryNode <V> node) { if (node.left != null) { Traverse(node.left); } Console.WriteLine(node.value); if (node.right != null) { Traverse(node.right); } }
void Find(V value, MyBinaryNode <V> node) { if (value.Equals(node.value)) { Console.WriteLine("Element found"); return; } else if (value.CompareTo(node.value) < 0 && node.left != null) { Find(value, node.left); } else if (value.CompareTo(node.value) > 0 && node.right != null) { Find(value, node.right); } else { Console.WriteLine("Element Not Found"); } }
public BST() { head = null; }