Advertisement
myname0

tree_contester_1

May 22nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. // sollution 1
  8. namespace ConsoleApplication
  9. {
  10.     class BinaryTree
  11.     {
  12.         private class Node
  13.         {
  14.             public char inf;
  15.             public Node left;
  16.             public Node right;
  17.  
  18.             public Node(char inf_)
  19.             {
  20.                 inf = inf_;
  21.                 left = null;
  22.                 right = null;
  23.             }
  24.  
  25.             public static void FormTree(ref Node r, char tmp)
  26.             {
  27.                 if (r == null)
  28.                     r = new Node(tmp);
  29.                 else if (r.left == null)
  30.                    FormTree(ref r.left, tmp);
  31.                 else FormTree(ref r.right, tmp);
  32.             }
  33.  
  34.             public static void Preorder(Node r, StreamWriter fin)
  35.             {
  36.                 if (r != null)
  37.                 {              
  38.                     Preorder(r.left, fin);
  39.                     fin.Write("{0}", r.inf);
  40.                     Preorder(r.right, fin);
  41.                 }
  42.             }            
  43.         }
  44.         Node tree;
  45.        
  46.         public BinaryTree()
  47.         {
  48.             tree = null;
  49.         }
  50.  
  51.         private BinaryTree(Node r)
  52.         {
  53.             tree = r;
  54.         }
  55.  
  56.         public void FormTree(string nameOfFile)
  57.         {
  58.             StreamReader fin = new StreamReader(nameOfFile);
  59.             string tmp = fin.ReadLine();
  60.             fin.Close();
  61.             Queue<char> letters = new Queue<char>();
  62.             Queue<char> symbols = new Queue<char>();
  63.             for (int i = 0; i < tmp.Length; i++)
  64.             {
  65.                 if (tmp[i] == '/' || '+' == tmp[i] || tmp[i] == '*' || tmp[i] == '-')
  66.                     symbols.Enqueue(tmp[i]);
  67.                 else letters.Enqueue(tmp[i]);
  68.             }
  69.             foreach (char i in symbols)
  70.             {
  71.                 Node.FormTree(ref tree, i);
  72.                 Node.FormTree(ref tree, letters.Peek());
  73.                 letters.Dequeue();
  74.             }
  75.             Node.FormTree(ref tree, letters.Peek());
  76.         }
  77.  
  78.         public void Preorder(string nameOfFile)
  79.         {
  80.             StreamWriter fin = new StreamWriter(nameOfFile);
  81.             Node.Preorder(tree, fin);
  82.             fin.Close();
  83.         }
  84.  
  85.     }
  86.     class Program
  87.     {
  88.  
  89.         static void Main(string[] args)
  90.         {
  91.             BinaryTree t = new BinaryTree();
  92.             t.FormTree("input.txt");
  93.             t.Preorder("output.txt");
  94.         }
  95.  
  96.     }
  97. }
  98.  
  99. // solution 2
  100.  
  101.     class BinaryTree
  102.     {
  103.         private class Node
  104.         {
  105.             public char inf;
  106.             public Node left;
  107.             public Node right;
  108.  
  109.             public Node(char inf_)
  110.             {
  111.                 inf = inf_;
  112.                 left = null;
  113.                 right = null;
  114.             }
  115.  
  116.             public static void FormTree(ref Node r, char tmp, ref bool flag)
  117.             {
  118.                 if (r == null)
  119.                 {
  120.                     r = new Node(tmp);
  121.                     flag = true;
  122.                 }
  123.                 else
  124.                 {
  125.                     if (!(r.inf == '/' || '+' == r.inf || r.inf == '*' || r.inf == '-'))
  126.                         return;
  127.  
  128.                     FormTree(ref r.left, tmp, ref flag);
  129.  
  130.                     if (!flag) FormTree(ref r.right, tmp, ref flag);
  131.                 }
  132.             }
  133.  
  134.             public static void Preorder(Node r, StreamWriter fin)
  135.             {
  136.                 if (r != null)
  137.                 {
  138.                     Preorder(r.left, fin);
  139.                     fin.Write("{0}", r.inf);
  140.                     Preorder(r.right, fin);
  141.                 }
  142.             }
  143.  
  144.         }
  145.         Node tree;
  146.  
  147.         public BinaryTree()
  148.         {
  149.             tree = null;
  150.         }
  151.  
  152.         private BinaryTree(Node r)
  153.         {
  154.             tree = r;
  155.         }
  156.  
  157.         public void FormTree(string nameOfFile)
  158.         {
  159.             StreamReader fin = new StreamReader(nameOfFile);
  160.             string tmp = fin.ReadLine();
  161.             fin.Close();
  162.             bool flag = false;
  163.             foreach (char i in tmp)
  164.             {
  165.                 flag = false;
  166.                 Node.FormTree(ref tree, i, ref flag);
  167.             }
  168.         }
  169.  
  170.         public void Preorder(string nameOfFile)
  171.         {
  172.             StreamWriter fin = new StreamWriter(nameOfFile);
  173.             Node.Preorder(tree, fin);
  174.             fin.Close();
  175.         }
  176.  
  177.     }
  178.     class Program
  179.     {
  180.         static void Main(string[] args)
  181.         {
  182.             BinaryTree t = new BinaryTree();
  183.             t.FormTree("input.txt");
  184.             t.Preorder("output.txt");
  185.         }
  186.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement