PDA

View Full Version : مقاله: استفاده از درخت ها



Arian1380
پنج شنبه 29 مرداد 1394, 16:15 عصر
دو کلاس زیر توانایی کار با درخت ها را به خوبی نمایش می دهد که به صورت ساده شده طراحی کرده ام.
کد و توضیحات را بخوانید و خوشحال می شوم نظر شما را هم بدانم.
کلاس مشخصات:

public class Specs:IComparable
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
private set
{
if (!string.IsNullOrEmpty(value))
_firstName = value;
else
throw new ArgumentException("FirstName is not valid");
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
private set
{
if (!string.IsNullOrEmpty(value))
_lastName = value;
else
throw new ArgumentException("LastName is not valid");
}
}

private string _fatherName;
public string FatherName
{
get { return _fatherName; }
private set
{
if (!string.IsNullOrEmpty(value))
_fatherName = value;
else
throw new ArgumentException("FatherName is not valid");
}
}

private DateTime _birthDate;
public int Year
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetYear(_birthDate);
}
}
public int Month
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetMonth(_birthDate);
}
}
public int Day
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetDayOfMonth(_birthDate);
}
}
public string BirthDateToString
{
get
{
return ComplementPersianClanander.ToStringDate(_birthDate );
}
}
public string ToWrittenBirthDate
{
get
{
return ComplementPersianClanander.ToWrittenDate(_birthDat e);
}
}

private String _nationalcode;
public string Nationalcode
{
get { return _nationalcode; }

private set
{
string temp = value;
if (temp.Length == 10)
{
decimal numberTemp = 0;
decimal.TryParse(temp, out numberTemp);
if (numberTemp != 0)
_nationalcode = value;
else
throw new ArgumentException("Nationalcode is not valid");
}
else
throw new ArgumentException("Nationalcode is not valid");
}
}

public Specs(string firstName, string lastName, string fatherName,int Year,
int Month,int Day, string nationalcode)
{

FirstName = firstName;
LastName = lastName;
FatherName = fatherName;
try
{
PersianCalendar pc = new PersianCalendar();
_birthDate = pc.ToDateTime(Year, Month, Day,0,0,0,0);
}
catch
{
throw new ArgumentException("Argument Of Birth Date is Not valid");
}
Nationalcode = nationalcode;

}

public virtual int CompareTo(object obj)
{
Specs temp = obj as Specs;
if (temp != null)
{
if (this.Nationalcode==temp.Nationalcode)
{
return 0;
}
else if (decimal.Parse(this.Nationalcode) > decimal.Parse(temp.Nationalcode))
{
return 1;
}
else
{
return -1;
}

}
else
{
throw new ArgumentException("object is not a SpecsClass");
}
} public class Specs:IComparable
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
private set
{
if (!string.IsNullOrEmpty(value))
_firstName = value;
else
throw new ArgumentException("FirstName is not valid");
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
private set
{
if (!string.IsNullOrEmpty(value))
_lastName = value;
else
throw new ArgumentException("LastName is not valid");
}
}

private string _fatherName;
public string FatherName
{
get { return _fatherName; }
private set
{
if (!string.IsNullOrEmpty(value))
_fatherName = value;
else
throw new ArgumentException("FatherName is not valid");
}
}

private DateTime _birthDate;
public int Year
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetYear(_birthDate);
}
}
public int Month
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetMonth(_birthDate);
}
}
public int Day
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetDayOfMonth(_birthDate);
}
}
public string BirthDateToString
{
get
{
return ComplementPersianClanander.ToStringDate(_birthDate );
}
}
public string ToWrittenBirthDate
{
get
{
return ComplementPersianClanander.ToWrittenDate(_birthDat e);
}
}

private String _nationalcode;
public string Nationalcode
{
get { return _nationalcode; }

private set
{
string temp = value;
if (temp.Length == 10)
{
decimal numberTemp = 0;
decimal.TryParse(temp, out numberTemp);
if (numberTemp != 0)
_nationalcode = value;
else
throw new ArgumentException("Nationalcode is not valid");
}
else
throw new ArgumentException("Nationalcode is not valid");
}
}

public Specs(string firstName, string lastName, string fatherName,int Year,
int Month,int Day, string nationalcode)
{

FirstName = firstName;
LastName = lastName;
FatherName = fatherName;
try
{
PersianCalendar pc = new PersianCalendar();
_birthDate = pc.ToDateTime(Year, Month, Day,0,0,0,0);
}
catch
{
throw new ArgumentException("Argument Of Birth Date is Not valid");
}
Nationalcode = nationalcode;

}

