fork download
  1. // Generic programming and operator overloading
  2.  
  3.  
  4. using System.Text;
  5.  
  6.  
  7. /// <summary>
  8. /// Represents a tree, or a subtree, there's no difference.
  9. /// The tree is kept sorted, but not balanced.
  10. /// </summary>
  11. /// <typeparam name="T">
  12. /// Any type to be stored in the tree, as long as CompareTo() (IComparable)
  13. /// is defined. Also the tree itself must present CompareTo() (IComparable),
  14. /// and Equals(Tree<T>?) (IEquatable<Tree<T>>).
  15. /// </typeparam>
  16. public class Tree<T>: IEquatable<Tree<T>>, IComparable<Tree<T>>
  17. where T: IComparable<T>
  18. {
  19. public T Val { get; set; }
  20. public Tree<T>? Right { get; private set; }
  21. public Tree<T>? Left { get; private set; }
  22.  
  23. /// <summary>Adds a new value to the tree, inserting is sorted.</summary>
  24. /// <param name="v">The given new value.</param>
  25. /// <returns>The top of the tree, so: this.</returns>
  26. public Tree<T> Add(T v)
  27. {
  28. if ( v.CompareTo( this.Val ) <= 0 ) {
  29. if ( this.Left is not null ) {
  30. this.Left.Add( v );
  31. } else {
  32. this.Left = new Tree<T>{ Val = v };
  33. }
  34. } else if (this.Right is not null ) {
  35. this.Right.Add( v );
  36. } else {
  37. this.Right = new Tree<T>{ Val = v };
  38. }
  39.  
  40. return this;
  41. }
  42.  
  43. /// <summary>
  44. /// Eliminates the node with the given value, as well as all
  45. /// the subnodes below that node.
  46. /// </summary>
  47. /// <param name="v">The given value for deletion.</param>
  48. /// <returns>The top of the tree, so: this.</returns>
  49. public Tree<T>? Remove(T v)
  50. {
  51. if ( this.Val.CompareTo( v ) == 0 ) {
  52. return null;
  53. }
  54.  
  55. if ( this.Left is not null ) {
  56. if ( this.Left.Val.CompareTo( v ) == 0 ) {
  57. this.Left = null;
  58. } else {
  59. this.Left.Remove( v );
  60. }
  61. }
  62.  
  63. if ( this.Right is not null ) {
  64. if ( this.Right.Val.CompareTo( v ) == 0 ) {
  65. this.Right = null;
  66. } else {
  67. this.Right.Remove( v );
  68. }
  69. }
  70.  
  71. return this;
  72. }
  73.  
  74. /// <summary>Compares two trees, this and other (given as parameter).
  75. /// </summary>
  76. /// <param name="other">The other, given tree, to compare this tree with.</param>
  77. /// <returns>
  78. /// Negative if this is before other, 0 if they are equal, positive otherwise.
  79. /// </returns>
  80. public int CompareTo(Tree<T>? other)
  81. {
  82. if ( other is null ) {
  83. return 1;
  84. }
  85.  
  86. if ( this.Equals( other ) ) {
  87. return 0;
  88. }
  89.  
  90. return this.Count() - other.Count();
  91. }
  92.  
  93. public override bool Equals(object? obj)
  94. => obj is Tree<T> t && this.Equals( t );
  95.  
  96. /// <summary>
  97. /// Returns an int with a value related to the value stored.
  98. /// </summary>
  99. public override int GetHashCode()
  100. => this.Val.GetHashCode();
  101.  
  102. public bool Equals(Tree<T>? other)
  103. {
  104. bool toret = false;
  105.  
  106. if ( other is not null
  107. && this.Val.CompareTo( other.Val ) == 0 )
  108. {
  109. toret = true;
  110.  
  111. if ( this.Left is not null
  112. && other.Left is not null )
  113. {
  114. toret = this.Left!.Equals( other.Left );
  115. } else {
  116. toret = this.Left is null
  117. && other.Left is null;
  118. }
  119.  
  120. if ( this.Right is not null
  121. && other.Right is not null )
  122. {
  123. toret = this.Right!.Equals( other.Right );
  124. } else {
  125. toret = this.Right is null
  126. && other.Right is null;
  127. }
  128. }
  129.  
  130. return toret;
  131. }
  132.  
  133. /// <summary>Counts the number of nodes in this tree.</summary>
  134. /// <returns>An int, at least 1.</returns>
  135. public int Count()
  136. {
  137. int toret = 1;
  138.  
  139. if ( this.Left is not null ) {
  140. toret += this.Left.Count();
  141. }
  142.  
  143. if ( this.Right is not null ) {
  144. toret += this.Right.Count();
  145. }
  146.  
  147. return toret;
  148. }
  149.  
  150. /// <summary>
  151. /// Visits all nodes, and calls ToString() to all of them.
  152. /// </summary>
  153. public override string ToString()
  154. {
  155. var toret = new StringBuilder();
  156.  
  157. if ( this.Left is not null ) {
  158. toret.Append( this.Left.ToString() );
  159. }
  160.  
  161. toret.Append( $"{this.Val}" );
  162.  
  163. if ( this.Right is not null ) {
  164. toret.Append( this.Right.ToString() );
  165. }
  166.  
  167. return toret.ToString();
  168. }
  169.  
  170. /// <summary>
  171. /// Creates a new tree, which is a copy of this.
  172. /// </summary>
  173. public Tree<T> Copy()
  174. {
  175. var toret = new Tree<T>{ Val = this.Val };
  176.  
  177. if ( this.Left is not null ) {
  178. toret.Left = this.Left.Copy();
  179. }
  180.  
  181. if ( this.Right is not null ) {
  182. toret.Right = this.Right.Copy();
  183. }
  184.  
  185. return toret;
  186. }
  187.  
  188. public static Tree<T> operator+(Tree<T> t, T v)
  189. {
  190. return t.Copy().Add( v );
  191. }
  192.  
  193. public static Tree<T> operator+(T v, Tree<T> t)
  194. {
  195. return t.Copy().Add( v );
  196. }
  197.  
  198. public static Tree<T>? operator-(Tree<T> t, T v)
  199. {
  200. return t.Copy().Remove( v );
  201. }
  202.  
  203. public static bool operator==(Tree<T>? t1, Tree<T>? t2)
  204. {
  205. return t1 is not null && t1.Equals( t2 );
  206. }
  207.  
  208. public static bool operator!=(Tree<T>? t1, Tree<T>? t2)
  209. {
  210. return !( t1 == t2 );
  211. }
  212.  
  213. public static bool operator<(Tree<T>? t1, Tree<T>? t2)
  214. {
  215. return t1 is not null && t1.CompareTo( t2 ) < 0;
  216. }
  217.  
  218. public static bool operator>(Tree<T>? t1, Tree<T>? t2)
  219. {
  220. return t1 is not null && t1.CompareTo( t2 ) > 0;
  221. }
  222.  
  223. public static bool operator<=(Tree<T>? t1, Tree<T>? t2)
  224. {
  225. return t1 is not null && t1.CompareTo( t2 ) <= 0;
  226. }
  227.  
  228. public static bool operator>=(Tree<T>? t1, Tree<T>? t2)
  229. {
  230. return t1 is not null && t1.CompareTo( t2 ) >= 0;
  231. }
  232. }
  233.  
  234. class App {
  235. static void Main()
  236. {
  237. var t0 = new Tree<int>{ Val = 0 };
  238. var t2 = new Tree<int>{ Val = 3 };
  239. var t1 = new Tree<int>{ Val = 3 };
  240.  
  241. for(int i = 0; i < 10; ++i) {
  242. // t1 += i; // t1 = t1 + v
  243. // i += t1 // t1 = i + t1
  244. t1 = i + t1;
  245. t2 += i;
  246. }
  247.  
  248. var tc = t2.Copy();
  249.  
  250. Console.WriteLine( "# Genericidad y sobercarga de operadores" );
  251. Console.WriteLine( "## Intro" );
  252. Console.WriteLine( $"- t0 = {t0}" );
  253. Console.WriteLine( $"- t1 = {t1}" );
  254. Console.WriteLine( $"- t2 = {t2}" );
  255. Console.WriteLine( $"- tc = {tc}" );
  256. t1 -= 7; // t1.Remove( 7 );
  257. Console.WriteLine( "## t1 - 7 (7 eliminado)" );
  258. Console.WriteLine( $"- t1 = {t1}" );
  259. Console.WriteLine( $"- t2 = {t2}" );
  260. Console.WriteLine( $"- tc = {tc}" );
  261. Console.WriteLine( "## Equals" );
  262. Console.WriteLine( $"- t1 Equals t2? {t1!.Equals( t2 )}" );
  263. Console.WriteLine( $"- t2 Equals tc? {t2.Equals( tc )}" );
  264. Console.WriteLine( $"- t1 == t2? {t1 == t2}" );
  265. Console.WriteLine( $"- t1 == tc? {t1 == tc}" );
  266. Console.WriteLine( $"- t2 == tc? {t2 == tc}" );
  267. Console.WriteLine( "## t.Count" );
  268. Console.WriteLine( $"- num. nodos t1: {t1.Count()}" );
  269. Console.WriteLine( $"- num. nodos t2: {t2.Count()}" );
  270. Console.WriteLine( $"- num. nodos tc: {tc.Count()}" );
  271. Console.WriteLine( "## CompareTo? " );
  272. Console.WriteLine( $"- t1 compareTo t2? {t1.CompareTo( t2 )}" );
  273. Console.WriteLine( $"- t1 < t2? {t1 < t2}" );
  274. Console.WriteLine( $"- t1 > t2? {t1 > t2}" );
  275. Console.WriteLine( $"- t1 <= t2? {t1 <= t2}" );
  276. Console.WriteLine( $"- t1 >= t2? {t1 >= t2}" );
  277. }
  278. }
  279.  
Success #stdin #stdout 0.05s 5220KB
stdin
Standard input is empty
stdout
# Genericidad y sobercarga de operadores
## Intro
- t0 = 0
- t1 = 01233456789
- t2 = 01233456789
- tc = 01233456789
## t1 - 7 (7 eliminado)
- t1 = 01233456
- t2 = 01233456789
- tc = 01233456789
## Equals
- t1 Equals t2? False
- t2 Equals tc? True
- t1 == t2?     False
- t1 == tc?     False
- t2 == tc?     True
## t.Count
- num. nodos t1: 8
- num. nodos t2: 11
- num. nodos tc: 11
## CompareTo? 
- t1 compareTo t2? -3
- t1 < t2?         True
- t1 > t2?         False
- t1 <= t2?        True
- t1 >= t2?        False