// Generic programming and operator overloading using System.Text; /// /// Represents a tree, or a subtree, there's no difference. /// The tree is kept sorted, but not balanced. /// /// /// 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?) (IEquatable>). /// public class Tree: IEquatable>, IComparable> where T: IComparable { public T Val { get; set; } public Tree? Right { get; private set; } public Tree? Left { get; private set; } /// Adds a new value to the tree, inserting is sorted. /// The given new value. /// The top of the tree, so: this. public Tree Add(T v) { if ( v.CompareTo( this.Val ) <= 0 ) { if ( this.Left is not null ) { this.Left.Add( v ); } else { this.Left = new Tree{ Val = v }; } } else if (this.Right is not null ) { this.Right.Add( v ); } else { this.Right = new Tree{ Val = v }; } return this; } /// /// Eliminates the node with the given value, as well as all /// the subnodes below that node. /// /// The given value for deletion. /// The top of the tree, so: this. public Tree? 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; } /// Compares two trees, this and other (given as parameter). /// /// The other, given tree, to compare this tree with. /// /// Negative if this is before other, 0 if they are equal, positive otherwise. /// public int CompareTo(Tree? 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 && this.Equals( t ); /// /// Returns an int with a value related to the value stored. /// public override int GetHashCode() => this.Val.GetHashCode(); public bool Equals(Tree? 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; } /// Counts the number of nodes in this tree. /// An int, at least 1. 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; } /// /// Visits all nodes, and calls ToString() to all of them. /// 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(); } /// /// Creates a new tree, which is a copy of this. /// public Tree Copy() { var toret = new Tree{ 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 operator+(Tree t, T v) { return t.Copy().Add( v ); } public static Tree operator+(T v, Tree t) { return t.Copy().Add( v ); } public static Tree? operator-(Tree t, T v) { return t.Copy().Remove( v ); } public static bool operator==(Tree? t1, Tree? t2) { return t1 is not null && t1.Equals( t2 ); } public static bool operator!=(Tree? t1, Tree? t2) { return !( t1 == t2 ); } public static bool operator<(Tree? t1, Tree? t2) { return t1 is not null && t1.CompareTo( t2 ) < 0; } public static bool operator>(Tree? t1, Tree? t2) { return t1 is not null && t1.CompareTo( t2 ) > 0; } public static bool operator<=(Tree? t1, Tree? t2) { return t1 is not null && t1.CompareTo( t2 ) <= 0; } public static bool operator>=(Tree? t1, Tree? t2) { return t1 is not null && t1.CompareTo( t2 ) >= 0; } } class App { static void Main() { var t0 = new Tree{ Val = 0 }; var t2 = new Tree{ Val = 3 }; var t1 = new Tree{ 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}" ); } }