I want to get the index of all the nodes in a bst. The code i am using to insert the values and search a node is
public class BTS
{
public class Node
{
public int value;
public Node Left;
public Node Right;
}
public Node Insert(Node nod,int value)
{
if (nod == null)
{
nod = new Node();
nod.value = value;
}
else if(value < nod.value)
{
nod.Left = Insert(nod.Left, value);
}
else if(value > nod.value)
{
nod.Right = Insert(nod.Right, value);
}
return nod;
}
public string FindNode(Node node, int s)
{
string Output = "";
if (node == null)
return Output = "not found";
else if (s.CompareTo(node.value) < 0)
return FindNode(node.Left, s);
else if (s.CompareTo(node.value) > 0)
return FindNode(node.Right, s);
return Output = "found";
}
static void Main()
{
Node N = null;
BTS bs = new BTS();
N = bs.Insert(N, 10);
N = bs.Insert(N, 9);
N = bs.Insert(N, 8);
N = bs.Insert(N, 11);
N = bs.Insert(N, 12);
bs.FindNode(N,9);
}
}
the above code gives me the value as true if the node 9 is present. But i want to get the index of the node just like how we get index for an array.
Thank you in advance.