public static void Main()
BinarySearchTree bsT = new BinarySearchTree();
Console.WriteLine("Min");
Console.WriteLine(bsT.FindMin(bsT.root));
Console.WriteLine("Tree : ");
Console.WriteLine("Deleting Node 6: ");
Console.WriteLine("Deleting Node 15: ");
bsT.Delete(bsT.root, 15);
Console.WriteLine("Deleting Node 1: ");
Console.WriteLine("PreOrder");
BinarySearchTree bsT1 = new BinarySearchTree();
bsT1.PreOrderTraversal(bsT1.root);
Console.WriteLine("Inorder");
bsT1.InOrderTraversal(bsT1.root);
Console.WriteLine("PostOrder");
bsT.PostOrderTraversal(bsT.root);
Console.WriteLine("FindKthMax: " + bsT1.FindKthMax(bsT1.root, 3));
Console.WriteLine("Find ancestors");
Console.WriteLine(bsT1.FindAncestors(bsT1.root, 12));
Console.WriteLine("Find Height");
Console.WriteLine(bsT1.FindHeight(bsT1.root));
Console.WriteLine("FindKNodes");
Console.WriteLine(bsT1.FindKNodes(bsT1.root, 2));
public int Data { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public class BinarySearchTree
public Node root { get; set; }
public bool Add(int value)
this.root = Insert(root, value);
public void PrintTree(Node current)
if(current == null) return;
Console.Write(current.Data + ", ");
PrintTree(current.Right);
public int FindMin(Node root)
return FindMin(root.Left);
public Node Insert(Node currentNode, int value)
if(value < currentNode.Data)
currentNode.Left = Insert(currentNode.Left, value);
else if (value > currentNode.Data)
currentNode.Right = Insert(currentNode.Right, value);
public Node Search(int value)
if(isEmpty()) return null;
while(currentNode != null)
if(currentNode.Data == value)
if(currentNode.Data < value)
currentNode = currentNode.Right;
currentNode = currentNode.Left;
public bool Delete(Node currentNode, int value)
while(currentNode != null && currentNode.Data != value)
if(value < currentNode.Data)
currentNode = currentNode.Left;
currentNode = currentNode.Right;
else if (currentNode.Left == null && currentNode.Right == null)
if(currentNode.Data == root.Data)
else if (currentNode.Data < parent.Data)
else if (currentNode.Data > parent.Data)
else if (currentNode.Right == null)
if(currentNode.Data == root.Data)
else if(parent.Data < currentNode.Data)
parent.Right = currentNode.Left;
else if(parent.Data > currentNode.Data)
parent.Left = currentNode.Left;
else if (currentNode.Left == null)
if(currentNode.Data == root.Data)
root = currentNode.Right;
else if (parent.Data < currentNode.Data)
parent.Right = currentNode.Right;
else if (parent.Data > currentNode.Data)
parent.Left = currentNode.Right;
Node leastNodeInRightSubTree = FindLeast(currentNode.Right);
int temp = leastNodeInRightSubTree.Data;
public void PreOrderTraversal(Node root)
Console.Write(root.Data + ", ");
PreOrderTraversal(root.Left);
PreOrderTraversal(root.Right);
public void InOrderTraversal(Node root)
InOrderTraversal(root.Left);
Console.Write(root.Data + ", ");
InOrderTraversal(root.Right);
public void PostOrderTraversal(Node root)
PostOrderTraversal(root.Left);
PostOrderTraversal(root.Right);
Console.Write(root.Data + ", ");
public StringBuilder InOrder(Node root, StringBuilder sb)
sb.Append(root.Data).Append(",");
public int FindKthMax(Node root, int k) {
StringBuilder sb = InOrder(root, new StringBuilder());
string[] arr = sb.ToString().Split(',');
return Int32.Parse(arr[arr.Length-k-1]);
public String FindAncestors(Node root, int k)
StringBuilder sb = new StringBuilder();
while(temp != null && temp.Data != k)
sb.Append(temp.Data).Append(",");
public int FindHeight(Node root)
if(root == null) return -1;
return 1 + Math.Max(FindHeight(root.Left), FindHeight(root.Right));
public String FindKNodes(Node root, int k) {
return FindNodesR(root, k, new StringBuilder());
public int CountNodesInRange(Node n, int low, int high)
return CountNodesInRange(n.Right, low, high);
return CountNodesInRange(n.Left, low, high);
return 1 + CountNodesInRange(n.Left, low, high) + CountNodesInRange(n.Right, low, high);
private String FindNodesR(Node root, int k, StringBuilder sb)
if(root == null) return null;
sb.Append(root.Data).Append(",");
FindNodesR(root.Left, k-1, sb);
FindNodesR(root.Right, k-1, sb);
private Node FindLeast(Node currentNode)