Seal_of_approval

tree_2

May 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. Program.cs_________________________
  2.  
  3. using System;
  4.  
  5. namespace test
  6. {
  7.     class Program
  8.     {
  9.         public static void Main (string[] args)
  10.         {
  11.             Console.WriteLine ("Hello World!");
  12.             BinaryTree bt = new BinaryTree ();
  13.  
  14.             string ins = "-a*b+/cd/ef";
  15.  
  16.             for (int i = 0; i < ins.Length; i++)
  17.             {
  18.                 bt.Add (ins [i]);
  19.             }
  20.  
  21.             bt.Preorder ();
  22.             Console.WriteLine ();
  23.             bt.Postorder ();
  24.         }
  25.     }
  26. }
  27.  
  28. Tree.cs _______________________________________
  29.  
  30. using System;
  31.  
  32. namespace test
  33. {
  34.     public class BinaryTree
  35.     {
  36.         //вложенный класс, отвечающий за узлы и операции для дерева
  37.         public class Node
  38.         {
  39.             public char inf;
  40.             public Node left;
  41.             public Node right;
  42.             //public Node parent;
  43.  
  44.             //создание узла
  45.             public Node (char nodeInf)
  46.             {
  47.                 inf = nodeInf;
  48.                 left = null;
  49.                 right = null;
  50.             }
  51.  
  52.             public static void Add(ref Node r, char n, ref bool flag)
  53.             {
  54.                 if (r == null)
  55.                 {
  56.                     r = new Node(n);
  57.                     flag = true;
  58.                 }
  59.                 else
  60.                 {
  61.                     if (!isOp(r.inf))
  62.                     {
  63.                         return;
  64.                     }
  65.  
  66.                     Add(ref r.left, n,ref flag);
  67.                     if(!flag)
  68.                         Add(ref r.right, n, ref flag);
  69.                 }
  70.             }
  71.  
  72.             public static void Preorder(Node r)
  73.             {
  74.                 if (r != null)
  75.                 {
  76.                     Console.Write ("{0}", r.inf);
  77.                     Preorder (r.left);
  78.                     Preorder (r.right);
  79.                 }
  80.             }
  81.  
  82.             public static void Postorder(Node r)
  83.             {
  84.                 if (r != null)
  85.                 {
  86.                     Postorder (r.left);
  87.                     Postorder (r.right);
  88.                     Console.Write ("{0}", r.inf);
  89.                 }
  90.             }
  91.  
  92.             public static void Inorder(Node r)
  93.             {
  94.                 if (r != null)
  95.                 {
  96.                     Inorder (r.left);
  97.                     Console.Write ("{0}", r.inf);
  98.                     Inorder (r.right);
  99.                 }
  100.             }
  101.  
  102.             private static Boolean isOp(char c)
  103.             {
  104.                 return c == '+' || c == '-' || c == '*' || c == '/';
  105.             }
  106.  
  107.         }
  108.         //конец вложенного класса
  109.  
  110.         Node tree;
  111.  
  112.         public Node Tree
  113.         {
  114.             get{return tree;}
  115.         }
  116.  
  117.         public char Inf
  118.         {
  119.             set{tree.inf = value;}
  120.             get{return tree.inf;}
  121.         }
  122.  
  123.         public BinaryTree()
  124.         {
  125.             tree = null;
  126.         }
  127.  
  128.         private BinaryTree (Node r)
  129.         {
  130.             tree = r;
  131.         }
  132.  
  133.         public void Add(char nodeInf)
  134.         {
  135.             bool flag = false;
  136.             Node.Add (ref tree, nodeInf, ref flag);
  137.         }
  138.  
  139.         public void Preorder()
  140.         {
  141.             Node.Preorder (tree);
  142.         }
  143.  
  144.         public void Postorder()
  145.         {
  146.             Node.Postorder (tree);
  147.         }
  148.  
  149.         public void Inorder()
  150.         {
  151.             Node.Inorder (tree);
  152.         }          
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment