Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- // sollution 1
- namespace ConsoleApplication
- {
- class BinaryTree
- {
- private class Node
- {
- public char inf;
- public Node left;
- public Node right;
- public Node(char inf_)
- {
- inf = inf_;
- left = null;
- right = null;
- }
- public static void FormTree(ref Node r, char tmp)
- {
- if (r == null)
- r = new Node(tmp);
- else if (r.left == null)
- FormTree(ref r.left, tmp);
- else FormTree(ref r.right, tmp);
- }
- public static void Preorder(Node r, StreamWriter fin)
- {
- if (r != null)
- {
- Preorder(r.left, fin);
- fin.Write("{0}", r.inf);
- Preorder(r.right, fin);
- }
- }
- }
- Node tree;
- public BinaryTree()
- {
- tree = null;
- }
- private BinaryTree(Node r)
- {
- tree = r;
- }
- public void FormTree(string nameOfFile)
- {
- StreamReader fin = new StreamReader(nameOfFile);
- string tmp = fin.ReadLine();
- fin.Close();
- Queue<char> letters = new Queue<char>();
- Queue<char> symbols = new Queue<char>();
- for (int i = 0; i < tmp.Length; i++)
- {
- if (tmp[i] == '/' || '+' == tmp[i] || tmp[i] == '*' || tmp[i] == '-')
- symbols.Enqueue(tmp[i]);
- else letters.Enqueue(tmp[i]);
- }
- foreach (char i in symbols)
- {
- Node.FormTree(ref tree, i);
- Node.FormTree(ref tree, letters.Peek());
- letters.Dequeue();
- }
- Node.FormTree(ref tree, letters.Peek());
- }
- public void Preorder(string nameOfFile)
- {
- StreamWriter fin = new StreamWriter(nameOfFile);
- Node.Preorder(tree, fin);
- fin.Close();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- BinaryTree t = new BinaryTree();
- t.FormTree("input.txt");
- t.Preorder("output.txt");
- }
- }
- }
- // solution 2
- class BinaryTree
- {
- private class Node
- {
- public char inf;
- public Node left;
- public Node right;
- public Node(char inf_)
- {
- inf = inf_;
- left = null;
- right = null;
- }
- public static void FormTree(ref Node r, char tmp, ref bool flag)
- {
- if (r == null)
- {
- r = new Node(tmp);
- flag = true;
- }
- else
- {
- if (!(r.inf == '/' || '+' == r.inf || r.inf == '*' || r.inf == '-'))
- return;
- FormTree(ref r.left, tmp, ref flag);
- if (!flag) FormTree(ref r.right, tmp, ref flag);
- }
- }
- public static void Preorder(Node r, StreamWriter fin)
- {
- if (r != null)
- {
- Preorder(r.left, fin);
- fin.Write("{0}", r.inf);
- Preorder(r.right, fin);
- }
- }
- }
- Node tree;
- public BinaryTree()
- {
- tree = null;
- }
- private BinaryTree(Node r)
- {
- tree = r;
- }
- public void FormTree(string nameOfFile)
- {
- StreamReader fin = new StreamReader(nameOfFile);
- string tmp = fin.ReadLine();
- fin.Close();
- bool flag = false;
- foreach (char i in tmp)
- {
- flag = false;
- Node.FormTree(ref tree, i, ref flag);
- }
- }
- public void Preorder(string nameOfFile)
- {
- StreamWriter fin = new StreamWriter(nameOfFile);
- Node.Preorder(tree, fin);
- fin.Close();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- BinaryTree t = new BinaryTree();
- t.FormTree("input.txt");
- t.Preorder("output.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement