Дерево string преобразовать в int - C#
Формулировка задачи:
Добрый день! Есть дерево в string. Подскажите, как реализовать его в инте?
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication6
- {
- class Program
- {
- static void Main(string[] args)
- {
- Tree t = new Tree();
- t.Insert("персик");
- t.Insert("черника");
- t.Insert("мандарин");
- t.Insert("груша");
- t.Insert("яблоко");
- t.Insert("клубника");
- Console.WriteLine(t.Display(t));
- Tree s = t.Search("мандарин");
- Console.WriteLine(s.Display(s));
- Console.Read();
- }
- class Tree
- {
- private string value;
- private int count;
- private Tree left;
- private Tree right;
- // вставка
- public void Insert(string value)
- {
- if (this.value == null)
- this.value = value;
- else
- {
- if (this.value.CompareTo(value) == 1)
- {
- if (left == null)
- this.left = new Tree();
- left.Insert(value);
- }
- else if (this.value.CompareTo(value) == -1)
- {
- if (right == null)
- this.right = new Tree();
- right.Insert(value);
- }
- else
- throw new Exception("Узел уже существует");
- }
- this.count = Recount(this);
- }
- // поиск
- public Tree Search(string value)
- {
- if (this.value == value)
- return this;
- else if (this.value.CompareTo(value) == 1)
- {
- if (left != null)
- return this.left.Search(value);
- else
- throw new Exception("Искомого узла в дереве нет");
- }
- else
- {
- if (right != null)
- return this.right.Search(value);
- else
- throw new Exception("Искомого узла в дереве нет");
- }
- }
- // отображение в строку
- public string Display(Tree t)
- {
- string result = "";
- if (t.left != null)
- result += Display(t.left);
- result += t.value + " ";
- if (t.right != null)
- result += Display(t.right);
- return result;
- }
- // подсчет
- private int Recount(Tree t)
- {
- int count = 0;
- if (t.left != null)
- count += Recount(t.left);
- count++;
- if (t.right != null)
- count += Recount(t.right);
- return count;
- }
- // очистка
- public void Clear()
- {
- this.value = null;
- this.left = null;
- this.right = null;
- }
- // проверка пустоты
- public bool IsEmpty()
- {
- if (this.value == null)
- return true;
- else
- return false;
- }
- public void Remove(string value)
- {
- Tree t = Search(value);
- string[] str1 = Display(t).TrimEnd().Split(' ');
- string[] str2 = new string[str1.Length - 1];
- int i = 0;
- foreach (string s in str1)
- {
- if (s != value)
- str2[i++] = s;
- }
- t.Clear();
- foreach (string s in str2)
- t.Insert(s);
- this.count = Recount(this);
- }
- }
- }
- }
Решение задачи: «Дерево string преобразовать в int»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication12
- {
- class Program
- {
- static void Main(string[] args)
- {
- Tree<int> intTree = new Tree<int>();
- Tree<string> stringTree = new Tree<string>();
- Tree<DateTime> dtTree = new Tree<DateTime>();
- }
- }
- public class Tree<T>
- {
- public void Insert(T arg)
- {
- //Тут реализация...
- }
- public void Remove(T arg)
- {
- //Тут реализация...
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д