Chaque fois que nous essayons de faire un petit problème lié aux arbres binaires, il prend les âges pour écrire le code de base pour remplir un assez grand arbre binaire. Je veux avoir un petit code pour construire rapidement un arbre de recherche binaire initialisé avec des valeurs aléatoires.
Créez rapidement un arbre binaire en C #
voix
1
Créé 14/07/2011 à 00:10 2011-07-14 00:10
source utilisateur Sriwantha Attanayake
Dans d'autres langues...
source utilisateur Sriwantha Attanayake
Dans d'autres langues...
1 réponses
voix 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();
}
}
Cordialement, Sriwantha Sri Aravinda