public virtual int CompareTo(object obj)
{
Specs temp = obj as Specs;
if (temp != null)
{
if (this.Nationalcode==temp.Nationalcode)
{
return 0;
}
else if (decimal.Parse(this.Nationalcode) > decimal.Parse(temp.Nationalcode))
{
return 1;
}
else
{
return -1;
}

}
else
{
throw new ArgumentException("object is not a SpecsClass");
}
}

// کلاس درخت جنریک:
public class TreeGeneric<T> where T : IComparable
{
public TreeGeneric<T> Right { get; private set; }
public T Data { get; private set; }
public TreeGeneric<T> Left { get; private set; }

public TreeGeneric(T nodeData)
{
Data = nodeData;
Right = null;
Left = null;
}

public void Insert(T insertValue)
{
if (insertValue.CompareTo(Data) < 0)
{
if (Left == null)
Left = new TreeGeneric<T>(insertValue);
else
Left.Insert(insertValue);
}
else if (insertValue.CompareTo(Data) > 0)
{
if (Right == null)
Right = new TreeGeneric<T>(insertValue);
else
Right.Insert(insertValue);
}
}

public static void InOrderMinToMax(TreeGeneric<T> tree, List<T> listT)
{
if (tree != null)
{
InOrderMinToMax(tree.Left, listT);
listT.Add(tree.Data);
InOrderMinToMax(tree.Right, listT);
}
}

public static void InOrderMaxToMin(TreeGeneric<T> tree, List<T> listT)
{
if (tree != null)
{
InOrderMaxToMin(tree.Right, listT);
listT.Add(tree.Data);
InOrderMaxToMin(tree.Left, listT);
}
}

public static void SearchItem(TreeGeneric<T> tree, T Item,ref T defult)
{
if (tree != null)
{
if (Item.CompareTo(tree.Data) == 0)
{
defult = tree.Data;
return;
}

if (Item.CompareTo(tree.Data) < 0 && tree.Left != null)
SearchItem(tree.Left, Item, ref defult);
else if (Item.CompareTo(tree.Data) > 0 && tree.Right != null)
SearchItem(tree.Right, Item, ref defult);
}
}
public class Specs:IComparable
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
private set
{
if (!string.IsNullOrEmpty(value))
_firstName = value;
else
throw new ArgumentException("FirstName is not valid");
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
private set
{
if (!string.IsNullOrEmpty(value))
_lastName = value;
else
throw new ArgumentException("LastName is not valid");
}
}

private string _fatherName;
public string FatherName
{
get { return _fatherName; }
private set
{
if (!string.IsNullOrEmpty(value))
_fatherName = value;
else
throw new ArgumentException("FatherName is not valid");
}
}

private DateTime _birthDate;
public int Year
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetYear(_birthDate);
}
}
public int Month
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetMonth(_birthDate);
}
}
public int Day
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetDayOfMonth(_birthDate);
}
}
public string BirthDateToString
{
get
{
return ComplementPersianClanander.ToStringDate(_birthDate );
}
}
public string ToWrittenBirthDate
{
get
{
return ComplementPersianClanander.ToWrittenDate(_birthDat e);
}
}

private String _nationalcode;
public string Nationalcode
{
get { return _nationalcode; }

private set
{
string temp = value;
if (temp.Length == 10)
{
decimal numberTemp = 0;
decimal.TryParse(temp, out numberTemp);
if (numberTemp != 0)
_nationalcode = value;
else
throw new ArgumentException("Nationalcode is not valid");
}
else
throw new ArgumentException("Nationalcode is not valid");
}
}

public Specs(string firstName, string lastName, string fatherName,int Year,
int Month,int Day, string nationalcode)
{
FirstName = firstName;
LastName = lastName;
FatherName = fatherName;
try
{
PersianCalendar pc = new PersianCalendar();
_birthDate = pc.ToDateTime(Year, Month, Day,0,0,0,0);
}
catch
{
throw new ArgumentException("Argument Of Birth Date is Not valid");
}
Nationalcode = nationalcode;

}

public virtual int CompareTo(object obj)
{
Specs temp = obj as Specs;
if (temp != null)
{
if (this.Nationalcode==temp.Nationalcode)
{
return 0;
}
else if (decimal.Parse(this.Nationalcode) > decimal.Parse(temp.Nationalcode))
{
return 1;
}
else
{
return -1;
}

}
else
{
throw new ArgumentException("object is not a SpecsClass");
}
} public class Specs:IComparable
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
private set
{
if (!string.IsNullOrEmpty(value))
_firstName = value;
else
throw new ArgumentException("FirstName is not valid");
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
private set
{
if (!string.IsNullOrEmpty(value))
_lastName = value;
else
throw new ArgumentException("LastName is not valid");
}
}

