Advertisement
Infiniti_Inter

21 - II # 5

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