Advertisement
Guest User

21 # 7

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