private string _fatherName;
public string FatherName
{
get { return _fatherName; }
private set
{
if (!string.IsNullOrEmpty(value))
_fatherName = value;
else
throw new ArgumentException("FatherName is not valid");
}
}

private DateTime _birthDate;
public int Year
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetYear(_birthDate);
}
}
public int Month
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetMonth(_birthDate);
}
}
public int Day
{
get
{
PersianCalendar pc = new PersianCalendar();
return pc.GetDayOfMonth(_birthDate);
}
}
public string BirthDateToString
{
get
{
return ComplementPersianClanander.ToStringDate(_birthDate );
}
}
public string ToWrittenBirthDate
{
get
{
return ComplementPersianClanander.ToWrittenDate(_birthDat e);
}
}

private String _nationalcode;
public string Nationalcode
{
get { return _nationalcode; }

private set
{
string temp = value;
if (temp.Length == 10)
{
decimal numberTemp = 0;
decimal.TryParse(temp, out numberTemp);
if (numberTemp != 0)
_nationalcode = value;
else
throw new ArgumentException("Nationalcode is not valid");
}
else
throw new ArgumentException("Nationalcode is not valid");
}
}

public Specs(string firstName, string lastName, string fatherName,int Year,
int Month,int Day, string nationalcode)
{
FirstName = firstName;
LastName = lastName;
FatherName = fatherName;
try
{
PersianCalendar pc = new PersianCalendar();
_birthDate = pc.ToDateTime(Year, Month, Day,0,0,0,0);
}
catch
{
throw new ArgumentException("Argument Of Birth Date is Not valid");
}
Nationalcode = nationalcode;

}

public virtual int CompareTo(object obj)
{
Specs temp = obj as Specs;
if (temp != null)
{
if (this.Nationalcode==temp.Nationalcode)
{
return 0;
}
else if (decimal.Parse(this.Nationalcode) > decimal.Parse(temp.Nationalcode))
{
return 1;
}
else
{
return -1;
}

}
else
{
throw new ArgumentException("object is not a SpecsClass");
}
}


//کلاس درخت جنریک:
public class TreeGeneric<T> where T : IComparable
{
public TreeGeneric<T> Right { get; private set; }
public T Data { get; private set; }
public TreeGeneric<T> Left { get; private set; }

public TreeGeneric(T nodeData)
{
Data = nodeData;
Right = null;
Left = null;
}

public void Insert(T insertValue)
{
if (insertValue.CompareTo(Data) < 0)
{
if (Left == null)
Left = new TreeGeneric<T>(insertValue);
else
Left.Insert(insertValue);
}
else if (insertValue.CompareTo(Data) > 0)
{
if (Right == null)
Right = new TreeGeneric<T>(insertValue);
else
Right.Insert(insertValue);
}
}

public static void InOrderMinToMax(TreeGeneric<T> tree, List<T> listT)
{
if (tree != null)
{
InOrderMinToMax(tree.Left, listT);
listT.Add(tree.Data);
InOrderMinToMax(tree.Right, listT);
}
}

public static void InOrderMaxToMin(TreeGeneric<T> tree, List<T> listT)
{
if (tree != null)
{
InOrderMaxToMin(tree.Right, listT);
listT.Add(tree.Data);
InOrderMaxToMin(tree.Left, listT);
}
}

public static void SearchItem(TreeGeneric<T> tree, T Item,ref T defult)
{
if (tree != null)
{
if (Item.CompareTo(tree.Data) == 0)
{
defult = tree.Data;
return;
}

if (Item.CompareTo(tree.Data) < 0 && tree.Left != null)
SearchItem(tree.Left, Item, ref defult);
else if (Item.CompareTo(tree.Data) > 0 && tree.Right != null)
SearchItem(tree.Right, Item, ref defult);
}
}

کلاس اول یک کلاس مشخصات میباشد که بر اساس کد ملی امکان جست و جو و سورت در کلاس درخت جنریک را دارد.
با داده های رندوم یک میلیون دیتا به این درخت اضافه کردم و سورت کردن ان بر حسب کد ملی کمتر از چهار ثانیه به طول انجامید و جست وجوی یک کد ملی خاص کمتر از یک صدم ثانبه.
البته این بدون در نظر گرفتن لود دیتا در گرید می باشد.
شما هم میتوانید امتحان کنید.
با سپاس آرین