जब भी हम एक छोटे से द्विआधारी पेड़ से संबंधित समस्या करने की कोशिश यह उम्र लेता है एक बड़ा पर्याप्त द्विआधारी पेड़ को भरने के लिए बुनियादी कोड लिखने के लिए। मैं जल्दी से यादृच्छिक मूल्यों के साथ प्रारंभ एक द्विआधारी खोज वृक्ष के निर्माण के लिए एक छोटा सा कोड करना चाहते हैं।
जल्दी सी # में एक द्विआधारी पेड़ बनाने
वोट
1
1 जवाब
वोट 1
1
static void Main(string[] args)
{
int numberOfNodes = 10;
Random rand = new Random();
int[] randomValues = Enumerable.Repeat(0, numberOfNodes).Select(i => rand.Next(0, 100)).ToArray();
//sort the array
int[] binaryTreeValues = (from x in randomValues orderby x select x).ToArray();
BNode root = null;
Construct(ref root, ref binaryTreeValues, 0, binaryTreeValues.Length - 1);
}
public static void Construct(ref BNode root, ref int[] array, int start, int end)
{
if (start > end)
{
root = null;
}
else if (start == end)
{
root = new BNode(array[start]);
}
else
{
int split = (start + end) / 2;
root = new BNode(array[split]);
Construct(ref root.Left, ref array, start, split - 1);
Construct(ref root.Right, ref array, split + 1, end);
}
}
public class BNode
{
public int ID;
public int Level;
public BNode Left;
public BNode Right;
public BNode(int ID)
{
this.ID = ID;
}
public override string ToString()
{
return this.ID.ToString();
}
}
सादर, Sriwantha श्री अरविंद













