Bob103

C#_Tree(I-2)

Feb 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.14 KB | None | 0 0
  1. class main
  2.     {
  3.         public static void Main()
  4.         {
  5.             BinaryTree tr1 = new BinaryTree();
  6.            
  7.             using (StreamReader fileIn = new StreamReader(@"C:\Users\lexpr\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\input.txt"))
  8.             {
  9.                 string line = fileIn.ReadToEnd();
  10.                 string[] data = line.Split(' ');
  11.  
  12.                 foreach (string item in data)
  13.                 {
  14.                     tr1.Add(int.Parse(item));
  15.                 }
  16.             }
  17.             tr1.Preorder();
  18.            
  19.         }
  20.     }
  21.  
  22. /****************************************************************************************************/
  23. public class BinaryTree
  24.     {
  25.         public class Node
  26.         {
  27.  
  28.             public object inf; //информационное поле
  29.             public Node left; //ссылка на левое поддерево
  30.             public Node rigth; //ссылка на правое поддерево
  31.  
  32.             //конструктор вложенного класса, создает узел дерева
  33.             public Node(object nodeInf)
  34.             {
  35.                 inf = nodeInf;
  36.                 left = null;
  37.                 rigth = null;
  38.             }
  39.  
  40.             //добавляет узел в дерево так, чтобы дерево оставалось деревом бинарного поиска
  41.             public static void Add(ref Node r, object nodeInf)
  42.             {
  43.                 if (r == null)
  44.                 {
  45.                     r = new Node(nodeInf);
  46.                 }
  47.                 else
  48.                 {
  49.                     if (((IComparable)(r.inf)).CompareTo(nodeInf) > 0)
  50.                     {
  51.                         Add(ref r.left, nodeInf);
  52.                     }
  53.                     else
  54.                     {
  55.                         Add(ref r.rigth, nodeInf);
  56.                     }
  57.                 }
  58.             }
  59.  
  60.             public static int Preorder(Node r) //прямой обход дерева
  61.             {
  62.                 int count = 0;
  63.                 if (r != null)
  64.                 {
  65.                     Console.Write("{0} ", r.inf);
  66.                     if ((int)r.inf % 2 == 0) { count++;};
  67.                     count += Preorder(r.left);
  68.                     count += Preorder(r.rigth);
  69.                 }
  70.                 Console.Write(count);
  71.                 return count;
  72.             }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.             private static void Del(Node t, ref Node tr)
  79.             {
  80.                 if (tr.rigth != null)
  81.                 {
  82.                     Del(t, ref tr.rigth);
  83.                 }
  84.                 else
  85.                 {
  86.                     t.inf = tr.inf;
  87.                     tr = tr.left;
  88.                 }
  89.             }
  90.  
  91.             public static void Delete(ref Node t, object key)
  92.             {
  93.                 if (t == null)
  94.                 {
  95.                     throw new Exception("Данное значение в дереве отсутствует");
  96.                 }
  97.                 else
  98.                 {
  99.                     if (((IComparable)(t.inf)).CompareTo(key) > 0)
  100.                     {
  101.                         Delete(ref t.left, key);
  102.                     }
  103.                     else
  104.                     {
  105.                         if (((IComparable)(t.inf)).CompareTo(key) < 0)
  106.                         {
  107.                             Delete(ref t.rigth, key);
  108.                         }
  109.                         else
  110.                         {
  111.                             if (t.left == null)
  112.                             {
  113.                                 t = t.rigth;
  114.                             }
  115.                             else
  116.                             {
  117.                                 if (t.rigth == null)
  118.                                 {
  119.                                     t = t.left;
  120.                                 }
  121.                                 else
  122.                                 {
  123.                                     Node tr = t.left;
  124.                                     Del(t, ref tr);
  125.                                 }
  126.                             }
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.  
  133.         Node tree; //ссылка на корень дерева
  134.         //свойство позволяет получить доступ к значению информационного поля корня дерева
  135.         public object Inf
  136.         {
  137.             set
  138.             {
  139.                 tree.inf = value;
  140.             }
  141.             get
  142.             {
  143.                 return tree.inf;
  144.             }
  145.         }
  146.  
  147.         public BinaryTree()
  148.         {
  149.             tree = null;
  150.         }
  151.  
  152.         private BinaryTree(Node r)
  153.         {
  154.             tree = r;
  155.         }
  156.  
  157.         public void Add(object nodeInf) //добавление узла в дерево
  158.         {
  159.             Node.Add(ref tree, nodeInf);
  160.         }
  161.  
  162.      
  163.        
  164.         public void Preorder()
  165.         {
  166.             Node.Preorder(tree);
  167.         }
  168.  
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment