Advertisement
Infiniti_Inter

21 - I

Mar 11th, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8. public class BinaryTree
  9. {
  10.     static int cnt = 0;
  11.     public void Reset() => cnt = 0;
  12.     public int Solve() => cnt;
  13.     private class Node
  14.     {
  15.         public object inf;
  16.         public Node left;
  17.         public Node rigth;
  18.                          
  19.         public Node(object nodeInf)
  20.         {
  21.             inf = nodeInf;
  22.             left = null;
  23.             rigth = null;
  24.         }
  25.         public static void Add(ref Node root, object nodeInf)
  26.         {
  27.             if (root == null)
  28.             {
  29.                 root = new Node(nodeInf);
  30.             }
  31.             else
  32.             {
  33.                 if (((IComparable)(root.inf)).CompareTo(nodeInf) > 0)
  34.                 {
  35.                     Add(ref root.left, nodeInf);
  36.                 }
  37.                 else
  38.                 {
  39.                     Add(ref root.rigth, nodeInf);
  40.                 }
  41.             }
  42.         }
  43.         public static void Preorder(Node root) //прямой обход дерева
  44.         {
  45.             if (root != null)
  46.             {
  47.                 if (root.left == null && root.rigth != null)
  48.                     BinaryTree.cnt += (int)root.inf;
  49.                 //Console.Write("{0} ", root.inf);
  50.                 Preorder(root.left);
  51.                 Preorder(root.rigth);
  52.                
  53.             }
  54.         }
  55.         public static void Inorder(Node root) //симметричный обход дерева
  56.         {
  57.             if (root != null)
  58.             {
  59.                 Inorder(root.left);
  60.                 Console.Write("{0} ", root.inf);
  61.                 Inorder(root.rigth);
  62.             }
  63.         }
  64.         public static void Postorder(Node root) //обратный обход дерева
  65.         {
  66.             if (root != null)
  67.             {
  68.                 Postorder(root.left);
  69.                 Postorder(root.rigth);
  70.                 Console.Write("{0} ", root.inf);
  71.             }
  72.         }
  73.         //поиск ключевого узла в дереве
  74.         public static void Search(Node r, object key, out Node item)
  75.         {
  76.             if (r == null)
  77.             {
  78.                 item = null;
  79.             }
  80.             else
  81.             {
  82.                 if (((IComparable)(r.inf)).CompareTo(key) == 0)
  83.                 {
  84.                     item = r;
  85.                 }
  86.                 else
  87.  
  88.                 {
  89.                     if (((IComparable)(r.inf)).CompareTo(key) > 0)
  90.                     {
  91.                         Search(r.left, key, out item);
  92.                     }
  93.                     else
  94.                     {
  95.                         Search(r.rigth, key, out item);
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         //методы Del и Delete позволяют удалить узел в дереве так, чтобы дерево при этом
  101.         //оставалось деревом бинарного поиска
  102.         private static void Del(Node t, ref Node tr)
  103.         {
  104.             if (tr.rigth != null)
  105.             {
  106.                 Del(t, ref tr.rigth);
  107.             }
  108.             else
  109.             {
  110.                 t.inf = tr.inf;
  111.                 tr = tr.left;
  112.             }
  113.         }
  114.         public static void Delete(ref Node t, object key)
  115.         {
  116.             if (t == null)
  117.             {
  118.                 throw new Exception("Данное значение в дереве отсутствует");
  119.             }
  120.             else
  121.             {
  122.                 if (((IComparable)(t.inf)).CompareTo(key) > 0)
  123.                 {
  124.                     Delete(ref t.left, key);
  125.                 }
  126.                 else
  127.                 {
  128.                     if (((IComparable)(t.inf)).CompareTo(key) < 0)
  129.                     {
  130.                         Delete(ref t.rigth, key);
  131.                     }
  132.                     else
  133.                     {
  134.                         if (t.left == null)
  135.                         {
  136.                             t = t.rigth;
  137.                         }
  138.                         else
  139.                         {
  140.  
  141.                             if (t.rigth == null)
  142.                             {
  143.                                 t = t.left;
  144.                             }
  145.                             else
  146.                             {
  147.                                 Del(t, ref t.left);
  148.                             }
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     } //конец вложенного класса
  155.     Node tree; //ссылка на корень дерева
  156.                //свойство позволяет получить доступ к значению информационного поля корня дерева
  157.     public object Inf
  158.     {
  159.         set { tree.inf = value; }
  160.         get { return tree.inf; }
  161.     }
  162.     public BinaryTree() //открытый конструктор
  163.     {
  164.         tree = null;
  165.     }
  166.     private BinaryTree(Node r) //закрытый конструктор
  167.     {
  168.         tree = r;
  169.     }
  170.     public void Add(object nodeInf) //добавление узла в дерево
  171.     {
  172.         Node.Add(ref tree, nodeInf);
  173.     }
  174.     //организация различных способов обхода дерева
  175.     public void Preorder()
  176.     {
  177.         Node.Preorder(tree);
  178.     }
  179.     public void Inorder()
  180.     {
  181.         Node.Inorder(tree);
  182.     }
  183.     public void Postorder()
  184.     {
  185.         Node.Postorder(tree);
  186.     }
  187.     //поиск ключевого узла в дереве
  188.     public BinaryTree Search(object key)
  189.     {
  190.  
  191.         Node r;
  192.         Node.Search(tree, key, out r);
  193.         BinaryTree t = new BinaryTree(r);
  194.         return t;
  195.     }
  196.     //удаление ключевого узла в дереве
  197.     public void Delete(object key)
  198.     {
  199.         Node.Delete(ref tree, key);
  200.     }
  201. }
  202.  
  203.  
  204. class Program
  205. {
  206.     static void Main(string[] args)
  207.     {
  208.         //добавить input
  209.         BinaryTree tree = new BinaryTree();
  210.         using (StreamReader inp = new StreamReader("D:/input.txt"))
  211.         {
  212.             string[] line = inp.ReadToEnd().Split();
  213.             foreach (var v in line)
  214.                 if (v != " " && v != "")
  215.                     tree.Add(int.Parse(v));
  216.         }
  217.        
  218.         tree.Preorder();
  219.         Console.WriteLine();
  220.         Console.WriteLine($"Answer : {tree.Solve()} ");
  221.  
  222.     }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement