Advertisement
Infiniti_Inter

21 - II

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