Дерево string преобразовать в int - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день! Есть дерево в string. Подскажите, как реализовать его в инте?
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication6
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Tree t = new Tree();
  13. t.Insert("персик");
  14. t.Insert("черника");
  15. t.Insert("мандарин");
  16. t.Insert("груша");
  17. t.Insert("яблоко");
  18. t.Insert("клубника");
  19. Console.WriteLine(t.Display(t));
  20. Tree s = t.Search("мандарин");
  21. Console.WriteLine(s.Display(s));
  22. Console.Read();
  23. }
  24. class Tree
  25. {
  26. private string value;
  27. private int count;
  28. private Tree left;
  29. private Tree right;
  30. // вставка
  31. public void Insert(string value)
  32. {
  33. if (this.value == null)
  34. this.value = value;
  35. else
  36. {
  37. if (this.value.CompareTo(value) == 1)
  38. {
  39. if (left == null)
  40. this.left = new Tree();
  41. left.Insert(value);
  42. }
  43. else if (this.value.CompareTo(value) == -1)
  44. {
  45. if (right == null)
  46. this.right = new Tree();
  47. right.Insert(value);
  48. }
  49. else
  50. throw new Exception("Узел уже существует");
  51. }
  52. this.count = Recount(this);
  53. }
  54. // поиск
  55. public Tree Search(string value)
  56. {
  57. if (this.value == value)
  58. return this;
  59. else if (this.value.CompareTo(value) == 1)
  60. {
  61. if (left != null)
  62. return this.left.Search(value);
  63. else
  64. throw new Exception("Искомого узла в дереве нет");
  65. }
  66. else
  67. {
  68. if (right != null)
  69. return this.right.Search(value);
  70. else
  71. throw new Exception("Искомого узла в дереве нет");
  72. }
  73. }
  74. // отображение в строку
  75. public string Display(Tree t)
  76. {
  77. string result = "";
  78. if (t.left != null)
  79. result += Display(t.left);
  80. result += t.value + " ";
  81. if (t.right != null)
  82. result += Display(t.right);
  83. return result;
  84. }
  85. // подсчет
  86. private int Recount(Tree t)
  87. {
  88. int count = 0;
  89. if (t.left != null)
  90. count += Recount(t.left);
  91. count++;
  92. if (t.right != null)
  93. count += Recount(t.right);
  94. return count;
  95. }
  96. // очистка
  97. public void Clear()
  98. {
  99. this.value = null;
  100. this.left = null;
  101. this.right = null;
  102. }
  103. // проверка пустоты
  104. public bool IsEmpty()
  105. {
  106. if (this.value == null)
  107. return true;
  108. else
  109. return false;
  110. }
  111. public void Remove(string value)
  112. {
  113. Tree t = Search(value);
  114. string[] str1 = Display(t).TrimEnd().Split(' ');
  115. string[] str2 = new string[str1.Length - 1];
  116. int i = 0;
  117. foreach (string s in str1)
  118. {
  119. if (s != value)
  120. str2[i++] = s;
  121. }
  122. t.Clear();
  123. foreach (string s in str2)
  124. t.Insert(s);
  125. this.count = Recount(this);
  126. }
  127. }
  128. }
  129. }

Решение задачи: «Дерево string преобразовать в int»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication12
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Tree<int> intTree = new Tree<int>();
  13.  
  14.             Tree<string> stringTree = new Tree<string>();
  15.  
  16.             Tree<DateTime> dtTree = new Tree<DateTime>();
  17.         }
  18.     }
  19.     public class Tree<T>
  20.     {
  21.         public void Insert(T arg)
  22.         {
  23.             //Тут реализация...
  24.         }
  25.         public void Remove(T arg)
  26.         {
  27.             //Тут реализация...
  28.         }
  29.     }
  30. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 3.769 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут