Seal_of_approval

Pr15R1ex5

Mar 4th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. Program.cs
  2.  
  3.  
  4. using System;
  5. using System.IO;
  6.  
  7. namespace test
  8. {
  9.     class Program
  10.     {
  11.         public static void Main (string[] args)
  12.         {
  13.             BinaryTree myTree = new BinaryTree ();
  14.             using (StreamReader fileIn = new StreamReader ("/Users/masha/Projects/test/test/input.txt"))
  15.             {
  16.  
  17.                 string line = fileIn.ReadToEnd ();
  18.                 string[] mas = line.Split (' ');
  19.  
  20.                 foreach (string item in mas)
  21.                     myTree.Add (int.Parse(item));
  22.  
  23.                 int sum = 0;
  24.                 sum += Convert.ToInt32(myTree.Walk(myTree.Tree));
  25.  
  26.                 Console.WriteLine ("{0}",sum);
  27.             }
  28.         }
  29.     }
  30. }
  31. ____________________________________________________
  32. BinaryTree.cs
  33.  
  34. using System;
  35.  
  36. namespace test
  37. {
  38.     public class BinaryTree
  39.     {
  40.         //вложенный класс, отвечающий за узлы и операции для дерева
  41.         public class Node
  42.         {
  43.             public object inf;
  44.             public Node left;
  45.             public Node right;
  46.  
  47.             //создание узла
  48.             public Node (object nodeInf)
  49.             {
  50.                 inf = nodeInf;
  51.                 left = null;
  52.                 right = null;
  53.             }
  54.  
  55.             //добавление узла согласно определению бин. дерева
  56.             public static void Add(ref Node r, object nodeInf)
  57.             {
  58.                 if (r == null)
  59.                 {
  60.                     r = new Node (nodeInf);
  61.                 }
  62.                 else
  63.                 {
  64.                     if (((IComparable)(r.inf)).CompareTo(nodeInf) > 0)
  65.                         Add (ref r.left, nodeInf);
  66.                     else
  67.                         Add (ref r.right, nodeInf);
  68.                 }
  69.             }
  70.  
  71.             //поиск листьев
  72.             public static object Sheet(Node x)
  73.             {
  74.                 if (x != null)
  75.                 {
  76.                     if (x.left != null)
  77.                         return Sheet (x.left);
  78.                     else if (x.right != null)
  79.                         return Sheet (x.right);
  80.                     else
  81.                         return 0;
  82.                 }
  83.                 return 0;
  84.             }
  85.         }
  86.         //конец вложенного класса
  87.  
  88.         Node tree;
  89.  
  90.         public Node Tree
  91.         {
  92.             get{return tree;}
  93.         }
  94.  
  95.         public object Inf
  96.         {
  97.             set{tree.inf = value;}
  98.             get{return tree.inf;}
  99.         }
  100.  
  101.         public BinaryTree()
  102.         {
  103.             tree = null;
  104.         }
  105.  
  106.         private BinaryTree (Node r)
  107.         {
  108.             tree = r;
  109.         }
  110.  
  111.         public void Add(object nodeInf)
  112.         {
  113.             Node.Add (ref tree, nodeInf);
  114.         }
  115.  
  116.         public object Sheet()
  117.         {
  118.             return Node.Sheet (tree);
  119.         }
  120.  
  121.         public int Walk(Node node)
  122.         {
  123.             if (node == null)
  124.             {
  125.                 return 0;
  126.             }
  127.  
  128.             if (node.left == null && node.right == null )
  129.             {
  130.                 return (int)node.inf;
  131.             }
  132.  
  133.             else
  134.             {
  135.                 return Walk(node.left) + Walk(node.right);
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment