// Generic programming and operator overloading
using System.Text;
/// <summary>
/// Represents a tree, or a subtree, there's no difference.
/// The tree is kept sorted, but not balanced.
/// </summary>
/// <typeparam name="T">
/// Any type to be stored in the tree, as long as CompareTo() (IComparable)
/// is defined. Also the tree itself must present CompareTo() (IComparable),
/// and Equals(Tree<T>?) (IEquatable<Tree<T>>).
/// </typeparam>
public class Tree<T>: IEquatable<Tree<T>>, IComparable<Tree<T>>
where T: IComparable<T>
{
public T Val { get; set; }
public Tree<T>? Right { get; private set; }
public Tree<T>? Left { get; private set; }
/// <summary>Adds a new value to the tree, inserting is sorted.</summary>
/// <param name="v">The given new value.</param>
/// <returns>The top of the tree, so: this.</returns>
public Tree<T> Add(T v)
{
if ( v.CompareTo( this.Val ) <= 0 ) {
if ( this.Left is not null ) {
this.Left.Add( v );
} else {
this.Left = new Tree<T>{ Val = v };
}
} else if (this.Right is not null ) {
this.Right.Add( v );
} else {
this.Right = new Tree<T>{ Val = v };
}
return this;
}
/// <summary>
/// Eliminates the node with the given value, as well as all
/// the subnodes below that node.
/// </summary>
/// <param name="v">The given value for deletion.</param>
/// <returns>The top of the tree, so: this.</returns>
public Tree<T>? Remove(T v)
{
if ( this.Val.CompareTo( v ) == 0 ) {
return null;
}
if ( this.Left is not null ) {
if ( this.Left.Val.CompareTo( v ) == 0 ) {
this.Left = null;
} else {
this.Left.Remove( v );
}
}
if ( this.Right is not null ) {
if ( this.Right.Val.CompareTo( v ) == 0 ) {
this.Right = null;
} else {
this.Right.Remove( v );
}
}
return this;
}
/// <summary>Compares two trees, this and other (given as parameter).
/// </summary>
/// <param name="other">The other, given tree, to compare this tree with.</param>
/// <returns>
/// Negative if this is before other, 0 if they are equal, positive otherwise.
/// </returns>
public int CompareTo(Tree<T>? other)
{
if ( other is null ) {
return 1;
}
if ( this.Equals( other ) ) {
return 0;
}
return this.Count() - other.Count();
}
public override bool Equals(object? obj)
=> obj is Tree<T> t && this.Equals( t );
/// <summary>
/// Returns an int with a value related to the value stored.
/// </summary>
public override int GetHashCode()
=> this.Val.GetHashCode();
public bool Equals(Tree<T>? other)
{
bool toret = false;
if ( other is not null
&& this.Val.CompareTo( other.Val ) == 0 )
{
toret = true;
if ( this.Left is not null
&& other.Left is not null )
{
toret = this.Left!.Equals( other.Left );
} else {
toret = this.Left is null
&& other.Left is null;
}
if ( this.Right is not null
&& other.Right is not null )
{
toret = this.Right!.Equals( other.Right );
} else {
toret = this.Right is null
&& other.Right is null;
}
}
return toret;
}
/// <summary>Counts the number of nodes in this tree.</summary>
/// <returns>An int, at least 1.</returns>
public int Count()
{
int toret = 1;
if ( this.Left is not null ) {
toret += this.Left.Count();
}
if ( this.Right is not null ) {
toret += this.Right.Count();
}
return toret;
}
/// <summary>
/// Visits all nodes, and calls ToString() to all of them.
/// </summary>
public override string ToString()
{
var toret = new StringBuilder();
if ( this.Left is not null ) {
toret.Append( this.Left.ToString() );
}
toret.Append( $"{this.Val}" );
if ( this.Right is not null ) {
toret.Append( this.Right.ToString() );
}
return toret.ToString();
}
/// <summary>
/// Creates a new tree, which is a copy of this.
/// </summary>
public Tree<T> Copy()
{
var toret = new Tree<T>{ Val = this.Val };
if ( this.Left is not null ) {
toret.Left = this.Left.Copy();
}
if ( this.Right is not null ) {
toret.Right = this.Right.Copy();
}
return toret;
}
public static Tree<T> operator+(Tree<T> t, T v)
{
return t.Copy().Add( v );
}
public static Tree<T> operator+(T v, Tree<T> t)
{
return t.Copy().Add( v );
}
public static Tree<T>? operator-(Tree<T> t, T v)
{
return t.Copy().Remove( v );
}
public static bool operator==(Tree<T>? t1, Tree<T>? t2)
{
return t1 is not null && t1.Equals( t2 );
}
public static bool operator!=(Tree<T>? t1, Tree<T>? t2)
{
return !( t1 == t2 );
}
public static bool operator<(Tree<T>? t1, Tree<T>? t2)
{
return t1 is not null && t1.CompareTo( t2 ) < 0;
}
public static bool operator>(Tree<T>? t1, Tree<T>? t2)
{
return t1 is not null && t1.CompareTo( t2 ) > 0;
}
public static bool operator<=(Tree<T>? t1, Tree<T>? t2)
{
return t1 is not null && t1.CompareTo( t2 ) <= 0;
}
public static bool operator>=(Tree<T>? t1, Tree<T>? t2)
{
return t1 is not null && t1.CompareTo( t2 ) >= 0;
}
}
class App {
static void Main()
{
var t0 = new Tree<int>{ Val = 0 };
var t2 = new Tree<int>{ Val = 3 };
var t1 = new Tree<int>{ Val = 3 };
for(int i = 0; i < 10; ++i) {
// t1 += i; // t1 = t1 + v
// i += t1 // t1 = i + t1
t1 = i + t1;
t2 += i;
}
var tc = t2.Copy();
Console.WriteLine( "# Genericidad y sobercarga de operadores" );
Console.WriteLine( "## Intro" );
Console.WriteLine( $"- t0 = {t0}" );
Console.WriteLine( $"- t1 = {t1}" );
Console.WriteLine( $"- t2 = {t2}" );
Console.WriteLine( $"- tc = {tc}" );
t1 -= 7; // t1.Remove( 7 );
Console.WriteLine( "## t1 - 7 (7 eliminado)" );
Console.WriteLine( $"- t1 = {t1}" );
Console.WriteLine( $"- t2 = {t2}" );
Console.WriteLine( $"- tc = {tc}" );
Console.WriteLine( "## Equals" );
Console.WriteLine( $"- t1 Equals t2? {t1!.Equals( t2 )}" );
Console.WriteLine( $"- t2 Equals tc? {t2.Equals( tc )}" );
Console.WriteLine( $"- t1 == t2? {t1 == t2}" );
Console.WriteLine( $"- t1 == tc? {t1 == tc}" );
Console.WriteLine( $"- t2 == tc? {t2 == tc}" );
Console.WriteLine( "## t.Count" );
Console.WriteLine( $"- num. nodos t1: {t1.Count()}" );
Console.WriteLine( $"- num. nodos t2: {t2.Count()}" );
Console.WriteLine( $"- num. nodos tc: {tc.Count()}" );
Console.WriteLine( "## CompareTo? " );
Console.WriteLine( $"- t1 compareTo t2? {t1.CompareTo( t2 )}" );
Console.WriteLine( $"- t1 < t2? {t1 < t2}" );
Console.WriteLine( $"- t1 > t2? {t1 > t2}" );
Console.WriteLine( $"- t1 <= t2? {t1 <= t2}" );
Console.WriteLine( $"- t1 >= t2? {t1 >= t2}" );
}
}