Advertisement
myname0

практикум 15_1_4

Feb 19th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace практикум15_1_4
  9. {
  10.     class Program
  11.     {
  12.         class BinaryTree
  13.         {
  14.             private class Node: IComparable
  15.             {
  16.                 public object inf;
  17.                 public Node left;
  18.                 public Node right;
  19.  
  20.                 public Node(object inf_)
  21.                 {
  22.                     inf = inf_;
  23.                     left = null;
  24.                     right = null;
  25.                 }
  26.  
  27.                 public static void Add(ref Node r, object nodeinf)
  28.                 {
  29.                     if (r == null)
  30.                         r = new Node(nodeinf);
  31.                     else if (((IComparable)(r.inf)).CompareTo(nodeinf) > 0)
  32.                         Add(ref r.left, nodeinf);
  33.                     else Add(ref r.right, nodeinf);
  34.                 }
  35.  
  36.                 //public static void Preorder(Node r)
  37.                 //{
  38.                 //    if (r != null)
  39.                 //    {
  40.                 //        Console.Write("{0} ", r.inf);
  41.                 //        Preorder(r.left);
  42.                 //        Preorder(r.right);
  43.                 //    }
  44.                 //}
  45.  
  46.                 public static void MultipleNegativeNumbers(Node r, ref int Multiple)
  47.                 {
  48.                     if (r != null)
  49.                     {
  50.                         if ((int)r.inf < 0)
  51.                             Multiple *= (int)r.inf;
  52.                         MultipleNegativeNumbers(r.left, ref Multiple);
  53.                         MultipleNegativeNumbers(r.right, ref Multiple);
  54.                     }
  55.                 }
  56.                 public int CompareTo(object obj)
  57.                 {
  58.                     Node tree_ = (Node)obj;
  59.                     if (this.inf == tree_.inf) return 0;
  60.                     else if ((int)this.inf < (int)tree_.inf) return -1;
  61.                     else return 1;
  62.                 }
  63.             }
  64.             Node tree;
  65.             public object Inf
  66.             {
  67.                 set { tree.inf = value; }
  68.                 get { return tree.inf; }
  69.             }
  70.  
  71.             public BinaryTree()
  72.             {
  73.                 tree = null;
  74.             }
  75.  
  76.             private BinaryTree(Node r)
  77.             {
  78.                 tree = r;
  79.             }
  80.  
  81.             public void Add(object nodeInf)
  82.             {
  83.                 Node.Add(ref tree, nodeInf);
  84.             }
  85.  
  86.             //public void Preorder()
  87.             //{
  88.             //    Node.Preorder(tree);
  89.             //}
  90.  
  91.             public int MultipleNegativeNumers()
  92.             {
  93.                 int Multiple = 1;              
  94.                 Node.MultipleNegativeNumbers(tree, ref Multiple);
  95.                 return Multiple;
  96.             }
  97.         }
  98.  
  99.         static void Main(string[] args)
  100.         {
  101.             StreamReader fin = new StreamReader ("input.txt");
  102.             string temp = fin.ReadToEnd();
  103.             string[] numbers = temp.Split(' ');
  104.             fin.Close();
  105.             BinaryTree tree = new BinaryTree();
  106.             foreach(string item in numbers)
  107.                 tree.Add(int.Parse(item));
  108.             //tree.Preorder();
  109.             //Console.WriteLine();
  110.             Console.WriteLine("Произведение отрицательных узлов дерева: {0}", tree.MultipleNegativeNumers());
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement