Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs_________________________
- using System;
- namespace test
- {
- class Program
- {
- public static void Main (string[] args)
- {
- Console.WriteLine ("Hello World!");
- BinaryTree bt = new BinaryTree ();
- string ins = "-a*b+/cd/ef";
- for (int i = 0; i < ins.Length; i++)
- {
- bt.Add (ins [i]);
- }
- bt.Preorder ();
- Console.WriteLine ();
- bt.Postorder ();
- }
- }
- }
- Tree.cs _______________________________________
- using System;
- namespace test
- {
- public class BinaryTree
- {
- //вложенный класс, отвечающий за узлы и операции для дерева
- public class Node
- {
- public char inf;
- public Node left;
- public Node right;
- //public Node parent;
- //создание узла
- public Node (char nodeInf)
- {
- inf = nodeInf;
- left = null;
- right = null;
- }
- public static void Add(ref Node r, char n, ref bool flag)
- {
- if (r == null)
- {
- r = new Node(n);
- flag = true;
- }
- else
- {
- if (!isOp(r.inf))
- {
- return;
- }
- Add(ref r.left, n,ref flag);
- if(!flag)
- Add(ref r.right, n, ref flag);
- }
- }
- public static void Preorder(Node r)
- {
- if (r != null)
- {
- Console.Write ("{0}", r.inf);
- Preorder (r.left);
- Preorder (r.right);
- }
- }
- public static void Postorder(Node r)
- {
- if (r != null)
- {
- Postorder (r.left);
- Postorder (r.right);
- Console.Write ("{0}", r.inf);
- }
- }
- public static void Inorder(Node r)
- {
- if (r != null)
- {
- Inorder (r.left);
- Console.Write ("{0}", r.inf);
- Inorder (r.right);
- }
- }
- private static Boolean isOp(char c)
- {
- return c == '+' || c == '-' || c == '*' || c == '/';
- }
- }
- //конец вложенного класса
- Node tree;
- public Node Tree
- {
- get{return tree;}
- }
- public char Inf
- {
- set{tree.inf = value;}
- get{return tree.inf;}
- }
- public BinaryTree()
- {
- tree = null;
- }
- private BinaryTree (Node r)
- {
- tree = r;
- }
- public void Add(char nodeInf)
- {
- bool flag = false;
- Node.Add (ref tree, nodeInf, ref flag);
- }
- public void Preorder()
- {
- Node.Preorder (tree);
- }
- public void Postorder()
- {
- Node.Postorder (tree);
- }
- public void Inorder()
- {
- Node.Inorder (tree);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment